Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting default values to previous record

Status
Not open for further replies.

gaffonso

Programmer
Jul 26, 2001
25
US
What are the recommended techniques for setting a default field value to the last-entered value for that field? In other words, can I set the default value for a field (either the field default or the form-control default) to the field value of the previous record?

Thanks!

- Gary
 
In order to get a value from the prevois record you need to get that value before you create a new record.

So
have a button to creeate a new record (use the Wizard if you like)

then edit the VBA code and add these items

like so:
--------------------
Private Sub Command22_Click()
On Error GoTo Err_Command22_Click

Dim GetItem As Variant
GetItem = Me!yourfield
DoCmd.GoToRecord , , acNewRec
Me!yourfield = GetItem


Exit_Command22_Click:
Exit Sub

Err_Command22_Click:
MsgBox Err.Description
Resume Exit_Command22_Click

End Sub
------------------------------------- DougP, MCP

Visit my WEB site to see how Bar-codes can help you be more productive
 
I was able to do this without creating a button that gets the value first. Here's what I did:

* In the field that gets the default value set an on_enter event.

* The Visual Basic code for that event looks like this:

Private Sub TrainerID_Enter()
Dim objRS As DAO.Recordset

' if no value exists, default to the previously entered value
If TrainerID.Text = "" Then
Set objRS = Me.RecordsetClone
objRS.MoveLast

If Not objRS.EOF Then
TrainerID = objRS!TrainerID

' Now that the field has a value, select the text
' (makes for easy replacement if the default value isn't desired)
TrainerID.SelStart = 0
TrainerID.SelLength = Len(TrainerID.Text)
End If
End If
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top