423,350 Members | 2,519 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,350 IT Pros & Developers. It's quick & easy.

Referencing Form fields

P: 1
Hi,

I am facing an unusual issue with my Access 2016 VBA code.

I have a Form with a field called txtUID. I'm using the Form_Load sub to initialize this field as follows:
Expand|Select|Wrap|Line Numbers
  1. Me.txtUID.Value  Format(Date, "dd/mm/yyyy")
In a separate module sub, when i use this...
Expand|Select|Wrap|Line Numbers
  1. tmpTimeStampUID = Me.txtUID.Value
, i get the error INVALID USE OF 'ME' KEYWORD.

If i delete the Me and keep
Expand|Select|Wrap|Line Numbers
  1. tmpTimeStampUID = txtUID.Value
, i get the error MISSING OBJECT

What am i doing wrong?? Please help.
2 Weeks Ago #1
Share this Question
Share on Google+
2 Replies


PhilOfWalton
Expert 100+
P: 1,297
The word "me" can only be used in a form or report's module, and obviously is a shorthand way of referring to that form or report.

In an external module, you must refer to the form or report by name
So
Expand|Select|Wrap|Line Numbers
  1. tmpTimeStampUID = Forms!MyFormName!txtUID.Value
where MyFormName is the name of your form.

I rarely find the word "Value" is needed, so try without it.

Phil
2 Weeks Ago #2

NeoPa
Expert Mod 15k+
P: 31,007
Hi mshriyan.

As Phil says, Me is a reference to the current Form/Report object and as such, can never be used unless within the linked module of a Form/Report. Obviously, whichever object's module it's used within is the object that Me refers to.

.Value is the default property for Controls so is not usually needed in such code. It can't hurt to be explicit but it's usually abbreviated.

A Control on a Form or Report is a Control and not a Field. Controls may be bound to Fields from the Form's RecordSource but they are by no means synonymous.

As Phil says, when a Form/Report is referenced outside of its own linked module then you need some other way of referencing that object. There are a million and one different ways but in most circumstances his example will work for you. The Form/Report would need to be open already mind you, but I would guess that's the case in your situation.
2 Weeks Ago #3

Post your reply

Sign in to post your reply or Sign up for a free account.