Form Retain Previous Value

J

John Michl

I'd like to create a simple data entry form. Is there a way to set the
default value for a field to be equal to what ever was in that field on the
previous record?

Thanks - John
 
N

Nikos Yannacopoulos

John,

It's a piece of cake with a hidden unbound text box and a few lines of VB
code. Open your form in design view, and add at textbox, leaving its
ControlSource property empty (unbound control). I'll assume this textbox is
named Text20 (you can use whatever name you want), and the textbox whose
value you want repeated is named Text1.
Select the form (click on the little square button in the cross-sectio of
the two rulers) and display properties; go to tab Events, put the cursor in
the box for property On Current (first one) and click on the small button
with the three dots on the right; select Code Builder. You will be taken to
a VB editor screen, and the cursor will be in between two lines reading:

Private Sub Form_Current()

End Sub

Paste the following code in between those lines:

If Me.NewRecord Then
Me.Text1 = Me.Text20
Else
Me.Text20 = Me.Text1
End If

Likewise, select the field you want to default a value in (Text1), and add a
line of code to its Before Update event. The cursor will be between lines:

Private Sub Text1_BeforeUpdate(Cancel As Integer)

End Sub

The line of code to add is:

Me.Text20 = Me.Text1

Change the textbox control names to the actual ones on your form. Close the
VB editor window, save your form and you're done.

The On Current event fires when you open the form, and when you move to
another record, existing or new. What the code does is: (a) when you open
the form to an existing record, or go to an existing record, the value of
the field in that record is stored in the hidden text box, (b) when you go
to a new record, the last value stored in the hidden textbox is placed in
the target textbox. When you open the form directly to a new record (form is
data entry) or there is no recotrd in the table to display, the hidden
textbox will be empty (Null), and so no value will be puit in the target
one.

The code behind the Before Update event of the control fires when you make a
change to the target control in a record, and stores the new value in the
hidden textbox (without it you would still have the old one, as the form's
Current event fires - and stores - when you get to the record, before you
make any change).

HTH,
Nikos
 
G

Guest

This works GREAT!! Exactly what I want,,,, almost. When I open the form to
add a new record, I get a run-time error that states "You tried to assign the
nul value to a variable that is not a varient data type" Error number
-2147352567 (800200009). It works perfectly all I want to do is prevent this
message from coming up. Any suggestions?

Thanks a bunch!
Mike
 

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