IF Statement Against Check Boxes

CharlesWh

Registered User.
Local time
Today, 22:46
Joined
Jan 19, 2006
Messages
36
I'm trying to warn a user if no check boxes have been selected but this code doesnt seem to work. Am I wring somewhere?

Code:
If Me.chkCommercial = "0" And Me.chkDomestic = "0" And Me.chkHotSpot = "0" And Me.chkReseller = "0" And Me.chkVoip = "0" Then
MsgBox "You need to define enquiry interests"
Exit Sub
 
As check boxes hold a boolean value, true/false, yes/no, 0/-1 then you must use the same comparision.

If me.Checkbox1 = 0 OR me.checkbox2 = 0 then


Noteice the Or instead of AND. You code will only fire if all the check boxes have 0 values.

Now bear in mind that the user may have unchecked a checkbox to denote false. This menas that no they have not forgotten to click it so how are you going to know if the answer is Yes, No or have not clicked the control. You may have to incorperate triple state evaluation. Whereby do not provide a default value for each ckeckbox so it appears greyed out. Then compare against it being Null not True or False.
 
You can also use something like this too:
Code:
Dim ctl as controls

for each ctl in me.section(acdetail).controls
    if ctl.controltype = accheckbox then
         if nz(ctl.value, 0) = 0 then
               Cancel = true
              msgbox "You need to define enquiry interests", vbexclamation, "Define enquiry interests"
              exit sub
         end if
    end if
next
You would put the code in the BEFORE UPDATE event of the FORM.

If you want to use your version, you will still need to to put it in the event mentioned above:
Code:
If Me.chkCommercial = 0 Or Me.chkDomestic = 0 Or Me.chkHotSpot = 0 Or Me.chkReseller = 0 Or Me.chkVoip = 0 Then
    MsgBox "You need to define enquiry interests"
End if
 
vbINet:

Now I know why I did not read War and Peace. Why say something in ten lines that you can do in 3.
 
Just throwing out different options and also it allows for extensibility.
 
I know what you are saying but if it is a very busy form with lots of controls it can have an effect on the time to evaluate the response.
 
True, which is why I tried to boil it down to just the Details section. The OP may find it useful someday.

But for brevity (and not to confuse the OP), I would go with your 3 three lines in this case. There are cases where the looping might be necessary.
 

Users who are viewing this thread

Back
Top Bottom