?
Solved

VBA Checkbox Field Validation

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

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

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.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Suggested Courses

765 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