Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

stupidest question ever (basic checkbox thing) 2

Status
Not open for further replies.

Jucius

Technical User
Aug 9, 2002
25
0
0
CA
I have created dynamic graphs based on a dynamic querydef, but I can't do checkboxes. It is so sad.

I am getting an odd error on this on a form:

Code:
If Me.checkbox1 = -1 Then 
     ' some code 
     ' more code
End If

The error is on the line with the comparison: "The expression you entered refers to an object that does not exist."

I did spell the checkbox1's name correctly, I have omitted the 'me.' in the front. I just want to detect in VBA whether or not it is checked!

(Help, please!]
 
Hey, the only stupid question is the one that isn't asked. More than likely a very subtle problem is causing this. Try typing:

If checkbox1.

to see if you are presented with a list of Properties for the checkbox. If not, then Access doesn't recognize the object. You might delete the checkbox and add it again. You might also check to make sure that the object is a checkbox instead of an option group. You might also make sure that you typed the code in the correct VB module. It has to be the module that matches the form that has the checkbox. dz
dzaccess@yahoo.com
 
You're using the wrong syntax.

To refer to controls, you must use the ! explanation symbol. To refer to properties, you use the . full stop symbol.

In your case, since you are refering to a control on the current form (me), your syntax should be:

If me!checkbox1 = -1 then

not

If me.checkbox1 = -1 then

so that the Access system knows it is dealing with a control on the current form, as opposed to a property or method of the form (which does'nt exist; ie. checkbox1 is not a valid property or method of the form).

Cheers,

Steve Lewy
Solutions Developer
steve@lewycomputing.com.au
 
Hi Steve,

I never use "Me" before control names, so I missed that. You got my curiosity up so I created a Form with a checkbox and tried the code both ways:

If Me!checkbox1 = True Then
MsgBox "Check box checked."
Else
MsgBox "Check box not checked."
End If


And


If Me.checkbox1 = True Then
MsgBox "Check box checked."
Else
MsgBox "Check box not checked."
End If


When I typed "Me.", the checkbox name showed up in the list along with the Properties. When I typed Me! nothing showed up in the list. However, I ran it both ways and got the exact same results. Here's what the Help says about use of the ! and . operators:

You use the ! and . (dot) operators in an identifier to indicate the type of item that immediately follows.

The ! operator indicates that what follows is a user-defined item (an element of a collection). For example, use the ! operator to refer to an open form, an open report, or a control on an open form or report.

Identifier Refers to
Forms![Orders] The open Orders form
Reports![Invoice] The open Invoice report
Forms![Orders]![OrderID] The OrderID control on the open Orders form


The . (dot) operator usually indicates that what follows is an item defined by Microsoft Access. For example, use the . (dot) operator to refer to a property of a form, report, or control.

Note You can also use the . (dot) operator to refer to a field value in an SQL statement, a Visual Basic for Applications method, or a collection. For example, the identifier Forms![Orders].Controls refers to the Controls collection of the Orders form. However, because the Controls collection is the default collection for forms and reports, it's usually not necessary to refer to it explicitly.


_____

This sounds like what you said, but it seems to work both ways. This is just one example of why Access is so confusing. Do you know why it worked both ways? Please note that the purpose of this post isn't to make you look bad. I am just trying to learn more about this language. Maybe it's time to go back to FoxPro! lol

Best regards,

dz
dzaccess@yahoo.com
 
Ah, it seemed that the problem was something different:

I had closed the form before doing the comparison, so the checkbox could not be read.

Silly me.

Thanks all, it works now.

FYI: I tried both the '.' and '!' syntax after I delayed the form's closing and both worked. I am going to stick with the '!' because it feels more syntactically correct. (Kind of like '.' vs '&(ref).' if you code C.) I agree with dz that this is one of the reasons why access is so confusing -- I definitely long to code in good ol' C again.
 
if you read the post carefully that was taken from access help, it said that a . (period) can be used to refer to a control name in vba, or sql... but good programing practice tells me that . would be used for a access defined property, and ! for a user defined object or property...

--James
junior1544@jmjpc.net
Life is change. To deny change is to deny life.
 
dz,

"This sounds like what you said, but it seems to work both ways. This is just one example of why Access is so confusing. Do you know why it worked both ways? Please note that the purpose of this post isn't to make you look bad. I am just trying to learn more about this language. Maybe it's time to go back to FoxPro! lol"

(a) It works both ways, because its (sometimes) forgiving, which I think is a mistake. ! should always be associated with a control to follow; full stop with a property or method. Period. If you always follow that convention, you can't go wrong, and more importantly, your code will be clearer, and other readers/reviewers will know that you know what you are doing.

(b) About the purpose of the post not to make me look bad. Well to use a mixed metaphor, in this world of dog eat dog, its really water off a ducks back. Seriously though, these forums are for discussion and learning, and far from taking your comments in the wrong light, your input is informative and useful; I know, cos I follow what you (and other regulars) write, and also notice your evolution and development with the language; keep at it mate.

Be well,


Cheers,

Steve Lewy
Solutions Developer
steve@lewycomputing.com.au
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top