Question Problem regarding validation

swethajain

Well-known member
Joined
Feb 1, 2010
Messages
48
Programming Experience
Beginner
Hi,
The following are the criteria according to which i wrote the code,
3 comboboxes-without selecting any item control should not go to another
textbox- "
% Textbox- value should be between o to 100
Hrs textbox- value should not be greater than 16hrs
the code is

Imports System.Data.OleDb

Public Class HT_Insert
Dim myConnection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='h:/info2.mdb'")
Dim myCommand As OleDbCommand
Dim ra As Integer
Dim result As Integer

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strSQL As String = "SELECT * FROM Region"
Dim da As New OleDbDataAdapter(strSQL, myConnection)
Dim ds As New DataSet
da.Fill(ds, "Region")

With cbx_Region
.DataSource = ds.Tables("Region")
.DisplayMember = "Reg"
.ValueMember = "ID"
.SelectedIndex = -1

End With

Dim strSQL1 As String = "SELECT * FROM ConstructionPackage"
Dim da1 As New OleDbDataAdapter(strSQL1, myConnection)
Dim ds1 As New DataSet
da1.Fill(ds1, "ConstructionPackage")

With cbx_ConPack
.DataSource = ds1.Tables("ConstructionPackage")
.DisplayMember = "ConPack"
.ValueMember = "ID"
.SelectedIndex = -1
End With
Dim strSQL2 As String = "SELECT * FROM Trade"
Dim da2 As New OleDbDataAdapter(strSQL2, myConnection)
Dim ds2 As New DataSet
da2.Fill(ds2, "Trade")

With cbx_Trade
.DataSource = ds2.Tables("Trade")
.DisplayMember = "Trade"
.ValueMember = "ID"
.SelectedIndex = -1
End With
txtbx_Resource.Hide()
txtbx_Resource.Text = Environment.UserName.ToString()
'txt_Date.Text = DateString
dtp_Date.Hide()
'DateTimePicker1.CustomFormat = "dd'/'MM'/'yyyy hh':'mm "
Me.Text = Me.Text & " User: " & Environment.UserDomainName & "\" & Environment.UserName
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_Save.Click
For Each ctrl As Control In Me.Controls
If ctrl.ForeColor.ToString = "Red" Then
MsgBox("Data is not valid")
End If

Next
Dim dt As String = dtp_Date.Text
Dim re As String = txtbx_Resource.Text
Dim cp As String = cbx_ConPack.Text
Dim rg As String = cbx_Region.Text
Dim tr As String = cbx_Trade.Text
Dim lo As String = txtbx_Location.Text
Dim nn As String = txtbx_Networkname.Text
Dim ta As String = txtbx_Task.Text
Dim pr As String = txtbx_Percent.Text
Dim du As String = txtbx_HRS.Text
Dim ass As String = txtbx_Assumptions.Text
Try
myConnection.Open()
Dim str As String = "insert into Record([Dt],[Res],[Construction Package],[Region],[Trade],[Location],[Network Name],[Task],[Progress],[Duration],[Assumptions])values ('" & dt & "','" & re & "','" & cp & "','" & rg & "','" & tr & "','" & lo & "','" & nn & "','" & ta & "','" & pr & "','" & du & "','" & ass & "')"
Dim cmd As New OleDbCommand(str, myConnection)
ra = cmd.ExecuteNonQuery()
If (ra > 0) Then
result = MsgBox("Record Inserted", MsgBoxStyle.Information, "Message Box")
End If
myConnection.Close()
Catch ex As Exception
MessageBox.Show(ex.Message.ToString())
End Try
For Each ctrl As Control In Me.Controls
If ctrl.GetType.Name = "TextBox" Then ctrl.Text = ""
If ctrl.GetType.Name = "ComboBox" Then ctrl.Text = ""

