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)
 
B

BruceM via AccessMonster.com

The problem may be that at the Load event Access doesn't yet know to which
record you want to add the value. You should be able to use the Current
event, but I have to say I wonder about using public variables. Are these
module-level (i.e. in the current code module only), or are they in a
standard module? Are they things such as the logged-on user's name? I tend
to avoid the use of public variables whenever possible, as there is some risk
of their losing their values, particularly if there is an unhandled error. I
generally use either function calls or references to values on hidden forms,
but others use custom properties and other means I do not fully understand.
There has been a lot of discussion about this subject. A groups search
should turn up plenty of hits. It would probably be worth doing some
investigation to make sure you can rely on the values being what they should
be.
 
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)
 
B

BruceM via AccessMonster.com

Open is before Load. I expect either would be fine for the unbound control,
but for the bound control Open and Load are too soon to assign a value to a
field in specific record in the form's recordset (other than by opening and
adding to a recordset programatically, but that is not what is being
described here).

Jack said:
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.
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
[quoted text clipped - 24 lines]
 
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