PDA

View Full Version : Solved: CheckBox control- IF



Victor
03-16-2009, 10:43 AM
Hi all:

I am trying to run the code below to repeat the postal address to the home address if the CheckBox name "Check" is selected in a form.

But something is wrong with "Forms("Register").Controls("Check") .

Please let me know how to fix it

Thanks for the help.

Victor

Sub CheckBox()
If Forms("Register").Controls("Check") Then
DirHom1 = DirPos1
DirHom2 = DirPos2
StateHom = StatePos
ZipHom = ZipPos
End If
End Sub

CreganTur
03-16-2009, 11:07 AM
If this code is behind the form names Register, then you should be using the "Me" keyword.

Me refers to the current, active form. It supports intellisense, which would show all of the methods and properties available to the Form object, as well as all other objects on the form.

It would look like this:
Me.CheckBoxName
where CheckBoxName is the name of the actual checkbox object on your form you want to evaluate.

When testing a checkbox, you need to test for True(checked) and False(unchecked). Like this:
If Me.CheckBoxName = True Then
MsgBox "checked"
ElseIf Me.CheckBoxName = False Then
msgbox "unchecked"
End If

HTH:thumb

Victor
03-16-2009, 12:09 PM
Cregan:

I tried it, and as soon as I checked the box in the form it shows calculating but the msgbox do not show anything.

My code is behind the form "Register" and the name of the CheckBox is Check165.

It is suppose to work but not for me.

Any idea what I am missing.

Code below.

Thanks for the help.

Victor




Option Compare Database
Private Sub Check_Click()
If Me.Check165 = True Then
MsgBox "Checked"
ElseIf Me.Check165 = False Then
MsgBox "Unchecked"
End If
End Sub

Movian
03-16-2009, 01:17 PM
Option Compare Database
Option Explicit

Private Sub Check165_Click()
If Me.Check165.value = True Then
MsgBox "Checked"
Else
MsgBox "Unchecked"
End If
End Sub
Try that

The second if is superfluous, if the checkbox is not true then it can only really be false (unless your using triple state). so there for you can just say if its true do this, else do this. Finally the sub needs to be named the same as the control (if the control is called fish the sub will be "Private sub fish_Click()"

i find the easiest way to do this is to right click on the control, then select build event. Then from the pop up list select code. Most of the time the system will allready have created a private sub for you there. Althouh on some controls it will pick a diffrent event, such as beforeupdate. But for a checkbox it should create the click event

Also i like to use Option Explicit for each of my code sections. I believe its good practice.

Hope this helps

CreganTur
03-16-2009, 01:31 PM
The second if is superfluous, if the checkbox is not true then it can only really be false
THis is completely true- Victor: I added it as an explicit example, but also so you would know how to check for both.


i find the easiest way to do this is to right click on the control, then select build event. Then from the pop up list select code.
If you are always going to handle events with Code then click Tools->Options ->Forms/Reports -> Always use event Procedures

This will get rid of the popup box and always take you to VBIDE when dealing with procedures.

Option Explicit forces you to always Dimension your variables. This is not only good coding practice, it is also the smartest thing you can do. With this on, your code will not run if there are variables that aren't dimensioned. If you run procedures with undimensioned variables it can cause a lot of problems, mainly because VBIDE guesses at what the variable should be... and it can often be wrong.

To make Option Explicit permanent and show uip automatically, from the VBIDE you click Tools -> Options -> Require Variable Declaration.


Althouh on some controls it will pick a diffrent event, such as beforeupdate. But for a checkbox it should create the click event

You can also access all available events for an object from your properties window.

Movian
03-16-2009, 01:36 PM
If you are always going to handle events with Code then click Tools->Options ->Forms/Reports -> Always use event Procedures

Tools -> Options -> Require Variable Declaration.

How would someone go about doing this in access 2007 ?

~edit

NM i found the options :)


Also thank you for clarifying my very sloppy post :bow:. I am hoping i can get better at helping and formatting my help as time goes on.

CreganTur
03-17-2009, 04:57 AM
Also thank you for clarifying my very sloppy post :bow:. I am hoping i can get better at helping and formatting my help as time goes on.
I've just learned with time that it's best to be really explicit sometimes, especially when answering questions for new members, since I don't know their level of experience. I've insulted someone by doing this once- they thought I was talking down to them - but that's only happened once.

Victor
03-17-2009, 10:16 AM
Thanks to both, Cregan and Movian for the explanation, it was great and adding ".Value" did work for me.

Victor

OBP
03-17-2009, 10:47 AM
Personally I prefer to use the

If Me.Check165 = -1 Then

as it is shorter and does the same thing

Me.Check165 = 0 for an unticked check box