Cannot Assign Value to Bound Control

C

ctaleck

In Access 97, I can assign a bound control (to a linked table) a value
in the Form_Open event with no problem. I'm creating a new record when
I open the form.

Me.master_id.SetFocus
Me.master_id.value = intMaster

However, after converting the same database to Access 2003, I am
unable to do so anymore. I've resorted to using the DefaultValue
instead.

Any insight as to why?
 
K

Ken Sheridan

To assign a value to a bound control you should use the form's Load event
procedure (I don't recall what the situation was in Access 97, but from what
you say things appear to have changed). This is executed after the Open
event procedure.

However, setting the DefaultValue property is often more appropriate as it
only applies to a new record, whereas setting the Value property will
overwrite any existing value in the first record to be displayed when the
form loads. It also does not Dirty the form so if the form opens at a new
record, as in your case, a user can, should they decide to abandon the
operation, close the form on a new record without having to Undo the record
to avoid accidentally inserting a new row into the underlying table.

If you do use the DefaultValue property remember that this is always a
string expression regardless of the data type of the column to which the
control is bound. Consequently it should always be wrapped in quotes
characters, e.g.

Me.MyControl.Defaultvalue = """" & MyVariable & """"

Often it won't matter if the quotes are omitted, but sometimes they can be
crucial when you might not think so. Dates in short date format are an
example as these can otherwise be interpreted as an arithmetical expression,
e.g. 14/07/2008 (today in European short date format) as an arithmetical
expression evaluates to 0.00099601593625498 which as a date/time value is 30
December 1899 00:01:26 due to Access's implementation of date/time values as
a 64 bit floating point number with its origin at 30 December 1899 00:00:00.
BTW don't think that the # date delimiter character can be used instead of
quotes in this context. That would work for today's date as it is
internationally unambiguous, but would change 4 July to 7 April if used on a
system with a European short date format set via Windows Control Panel.

Ken Sheridan
Stafford, England
 

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