Article ID: 190515
Article Last Modified on 1/23/2007
Field Name Data Type Description
---------- --------- -----------
Variable Text The name of the variable.
Make it a primary key.
Value Text Holds the value to be returned.
Description Text What this variable is for.
Variable
--------
FieldSize = 30
Value
-----
FieldSize = 70
Description
-----------
FieldSize = 255
' The code below finds or creates a record in tblStorage where the
' field Variable contains "CustomerIDLast", and stores the current
' CustomerID in the field called Value.
Private Sub Form_Unload(Cancel As Integer)
' Declare database object and recordset object.
Dim db As Database, rst As Recordset
' If the current record has no customer ID, do nothing.
If IsNull(Me![CustomerID]) Then Exit Sub
Set db = CurrentDb
Set rst = db.OpenRecordset("tblStorage")
rst.Index = "PrimaryKey"
rst.Seek "=", "CustomerIDLast"
' If not found, create the entry.
If rst.NoMatch Then
rst.AddNew
rst![Variable] = "CustomerIDLast"
rst![Value] = Me![CustomerID]
rst![Description] = "ID of last edited customer record," _
& Me.Name & "."
rst.Update ' Update the recordset.
Else ' Else save the customer ID of the current record.
rst.Edit
rst![Value] = Me![CustomerID]
rst.Update ' Update the recordset.
End If
rst.Close ' Close the recordset.
End Sub
a. Locate the record in tblStorage where the Variable field contains
the "CustomerIDLast" string. If it is not there, do nothing.
b. If located, get the last stored CustomerID from the Value field.
c. Create a RecordsetClone of the records in the form.
d. Find the matching CustomerID in the clone set.
e. Move to the record by setting the form's BookMark.
To set this up, set the form's Load property to the following event
procedure:
Private Sub Form_Load()
' Declare database object and recordset objects.
Dim db As Database, rst As Recordset, rstFrm As Recordset
' Set the database object to the current database.
Set db = CurrentDb
' Open the table tblStorage.
Set rst = db.OpenRecordset("tblStorage")
' Set the index for the seek.
rst.Index = "PrimaryKey"
' Look for the CustomerIDLast record.
rst.Seek "=", "CustomerIDLast"
' If a match, get the customer ID and take the form to that
' record. If no match, do nothing.
If Not rst.NoMatch Then
If Not IsNull(rst![Value]) Then
' Create clone of the form's record set.
Set rstFrm = Me.RecordsetClone
' Find the matching record.
rstFrm.FindFirst "[CustomerID] = '" & rst![Value] & "'"
If Not rstFrm.NoMatch Then
' Have the form go to that matching record.
Me.Bookmark = rstFrm.Bookmark
End If
rstFrm.Close ' Close the recordset rstFrm.
End If
End If
rst.Close ' Close the recordset rst.
End Sub
163435 VBA: Programming Resources for Visual Basic for Applications
Additional query words: remember store global return
Keywords: kbhowto kbprogramming KB190515