?
Solved

VBA Checkbox Field Validation

Posted on 2008-10-16
3
Medium Priority
?
345 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 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

809 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