Next
txtbx_Resource.Text = Environment.UserName.ToString()
End Sub
Private Sub cbx_ConPack_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles cbx_ConPack.Validating
Validate_ConPack()
End Sub
Private Sub cbx_Region_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles cbx_Region.Validating
Validate_Resource()
End Sub

Private Sub cbx_Trade_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles cbx_Trade.Validating
Validate_Trade()
End Sub
Private Sub txtbx_Percent_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtbx_Percent.Validating
Validate_Percentage()
End Sub
Private Sub txtbx_Task_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtbx_Task.Validating
Validate_Task()
End Sub
Private Sub txtbx_HRS_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtbx_HRS.Validating
Validate_Hours()
End Sub

Private Function Validate_Percentage() As Boolean
If txtbx_Percent.Text <= 0 Or txtbx_Percent.Text > 100 Or txtbx_Percent.Text = "" Then
MsgBox("Precentage should be between 0 to 100")
txtbx_Percent.Clear()
txtbx_Percent.Focus()
txtbx_Percent.BackColor = Color.Red
Else
txtbx_Percent.Text = Val(txtbx_Percent.Text)
txtbx_Percent.BackColor = Color.White
End If

End Function
Private Sub txtbx_Percent_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtbx_Percent.KeyPress
Dim allowedChars As String = "0123456789"
Dim disallowedChars As String = "abcdefghijklmnopqrstuvwxyz!@#$%&()-[]{}"
If allowedChars.IndexOf(e.KeyChar) = -1 Then
' Invalid Character
MsgBox("Enter numeric data")
End If
If allowedChars.IndexOf(e.KeyChar) = -1 Then
' Invalid Character
e.Handled = True
End If

End Sub
Private Function Validate_Hours() As Boolean
If txtbx_HRS.Text <= 0 Or txtbx_HRS.Text > 16 Or txtbx_HRS.Text = "" Then
MsgBox("Duration should not be greater than 16 HRS")
txtbx_HRS.Clear()
txtbx_HRS.Focus()
txtbx_HRS.BackColor = Color.Red
Else
txtbx_HRS.Text = Val(txtbx_HRS.Text)
txtbx_HRS.BackColor = Color.White
End If
End Function
Private Sub txtbx_HRS_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtbx_HRS.KeyPress
Dim allowedChars As String = "0123456789"
Dim disallowedChars As String = "abcdefghijklmnopqrstuvwxyz!@#$%&()-[]{}"
If allowedChars.IndexOf(e.KeyChar) = -1 Then
' Invalid Character
MsgBox("Enter numeric data")
End If
If allowedChars.IndexOf(e.KeyChar) = -1 Then
' Invalid Character
e.Handled = True
End If

End Sub

Private Function Validate_ConPack() As Boolean
If cbx_ConPack.SelectedIndex = -1 Then
cbx_ConPack.Focus()
cbx_ConPack.BackColor = Color.Red
Else
cbx_ConPack.BackColor = Color.White
End If

End Function
Private Function Validate_Resource() As Boolean
If cbx_Region.SelectedIndex = -1 Then
cbx_Region.Focus()
cbx_Region.BackColor = Color.Red
Else
cbx_Region.BackColor = Color.White
End If

End Function
Private Function Validate_Trade() As Boolean
If cbx_Trade.SelectedIndex = -1 Then
cbx_Trade.Focus()
cbx_Trade.BackColor = Color.Red
Else
cbx_Trade.BackColor = Color.White
End If

End Function
Private Function Validate_Task() As Boolean
If txtbx_Task.Text = "" Then
txtbx_Task.Clear()
txtbx_Task.Focus()
txtbx_Task.BackColor = Color.Red
Else
txtbx_Task.Text = txtbx_Task.Text
txtbx_Task.BackColor = Color.White
End If
End Function
End Class

The Problem is
1) when without entering any values if i press "save" record is getting inserted
2) when without entering any data if i press "search", red color is getting displayed in first combobox, which should not be displayed

Can anyone please help me?

Thanks,
Swetha.
 
Back
Top