Runtime error "you can't assign a value to this object"

A

Amit

MS Access 2K, Windows XP
=====================
Hi,

I have a simple form with the following code in the forms's On Load event:
=====
Me.RecordCreatePerson = fOSUserName()
MsgBox "Current user is " & CurrentUser()
Me.CurrentUserName = CurrentUser()
=====
I also have hidden text controls on the form bound to the corresponding
fields (of type text) for capturing Windows/Network username and the Access
username in the table.

When I open the form, there are instances when it works fine (the value from
fOSUserName and CurrentUser are nicely stored in the table), and at some
other instances, it gives me the error at the first line:
"You can't assign a value to this object."

The only other code I have is in the form's On Open event -
"Docmd.Maximize", and of course, the module.

Is there any reason why this would work some time, and not other times? The
message says "run time error '-2147352567 (80020009)': You can't assign a
value to this object."

Should this code be placed in the OnCurrent event of the form instead? I
think On Open is too early to assign values.

Thanks.

-amit
 
N

Nikos Yannacopoulos

Amit,

I suspect you're right, the On Load is probably too early. Try the
Current event. While you're at it, you could make it dependent on
Me.NewRecord = True, so it doesn't execute as you browse through
existing records (I realize this is probably a data entry form, just
giving you - and other readers possibly - the general idea).

HTH,
Nikos
 
A

Amit

Hi Nikos,

Thanks for the response. Yes, I moved the code to OnCurrent (with If
me.newrecord..) and it is working now.

I think the issue could have been when I moved from one record to the other.
In that case, OnLoad would not trigger, because the form is already loaded. I
could be wrong about this.

Thanks.

-Amit
 
N

Nikos Yannacopoulos

No, you're not wrong, this is indeed the case. If you wanted this to
work on every record, then the Current event is the right one. The Load
event fires only once, right after the form is opened.

Nikos
 

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