static variable

G

Guest

I want to use a single date obtained from the user on each record in a
current data entry session. The date is captured in a dialog box linked to a
one field table.
I have placed code in the data entry form's ON LOAD event to assign the
value to a static date varible and set the value of the form's date field on
the initial record. I then attempted to place code in the form's AFTER UP
DATE event to reset the form's date control for the next record, but the
static variable is not holding its value, so obviously I have misunderstood
its scope. Is there a good way to pass the value to each new record short of
redefining the value after each move to new record, i.e. by moving the code
in the form's On LOAD event to something that fires with each record update.

DM
 
M

Marshall Barton

Dick said:
I want to use a single date obtained from the user on each record in a
current data entry session. The date is captured in a dialog box linked to a
one field table.
I have placed code in the data entry form's ON LOAD event to assign the
value to a static date varible and set the value of the form's date field on
the initial record. I then attempted to place code in the form's AFTER UP
DATE event to reset the form's date control for the next record, but the
static variable is not holding its value, so obviously I have misunderstood
its scope. Is there a good way to pass the value to each new record short of
redefining the value after each move to new record, i.e. by moving the code
in the form's On LOAD event to something that fires with each record update.


I don't think I followed that. A Static variable is a
**local** (to a single procedure) variable that maintains
its value from one call to the procedure to the net call.

It sounds more like you want a module level variable
instead. (A module level variable is declared at the top of
the module, before any procedure declarations.

Your use of the form's AfterUpdate event is inappropriate.
The AfterUpdate event fires **after** the record is saved,
so when you change a field's value it causes the record to
be saved a second time. Use the form's BeforeUpdate event
instead.

If you only want to use the date value for new records, then
forget the module level variable and the BeforeUpdate event
and just set the field's text box's DafaultValue property in
the Open or Load event.
 
G

Guest

Your suggestion is perfect but the following code (in the form's open event)
did not work:

me![control name].defaultvalue = x

On the other hand, the value property as in
me![control name].value = x, did sucessfully set the controls value??

DM
 
M

Marshall Barton

Setting the Value property dirties the record, so it is the
wrong thing to do anyplace other than the **Form**
BeforeUpdate event. (Well, you can use a **control**
AfterUpdate event in response to a user action, but this is
not what you are trying to do.)

Since the DefaultValue property is a string, you have to
set it differently that you would the Value property. For a
Date type use this kind of statement:

Me.textbox.DafaultValue = Format(x, "\#m\/d\/yyyy\#")

For a Text type use this syntax:

Me.textbox.DafaultValue = """" & x & """"

Only Numeric types can be done using the syntax you used.
--
Marsh
MVP [MS Access]


Dick said:
Your suggestion is perfect but the following code (in the form's open event)
did not work:

me![control name].defaultvalue = x

On the other hand, the value property as in
me![control name].value = x, did sucessfully set the controls value??

Marshall Barton said:
I don't think I followed that. A Static variable is a
**local** (to a single procedure) variable that maintains
its value from one call to the procedure to the net call.

It sounds more like you want a module level variable
instead. (A module level variable is declared at the top of
the module, before any procedure declarations.

Your use of the form's AfterUpdate event is inappropriate.
The AfterUpdate event fires **after** the record is saved,
so when you change a field's value it causes the record to
be saved a second time. Use the form's BeforeUpdate event
instead.

If you only want to use the date value for new records, then
forget the module level variable and the BeforeUpdate event
and just set the field's text box's DafaultValue property in
the Open or Load event.
 
G

Guest

I thought it was a data type issue. Thanks for the explanation.

DM

Marshall Barton said:
Setting the Value property dirties the record, so it is the
wrong thing to do anyplace other than the **Form**
BeforeUpdate event. (Well, you can use a **control**
AfterUpdate event in response to a user action, but this is
not what you are trying to do.)

Since the DefaultValue property is a string, you have to
set it differently that you would the Value property. For a
Date type use this kind of statement:

Me.textbox.DafaultValue = Format(x, "\#m\/d\/yyyy\#")

For a Text type use this syntax:

Me.textbox.DafaultValue = """" & x & """"

Only Numeric types can be done using the syntax you used.
--
Marsh
MVP [MS Access]


Dick said:
Your suggestion is perfect but the following code (in the form's open event)
did not work:

me![control name].defaultvalue = x

On the other hand, the value property as in
me![control name].value = x, did sucessfully set the controls value??

Dick Minter wrote:
I want to use a single date obtained from the user on each record in a
current data entry session. The date is captured in a dialog box linked to a
one field table.
I have placed code in the data entry form's ON LOAD event to assign the
value to a static date varible and set the value of the form's date field on
the initial record. I then attempted to place code in the form's AFTER UP
DATE event to reset the form's date control for the next record, but the
static variable is not holding its value, so obviously I have misunderstood
its scope. Is there a good way to pass the value to each new record short of
redefining the value after each move to new record, i.e. by moving the code
in the form's On LOAD event to something that fires with each record update.
Marshall Barton said:
I don't think I followed that. A Static variable is a
**local** (to a single procedure) variable that maintains
its value from one call to the procedure to the net call.

It sounds more like you want a module level variable
instead. (A module level variable is declared at the top of
the module, before any procedure declarations.

Your use of the form's AfterUpdate event is inappropriate.
The AfterUpdate event fires **after** the record is saved,
so when you change a field's value it causes the record to
be saved a second time. Use the form's BeforeUpdate event
instead.

If you only want to use the date value for new records, then
forget the module level variable and the BeforeUpdate event
and just set the field's text box's DafaultValue property in
the Open or Load event.
 

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