Can't get "Me" to work

E

el zorro

I'm trying to optimize my VBA by refering to a form field
using "Me" and the field name instead of including the
path beginning with "Forms," but I can't seem to get it
to work. (The form is opened from another form, from
which the user has selected a doctor. The new form opens
to the appropriate doctor) Here's part of the code I have
in the click event that opens the new form:

stLinkCriteria = "[nDoc]=" & Me![nDoctor]

'Open form to selected doctor
DoCmd.OpenForm stDocName, , , stLinkCriteria
'Don't allow user to change name of facility
Forms!DoctorsF!DocFacility.Locked = True

THis works fine. But when I try to change that last line
to something like this:
Me!DocFacility.Locked = True, or Me.DocFacility.Locked =
True, it doesn't work. ANy idea where I'm going wrong?
THanks!
 
M

Mike

Yes, that's because you're focus is still on the calling form!
You'll have to use what you have or put the 'Me' command in the form itself.
 
D

Dirk Goldgar

el zorro said:
I'm trying to optimize my VBA by refering to a form field
using "Me" and the field name instead of including the
path beginning with "Forms," but I can't seem to get it
to work. (The form is opened from another form, from
which the user has selected a doctor. The new form opens
to the appropriate doctor) Here's part of the code I have
in the click event that opens the new form:

stLinkCriteria = "[nDoc]=" & Me![nDoctor]

'Open form to selected doctor
DoCmd.OpenForm stDocName, , , stLinkCriteria
'Don't allow user to change name of facility
Forms!DoctorsF!DocFacility.Locked = True

THis works fine. But when I try to change that last line
to something like this:
Me!DocFacility.Locked = True, or Me.DocFacility.Locked =
True, it doesn't work. ANy idea where I'm going wrong?
THanks!

Is "DoctorsF" the name of the form containing the code, or the name of
the form the code just opened? "Me" refers to the object containing the
executing code, so
Me!DocFacility.Locked = True

will only work if DocFacility is a control on the form where this line
of code is being executed. I can't say for sure, but it looks to me as
though DoctorsF is the name of the form you're opening, not the name of
the form where this code resides.
 
E

el zorro

Right-- DotorsF is the form being opened by the code.
THanks for pointing out why it won't work. (I was
thinking that once the form was opened, "Me" would refer
to that form, not the form with the code. Now I get it--
Me refers to the form with the code!)
-----Original Message-----
I'm trying to optimize my VBA by refering to a form field
using "Me" and the field name instead of including the
path beginning with "Forms," but I can't seem to get it
to work. (The form is opened from another form, from
which the user has selected a doctor. The new form opens
to the appropriate doctor) Here's part of the code I have
in the click event that opens the new form:

stLinkCriteria = "[nDoc]=" & Me![nDoctor]

'Open form to selected doctor
DoCmd.OpenForm stDocName, , , stLinkCriteria
'Don't allow user to change name of facility
Forms!DoctorsF!DocFacility.Locked = True

THis works fine. But when I try to change that last line
to something like this:
Me!DocFacility.Locked = True, or Me.DocFacility.Locked =
True, it doesn't work. ANy idea where I'm going wrong?
THanks!

Is "DoctorsF" the name of the form containing the code, or the name of
the form the code just opened? "Me" refers to the object containing the
executing code, so
Me!DocFacility.Locked = True

will only work if DocFacility is a control on the form where this line
of code is being executed. I can't say for sure, but it looks to me as
though DoctorsF is the name of the form you're opening, not the name of
the form where this code resides.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)


.
 

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