Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form Validation before SQL Insert 1

Status
Not open for further replies.

lance59

IS-IT--Management
Mar 6, 2007
50
US
I have a form that has several text boxes and combo boxes. I need to validate that the text boxes and combo boxes have values in them before using the data to populate a table in SQL.

For the text boxes I found this, which does check to see if they are empty. But if the user just skips to the next text box the user still can click on the save button and the data or lack of goes into the SQL table.
Code:
Private Sub TextBox3_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox3.Validated

        Me.ErrorProvider1.SetError(TextBox3, "")

    End Sub


    Private Sub TextBox3_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox3.Validating

        Try

            If Me.TextBox3.Text.Length = 0 Then

                Me.ErrorProvider1.SetError(Me.TextBox3, "Please Enter a value")

                Throw New Exception("Please enter a value")

            End If

        Catch ex As Exception

            MsgBox(ex.Message)

        End Try

    End Sub

Any way to keep the user from clicking on the save button unless all the text boxes have text and combo boxes have something selected?

TIA
 
Validate at your button click event, but before you call your actual save routine. Here is an example. I'm assuming all controls you want to validate are contained in the form itself and not another container (panel, etc.). I'm also assuming your ComboBoxes require the user to select a value, and disallows typing in their own values. If either of those two assumptions are incorrect, you can modify this code to account for those differences
Code:
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim al As ArrayList = Me.CheckControlsForValues
        If al.Count > 0 Then
            Dim s As String = ""
            For Each st As String In al
                s &= st & Environment.NewLine
            Next
            MessageBox.Show("Please enter values for:  " & Environment.NewLine & s)
        Else
            'Put Save Routine Call Here
        End If
    End Sub

    Friend Function CheckControlsForValues() As ArrayList
        Dim al As New ArrayList
        For Each c As Control In Me.Controls
            If TypeOf c Is TextBox Then
                If CType(c, TextBox).Text.Trim = "" Then
                    al.Add(c.Name)
                End If
            ElseIf TypeOf c Is ComboBox Then
                If CType(c, ComboBox).SelectedIndex < 0 Then
                    al.Add(c.Name)
                End If
            End If
        Next
        Return al
    End Function
 
Thanks, I modified it to show that name rather then the control name for the user to understand.

Code:
Friend Function CheckControlsForValues() As ArrayList
        Dim al As New ArrayList
        For Each c As Control In Me.Controls
            If TypeOf c Is TextBox Then
                If CType(c, TextBox).Text.Trim = "" Then
                    Select Case c.Name
                        Case Is = "TextBox1"
                            al.Add("Serial Number")
                        Case Is = "TextBox3"
                            al.Add("Model")
                        Case Is = "TextBox4"
                            al.Add("PO #")
                        Case Is = "TextBox6"
                            al.Add("Price")
                    End Select
                End If
            ElseIf TypeOf c Is ComboBox Then
                If CType(c, ComboBox).SelectedIndex < 0 Then
                    Select Case c.Name
                        Case Is = "ComboBox1"
                            al.Add("Item Type")
                        Case Is = "ComboBox2"
                            al.Add("Owners Division")
                        Case Is = "ComboBox3"
                            al.Add("Manufacturer")
                        Case Is = "ComboBox4"
                            al.Add("OS Type")
                        Case Is = "ComboBox5"
                            al.Add("Vendor")
                        Case Is = "ComboBox6"
                            al.Add("Location")
                    End Select
                End If
            End If
        Next
        Return al
    End Function
 
Looks good. To be a little more dynamic, you might want to set the .Tag property to contain the "description" of your controls. Then, instead of the case statements, you can output the .Tag property of your control.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top