Can't Assign Value to this Object

P

Peter Marshall

I think I've seen this problem before but I can't remember the reason or
solution. I have a form (in add mode) whose record source is a single
table. For most fields on the form, the user will input data, but for a
couple of fields I am trying to populate them with the values of global
variables. I've got the global variables working (I can assign one to an
unbound text box on the form), but when I try to assign the variable value
to a bound text box, I get an error message that "Run-time error 2448: You
can't assign a value to this object". Here is my code:

Private Sub Form_Load()
If Me.OpenArgs = "Add" Then
DeclareDefaults 'This is a public sub that assigns values to
public variables
Me.txtDefLastName = defLastName 'assign global defLastName to the
unbound text box me.txtDefLastName. THIS WORKS.
Me.txtEmployeeLastName = defLastName 'TRY to assign global
defLastName to the bound text box Me.txtEmployeeLastName. ERRORS OUT.
End If
End Sub

Am I trying to do this too soon with the Form_Load. Where would be the
better event to tie this action to?
--

Peter Marshall
Manager Information Services
Ohio Coatings Company
740.859.5560 (w)
 
J

Jack Leach

In addition to Bruce's comments on Global variables (of which I fully agree),
you should use the Open even of the form rather than Load. As Load fires
before Open, there is much that is not available yet.

hth

--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)
 
J

Jack Leach

Wait wait wait... that order of events may not be correct... either way
though, I tend to use Open for this type of stuff rather than Load. You can
try, if not possibly you can isolate the first occurence of the Current event
as Bruce mentioned.

--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)
 
P

Peter Marshall

Gentlemen,

Thanks for the replies. I was on the right track in that the Load event
seemed to be too soon to populate a bound control on the form that is based
on a table. I also found that the Open event is too soon, whichever does
come first. I ended up using the form's Form_Activate event and that works
just fine.

As for the public variables, I am designing a time entry database for
multiple users for which the back-end will reside on a network drive with
all user records and the front-end will reside on each user's PC. There is
a tblUserDefaults table in each user's front-end with that particular user's
default fields such as FirstName, LastName, StartTime, EndTime, WorkCode.
As a user fires up the application, the Switchboard triggers code that loads
the default field values into Public variables one time only for that
session. As the user creates a new time record, those fields on the time
entry form are automatically populated but can be overridden by the user.
That was my first experience with Public variables, so I will take your
recommendations into account. Thanks again.

Peter Marshall
Manager Information Services
Ohio Coatings Company
 
M

Marshall Barton

Peter said:
I think I've seen this problem before but I can't remember the reason or
solution. I have a form (in add mode) whose record source is a single
table. For most fields on the form, the user will input data, but for a
couple of fields I am trying to populate them with the values of global
variables. I've got the global variables working (I can assign one to an
unbound text box on the form), but when I try to assign the variable value
to a bound text box, I get an error message that "Run-time error 2448: You
can't assign a value to this object". Here is my code:

Private Sub Form_Load()
If Me.OpenArgs = "Add" Then
DeclareDefaults 'This is a public sub that assigns values to
public variables
Me.txtDefLastName = defLastName 'assign global defLastName to the
unbound text box me.txtDefLastName. THIS WORKS.
Me.txtEmployeeLastName = defLastName 'TRY to assign global
defLastName to the bound text box Me.txtEmployeeLastName. ERRORS OUT.
End If
End Sub

Am I trying to do this too soon with the Form_Load. Where would be the
better event to tie this action to?


That's the wrong thing to do in either event. If you set a
bound control's Value before the user has a chance to do
anything, you will dirty the record. If the user decides
that's not what they wanted to do, either a mostly empty
record will be saved or the user will have to remember to
hit the Esc key until the record is clean before closing or
backing out of the form.

Most of the time, it is more appropriate to set the
control's DafaultValue property:

Me.txtDefLastName.DefaultValue = """" & defLastName & """"

That can be done in the form's Open event.

Note that the DefaultValue property is only applied to a new
record when the user actually starts to enter something.
 
P

Peter Marshall

Thanks, Marshall. That makes a lot of sense. I do have a Cancel button on
the screen (along with a Save button) that would probably avoid the dirty
scenario, but I like your advice.

--

Peter Marshall
Manager Information Services
Ohio Coatings Company
740.859.5560 (w)
 

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