Solved

VBA Checkbox Field Validation

Posted on 2008-10-16
3
330 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
ID: 22731688
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
ID: 22732048
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
ID: 31506715
Thanks it worked like charm.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

895 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

11 Experts available now in Live!

Get 1:1 Help Now