@@ -18,7 +18,7 @@ ms.localizationpriority: medium
1818
1919** Applies to** : Access 2013, Office 2013
2020
21- Returns a bookmark indicating the most recently added or changed record.
21+ Returns a bookmark indicating the most recently added or modified record.
2222
2323## Syntax
2424
@@ -35,121 +35,125 @@ You can use the **LastModified** property to move to the most recently added or
3535This example uses the ** LastModified** property to move the current record pointer to both a record that has been modified and a newly created record.
3636
3737``` vb
38- Sub LastModifiedX()
39-
40- Dim dbsNorthwind As Database
41- Dim rstEmployees As Recordset
42- Dim strFirst As String
43- Dim strLast As String
44-
45- Set dbsNorthwind = OpenDatabase( "Northwind.mdb" )
46- Set rstEmployees = _
47- dbsNorthwind.OpenRecordset( "Employees" , _
48- dbOpenDynaset)
49-
50- With rstEmployees
51- ' Store current data.
52- strFirst = !FirstName
53- strLast = !LastName
54- ' Change data in current record.
55- .Edit
56- !FirstName = "Julie"
57- !LastName = "Warren"
58- .Update
59- ' Move current record pointer to the most recently
60- ' changed or added record.
61- .Bookmark = .LastModified
62- Debug.Print _
63- "Data in LastModified record after Edit: " & _
64- !FirstName & " " & !LastName
65-
66- ' Restore original data because this is a demonstration.
67- .Edit
68- !FirstName = strFirst
69- !LastName = strLast
70- .Update
71-
72- ' Add new record.
73- .AddNew
74- !FirstName = "Roger"
75- !LastName = "Harui"
76- .Update
77- ' Move current record pointer to the most recently
78- ' changed or added record.
79- .Bookmark = .LastModified
80- Debug.Print _
81- "Data in LastModified record after AddNew: " & _
82- !FirstName & " " & !LastName
83-
84- ' Delete new record because this is a demonstration.
85- .Delete
86- .Close
87- End With
88-
89- dbsNorthwind.Close
90-
91- End Sub
38+ Public Sub LastModifiedDemo()
39+
40+ Dim Northwind As DAO.Database
41+ Dim Employees As DAO.Recordset
42+ Dim CurrentFirstName As String
43+ Dim CurrentLastName As String
44+
45+ Set Northwind = OpenDatabase( "Northwind.mdb" )
46+ Set Employees = Northwind.OpenRecordset( "Employees" , dbOpenDynaset)
47+
48+ With Employees
49+ ' Store current data.
50+ CurrentFirstName = !FirstName
51+ CurrentLastName = !LastName
52+
53+ ' Modify the data in the current record.
54+ .Edit
55+ !FirstName = "Julie"
56+ !LastName = "Warren"
57+ .Update
58+ ' Move the current record pointer to the most recently
59+ ' modified or added record.
60+ .Bookmark = .LastModified
61+ Debug.Print _
62+ "Data in LastModified record after Edit: " & _
63+ !FirstName & " " & !LastName
64+
65+ ' Restore the original data because this is a demonstration.
66+ .Edit
67+ !FirstName = CurrentFirstName
68+ !LastName = CurrentLastName
69+ .Update
70+
71+ ' Add new record.
72+ .AddNew
73+ !FirstName = "Roger"
74+ !LastName = "Harui"
75+ .Update
76+ ' Move the current record pointer to the most recently
77+ ' modified or added record.
78+ .Bookmark = .LastModified
79+ Debug.Print _
80+ "Data in LastModified record after AddNew: " & _
81+ !FirstName & " " & !LastName
82+
83+ ' Delete the new record because this is a demonstration.
84+ .Delete
85+ .Close
86+ End With
87+ Set Employees = Nothing
88+
89+ Northwind.Close
90+ Set Northwind = Nothing
91+
92+ End Sub
9293```
9394
9495
95- This example uses the ** AddNew** method to create a new record with the specified name. The AddName function is required for this procedure to run.
96+ This example uses the ** AddNew** method to create a new record with the specified name. The AddName sub is required for this procedure to run.
9697
9798``` vb
98- Sub AddNewX()
99-
100- Dim dbsNorthwind As Database
101- Dim rstEmployees As Recordset
102- Dim strFirstName As String
103- Dim strLastName As String
104-
105- Set dbsNorthwind = OpenDatabase( "Northwind.mdb" )
106- Set rstEmployees = _
107- dbsNorthwind.OpenRecordset( "Employees" , dbOpenDynaset)
108-
109- ' Get data from the user.
110- strFirstName = Trim(InputBox( _
111- "Enter first name:" ))
112- strLastName = Trim(InputBox( _
113- "Enter last name:" ))
114-
115- ' Proceed only if the user actually entered something
116- ' for both the first and last names.
117- If strFirstName <> "" and strLastName <> "" Then
118-
119- ' Call the function that adds the record.
120- AddName rstEmployees, strFirstName, strLastName
121-
122- ' Show the newly added data.
123- With rstEmployees
124- Debug.Print "New record: " & !FirstName & _
125- " " & !LastName
126- ' Delete new record because this is a demonstration.
127- .Delete
128- End With
129-
130- Else
131- Debug.Print _
132- "You must input a string for first and last name!"
133- End If
134-
135- rstEmployees.Close
136- dbsNorthwind.Close
137-
138- End Sub
139-
140- Function AddName(rstTemp As Recordset, _
141- strFirst As String , strLast As String )
142-
143- ' Adds a new record to a Recordset using the data passed
144- ' by the calling procedure. The new record is then made
145- ' the current record.
146- With rstTemp
147- .AddNew
148- !FirstName = strFirst
149- !LastName = strLast
150- .Update
151- .Bookmark = .LastModified
152- End With
153-
154- End Function
99+ Public Sub AddNewDemo()
100+
101+ Dim Northwind As DAO.Database
102+ Dim Employees As DAO.Recordset
103+ Dim FirstName As String
104+ Dim LastName As String
105+
106+ Set Northwind = OpenDatabase( "Northwind.mdb" )
107+ Set Employees = Northwind.OpenRecordset( "Employees" , dbOpenDynaset)
108+
109+ ' Get data from the user.
110+ FirstName = Trim(InputBox( "Enter first name:" ))
111+ LastName = Trim(InputBox( "Enter last name:" ))
112+
113+ ' Proceed only if the user actually entered something
114+ ' for both the first and last names.
115+ If FirstName <> "" And LastName <> "" Then
116+ ' Call the sub that adds the record.
117+ AddName Employees, FirstName, LastName
118+
119+ ' Show the newly added data.
120+ With Employees
121+ Debug.Print _
122+ "New record: " & !FirstName & " " & !LastName
123+ ' Delete the new record because this is a demonstration.
124+ .Delete
125+ End With
126+ Else
127+ MsgBox _
128+ "You must input values for both first and last name." , _
129+ vbInformation + vbOKOnly, _
130+ "Add new name"
131+ End If
132+
133+ Employees.Close
134+ Set Employees = Nothing
135+
136+ Northwind.Close
137+ Set Northwind = Nothing
138+
139+ End Sub
140+
141+
142+ Public Sub AddName( _
143+ ByRef Records As DAO.Recordset, _
144+ ByVal FirstName As String , _
145+ ByVal LastName As String )
146+
147+ ' Adds a new record to a Recordset using the data
148+ ' passed by the calling procedure.
149+ ' The new record is then made the current record.
150+ With Records
151+ .AddNew
152+ !FirstName = FirstName
153+ !LastName = LastName
154+ .Update
155+ .Bookmark = .LastModified
156+ End With
157+
158+ End Function
155159```
0 commit comments