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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form Validation

Status
Not open for further replies.

696796

Programmer
Aug 3, 2004
218
GB
Hi,

I need to validate my form so that when a user tries to insert a record, certain fields must be completed. If a field isn't filled, i need the label next to it to be highlighted red. I think i'm nearly there but it isnt working quite how i'd like.

At the mo, it does the first if, then skips the rest and goes to the msgbox part - i need it to loop round i think so that it goes through and checks all the fiellds before displaying the message and highlighting each's label red.

Code:
    Public Sub validateForm()

        If Me.cboSupplier.SelectedValue = (0) Then
            lblSupplier.ForeColor = System.Drawing.Color.Red
        ElseIf cboMaterial.SelectedValue = (0) Then
            lblPartNo.ForeColor = System.Drawing.Color.Red
        ElseIf txtDefectDesc Is "" Then
            lblDefectDesc.ForeColor = System.Drawing.Color.Red
        ElseIf cboDefect.SelectedValue = (0) Then
            lblDefect.ForeColor = System.Drawing.Color.Red
        ElseIf cboAction.SelectedValue = (0) Then
            lblAction.ForeColor = System.Drawing.Color.Red
        End If

        'MsgBox("The fields in red must be updated")
        Exit Sub


    End Sub

What do i need to do to get this working please?

Al
 
Code:
Public Sub validateForm()

        If Me.cboSupplier.SelectedValue = (0) Then
            lblSupplier.ForeColor = System.Drawing.Color.Red
        End If
        If cboMaterial.SelectedValue = (0) Then
            lblPartNo.ForeColor = System.Drawing.Color.Red
        End If
        If txtDefectDesc Is "" Then
            lblDefectDesc.ForeColor = System.Drawing.Color.Red
        End If
        If cboDefect.SelectedValue = (0) Then
            lblDefect.ForeColor = System.Drawing.Color.Red
        End If
        If cboAction.SelectedValue = (0) Then
            lblAction.ForeColor = System.Drawing.Color.Red
        End If

        'MsgBox("The fields in red must be updated")
        Exit Sub


    End Sub

Christiaan Baes
Belgium

"My new site" - Me
 
Thnaks for that - the porblem with that is once it has gone through the if's, it displays the messagebox even if all of the fields are filled up. I want the mesgbox only to appear if one or more of the if's is true. Putting in a msgbox under inside each if would result in multiple msgboxes if all if's are hit...
 
Code:
Public Sub validateForm()
        dim fieldisred as integer = 0
        If Me.cboSupplier.SelectedValue = (0) Then
            lblSupplier.ForeColor = System.Drawing.Color.Red
            fieldisred += 1
        End If
        If cboMaterial.SelectedValue = (0) Then
            lblPartNo.ForeColor = System.Drawing.Color.Red
            fieldisred += 1
        End If
        If txtDefectDesc Is "" Then
            lblDefectDesc.ForeColor = System.Drawing.Color.Red
            fieldisred += 1
        End If
        If cboDefect.SelectedValue = (0) Then
            lblDefect.ForeColor = System.Drawing.Color.Red
            fieldisred += 1
        End If
        If cboAction.SelectedValue = (0) Then
            lblAction.ForeColor = System.Drawing.Color.Red
            fieldisred += 1
        End If
        if fieldisred >0 then
            MsgBox("The fields in red must be updated")
        endif
        Exit Sub


    End Sub

Christiaan Baes
Belgium

"My new site" - Me
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top