Validating input problem

johnuk4

Active member
Joined
Feb 18, 2006
Messages
25
Programming Experience
Beginner
Hi,

As you will all tell from reading this i am a novice so be warned. Basically iv been trying to validate user input however struggling to get the right if statement to work and what format to use. As you will see from the code below i have 4 textboxes and i just want a simple if statement or something to make sure each text box has a value in it before executing the database insert below. Sorry as i know this is pretty trival stuff its just been bugging me as anything i try it won't except.

Any help would be very much appreciated

thanks in advance

john

VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] btnLog_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] btnLog.Click
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] HardwareId [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Staff [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Description [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Location [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] d [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DateTime = DateTime.Now
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] MsgResult [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] MsgBoxResult
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Message [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2]HardwareId = ListId.Text
Staff = txtStaff.Text
Description = txtDescription.Text
Location = txtLocation.Text
 
[/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2]DBCon.Open()
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] sa [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] DSet4 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataSet, SQLStr4 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] dbAdaptr4 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Data.OleDb.OleDbDataAdapter = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Data.OleDb.OleDbDataAdapter
[/SIZE][SIZE=2][COLOR=#0000ff]With[/COLOR][/SIZE][SIZE=2] dbAdaptr4
.TableMappings.Add("Table", "Hardware_Problems")
SQLStr4 = "INSERT INTO Hardware_Problems (Hardware_Id, Date_Time, Staff_Reported, Problem_Description, Location) VALUES (@Hardware_Id, @Date_Time, @Staff_Reported, @Problem_Description, @Location)"
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] sqlCommand [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Data.OleDb.OleDbCommand
sqlCommand.Connection = DBCon
sqlCommand.CommandText = SQLStr4
sqlCommand.Parameters.Add("@Hardware_Id", System.Data.OleDb.OleDbType.VarChar).Value = HardwareId
sqlCommand.Parameters.Add("@Date_Time", System.Data.OleDb.OleDbType.Date).Value = d
sqlCommand.Parameters.Add("@Staff_Reported", System.Data.OleDb.OleDbType.VarChar).Value = Staff
sqlCommand.Parameters.Add("@Problem_Description", System.Data.OleDb.OleDbType.VarChar).Value = Description
sqlCommand.Parameters.Add("@Location", System.Data.OleDb.OleDbType.VarChar).Value = Location
sa = sqlCommand.ExecuteNonQuery()
Message = "Problem with " & cboPrinterMake.Text & " " & cboPrinterModel.Text
MsgBox(Message)
DBCon.Close()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]With
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception
Label1.Text = "Error reading database " & ex.Message
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
 
textboxes raise their own validation events so you can check
what has been entered in them and cancel the event if it's not
what you want. In addition to this you can add the handles
clause for each textbox to one validation event, rather than

having three different event handlers. Something like this....

VB.NET:
Private sub TextBox1_Validating(byval sender as object, 
byval e as system.componentmodel.canceleventargs) handles Textbox1.validating, 
textbox2.validating, textbox3.validating
 
'Cast to the textbox that raised the event..
Dim Tb as textbox = CType(sender,textbox)
 
if tb.text = string.empty then
e.cancel
Else
'Do your update
end sub
 
Hi,

thanks for the reply, iv tried to use what you recommended however a few errors pop up. Would you possibly know why i get the errors:

Argument not specified for parameter 'e' of 'Private Sub Validate(sender As Object, e As System.ComponentModel.CancelEventArgs)'.

and

Argument not specified for parameter 'sender' of 'Private Sub Validate(sender As Object, e As System.ComponentModel.CancelEventArgs)'.


here is the code below to show what iv done

thanks again


VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] btnLog_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] btnLog.Click
Validate()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Validate([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.componentmodel.CancelEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] txtLocation.Validating, txtStaff.Validating, txtDescription.Validating
[/SIZE][SIZE=2][COLOR=#008000]'Cast to the textbox that raised the event..
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Tb [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] TextBox = [/SIZE][SIZE=2][COLOR=#0000ff]CType[/COLOR][/SIZE][SIZE=2](sender, TextBox)
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] Tb.Text = [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2].Empty [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]e.Cancel()
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'Do your update
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] HardwareId [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Staff [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Description [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Location [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] d [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DateTime = DateTime.Now
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] MsgResult [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] MsgBoxResult
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Message [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2]HardwareId = ListId.Text
Staff = txtStaff.Text
Description = txtDescription.Text
Location = txtLocation.Text
 
[/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2]DBCon.Open()
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] sa [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] DSet4 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataSet, SQLStr4 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] dbAdaptr4 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Data.OleDb.OleDbDataAdapter = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Data.OleDb.OleDbDataAdapter
[/SIZE][SIZE=2][COLOR=#0000ff]With[/COLOR][/SIZE][SIZE=2] dbAdaptr4
.TableMappings.Add("Table", "Hardware_Problems")
SQLStr4 = "INSERT INTO Hardware_Problems (Hardware_Id, Date_Time, Staff_Reported, Problem_Description, Location) VALUES (@Hardware_Id, @Date_Time, @Staff_Reported, @Problem_Description, @Location)"
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] sqlCommand [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Data.OleDb.OleDbCommand
sqlCommand.Connection = DBCon
sqlCommand.CommandText = SQLStr4
sqlCommand.Parameters.Add("@Hardware_Id", System.Data.OleDb.OleDbType.VarChar).Value = HardwareId
sqlCommand.Parameters.Add("@Date_Time", System.Data.OleDb.OleDbType.Date).Value = d
sqlCommand.Parameters.Add("@Staff_Reported", System.Data.OleDb.OleDbType.VarChar).Value = Staff
sqlCommand.Parameters.Add("@Problem_Description", System.Data.OleDb.OleDbType.VarChar).Value = Description
sqlCommand.Parameters.Add("@Location", System.Data.OleDb.OleDbType.VarChar).Value = Location
sa = sqlCommand.ExecuteNonQuery()
Message = "Problem with " & cboPrinterMake.Text & " " & cboPrinterModel.Text
MsgBox(Message)
DBCon.Close()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]With
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception
Label1.Text = "Error reading database " & ex.Message
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
 
No, sorry you've misunderstood. Go to the top left combobox in the ide and click it, select listid, then go to the other combobox and select the validating event. This will open up the validation event handler for this textbox, with me? Then add the handles clauses for the other textbox's.
 
Here is another option:

The log button uses nested IF's to see if the text is present - if it is not blank the variables are set and the sub for getting the data is called (Me.RequestData()), if one is blank an error message is displayed and the sub is not called. Public variables hold the text - could be passed to the sub also, but I like public variables.

VB.NET:
[COLOR=Red]Public HardwareId AsString
Public Staff AsString
Public Description AsString
Public Location AsString[/COLOR]

PrivateSub btnLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLog.Click
        If Not listid.text = String.Empty Then
            HardwareId = ListId.Text
            If Not txtStaff.Text = String.Empty Then
                Staff = txtStaff.Text
                If Not txtDescription.Text = String.Empty Then
                    Description = txtDescription.Text
                    If Not txtLocation.Text = String.Empty Then
                        Location = txtLocation.Text
                        [COLOR=Blue]Me.RequestData()[/COLOR]
                    Else
                        MessageBox.Show("Location is blank", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If
                Else
                    MessageBox.Show("Description is blank", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
            Else
                MessageBox.Show("Staff is blank", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Else
            MessageBox.Show("ListId is blank", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
End sub

[COLOR=Blue] Private Sub RequestData[/COLOR]
Dim d As DateTime = DateTime.Now
Dim MsgResult As MsgBoxResult
Dim Message AsString
Try
DBCon.Open()
Dim sa AsInteger
Dim DSet4 AsNew DataSet, SQLStr4 AsString
Dim dbAdaptr4 As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter
With dbAdaptr4
.TableMappings.Add("Table", "Hardware_Problems")
SQLStr4 = "INSERT INTO Hardware_Problems (Hardware_Id, Date_Time, Staff_Reported, Problem_Description, Location) VALUES (@Hardware_Id, @Date_Time, @Staff_Reported, @Problem_Description, @Location)"
Dim sqlCommand AsNew System.Data.OleDb.OleDbCommand
sqlCommand.Connection = DBCon
sqlCommand.CommandText = SQLStr4
sqlCommand.Parameters.Add("@Hardware_Id", System.Data.OleDb.OleDbType.VarChar).Value = HardwareId
sqlCommand.Parameters.Add("@Date_Time", System.Data.OleDb.OleDbType.Date).Value = d
sqlCommand.Parameters.Add("@Staff_Reported", System.Data.OleDb.OleDbType.VarChar).Value = Staff
sqlCommand.Parameters.Add("@Problem_Description", System.Data.OleDb.OleDbType.VarChar).Value = Description
sqlCommand.Parameters.Add("@Location", System.Data.OleDb.OleDbType.VarChar).Value = Location
sa = sqlCommand.ExecuteNonQuery()
Message = "Problem with " & cboPrinterMake.Text & " " & cboPrinterModel.Text
MsgBox(Message)
DBCon.Close()
EndWith
Catch ex As Exception
Label1.Text = "Error reading database " & ex.Message
EndTry
End Sub

There are a lot of ways to skin this cat!
 
Back
Top