Solved

VBA Checkbox Field Validation

Posted on 2008-10-16
3
329 Views
Last Modified: 2013-11-28
I have a form with 3 text boxes fields, 1 combo box field and 8 check boxes fields. The text boxes, the combo box and 6 check boxes have a tag name "Req". After the last checkbox, the user press a button to save the record and close the form. At this point, I want to validate if all fields with the "Req" tag are filled or checked - if not, will prompt a message that there's a field that's missing.

I have this code running, but it only works for the text boxes and the combo box. What I'm missing?
Private Sub abtnCommand1_Click()

Dim ctl As Control
 
 

For Each ctl In Me.Controls

    If ctl.Tag = "Req" And IsNull(ctl) Then

    MsgBox "Required fields missing. Fill or check missing fields", vbCritical, "Missing fields"

    ctl.BackColor = vbYellow

    Else

    

    End If

    

    Next

    

    Exit Sub
 

End Sub

Open in new window

0
Comment
Question by:richieac77
  • 2
3 Comments
 
LVL 28

Expert Comment

by:omgang
Comment Utility
Checkbox controls are either True or False, Yes or No, On or Off, -1 or 0.  You need to test for one of those values.

OM Gang
0
 
LVL 28

Accepted Solution

by:
omgang earned 125 total points
Comment Utility
OM Gang
Private Sub abtnCommand1_Click()
 

Dim ctl As Control

 

 

For Each ctl In Me.Controls

    If ctl.Tag = "Req" Then

        If ctl.ControlType = acCheckBox Then

            If IsNull(ctl.Value) Or ctl.Value = 0 Then

                MsgBox "Required fields missing. Fill or check missing fields", vbCritical, "Missing fields"

                ctl.SetFocus

                Exit For

            End If

        Else

            If IsNull(ctl) Then

                MsgBox "Required fields missing. Fill or check missing fields", vbCritical, "Missing fields"

                ctl.BackColor = vbYellow

                Exit For

            End If

        End If

    End If

    

Next
 

Exit Sub

        
 
 

End Sub

Open in new window

0
 

Author Closing Comment

by:richieac77
Comment Utility
Thanks it worked like charm.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now