Wildcards in event procedure?

P

Phil Hood

Hi,

I'm trying to create some code that will check the name of
a control on a form. I want to include a wildcard in the
check. There are a set of controls on the form named:

1A, 2A, 3A, 4A, 5A

I select each of the controls in turn and then test as
follows:

If Me.Name = *A Then do something....

but it resulted in a message saying 'Compile error,
Expected: expression'

I thought the problem was because I hadn't enclosed *A in
quotes as follows (because the name of a control is a
string):

If Me.Name = "*A" Then do something....

This complied OK but when I run the code it does
not 'find' the control and trigger the 'then' statement.

Any ideas?

Phil.
 
D

Douglas J. Steele

In a query, you need to use Like instead of = in conjunction with wildcards.
However, in VBA, you can't use Like.

What you can do is check whether the last character of the name is an A:

If Right(Me.Name, 1) = "A" Then
 
J

John Spencer (MVP)

Pardon me, but you can use the LIKE comparison operator in VBA. You have to be
careful with it since it can be case specific or not depending on how you have
set the modules Option Compare statement.

For instance in the immediate window I can enter
?"A" Like "*" and have it return True

More likely the problem is that the OP is testing the name of the form vice the
name of the control.

Me.Name will return the name of the form (or report)

ControlReference.Name will return the name of the control

Since the OP did not share all the code, it is difficult to be exact on how he
needs to change the reference.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top