Checkbox to hide labels

  • Thread starter Thread starter Danno
  • Start date Start date
D

Danno

Guest
I have a form that I want to have several labels become not visible when I check a combination of two checkboxes. I haven't used VBA in Access before, so syntax for me is a problem.

I am using Access 2000 and here is the code as I have it:

Private Sub Check1_Click()
If ((Me![Check1].Checked = True) And (Me![Check2].Checked = True)) Then
MsgBox "OK", vbOKOnly
percentA.Visible = True
percentB.Visible = False
percentC.Visible = False
percentD.Visible = False
End If

If ((Me![Check1].Checked = False) And (Me![Check2].Checked = True)) Then
percentA.Visible = False
percentB.Visible = True
percentC.Visible = False
percentD.Visible = False
End If

If ((Me![Check1].Checked = True) And (Me![Check2].Checked = False)) Then
percentA.Visible = False
percentB.Visible = False
percentC.Visible = True
percentD.Visible = False
End If

If ((Me![Check1].Checked = False) And (Me![Check2].Checked = False)) Then
percentA.Visible = False
percentB.Visible = False
percentC.Visible = False
percentD.Visible = True
End If

End Sub

Private Sub Check2_Click()
If (Check1 = True) And (Check2 = True) Then
percentA.Visible = True
percentB.Visible = False
percentC.Visible = False
percentD.Visible = False
End If

If (Check1 = False) And (Check2 = True) Then
percentA.Visible = False
percentB.Visible = True
percentC.Visible = False
percentD.Visible = False
End If

If (Check1 = True) And (Check2 = False) Then
percentA.Visible = False
percentB.Visible = False
percentC.Visible = True
percentD.Visible = False
End If

If (Check1 = False) And (Check2 = False) Then
percentA.Visible = False
percentB.Visible = False
percentC.Visible = False
percentD.Visible = True
End If
End Sub

As you can see, I tried a different syntax for the first checkbox than the second. I get nothing but errors on my expression at this point...help!

Danno
 
Danno,

If me.check1 = -1 and me.check2 =-1 then
"do something"
end if

If me.Check1 = 0 And Me.Check2 = -1 Then
"do something"
end if

As you can see, instead of using true use -1 and instead of using false use 0. Also, I would put the statements in the After Update event of the check boxes instead of the click event.

Hope this helps.

George
 
Changing True/False to their numeric eqv. makes no difference they both work
If Me.MyChk1=True And Me.MyChk2=False etc
Put the code in the form Current_Event
on the After Update Event of both boxes put Form_Current
 
Another solution...

Hi

I've attached an alternative solution for you to use or ignore as you please!

There's any number of ways to code a problem but always try to avoid having to repeat code. If you need the same piece of code in more than one place, write a separate sub or function and call that instead.

Also as Rich said earlier,

IF Me.MyChk1 = True then ...

is the same as

IF Me.MyChk1 = -1 then ...

but

IF Me.MyChk1 then ...

works just as nicely.

Hope the code helps.

shay

=========================

Private Sub chk1_Click()

DisplayLabels

End Sub

Private Sub chk2_Click()

DisplayLabels

End Sub

Private Sub DisplayLabels()

If Not IsNull(Me.chk1) And Not IsNull(Me.chk2) Then

Me.lblPercentA.Visible = False
Me.lblPercentB.Visible = False
Me.lblPercentC.Visible = False
Me.lblPercentD.Visible = False

Me.lblPercentA.Visible = (Me.chk1 And Me.chk2)
Me.lblPercentB.Visible = (Not Me.chk1 And Me.chk2)
Me.lblPercentC.Visible = (Me.chk1 And Not Me.chk2)
Me.lblPercentD.Visible = (Not Me.chk1 And Not Me.chk2)

End If

End Sub
 
If Me.MyCheck Then
etc
can produce invalid use of Null errors, so you then have to add the Nz funtion into the code.
 
That Works! - Thanx

I have just one question. I have the two unchecked as the default and visible, but it takes two clicks to activate it. In other words, on click makes no change, but with the second click it starts to work...any suggestions?
 

Users who are viewing this thread

Back
Top Bottom