Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VBA Checkbox Field Validation

Posted on 2008-10-16
3
Medium Priority
?
342 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

618 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