Applies To Dynamic-Type Recordset object, Dynaset-Type Recordset object, Forward-Only–Type Recordset object, Recordset object, Snapshot-Type Recordset object, Table-Type Recordset object.
Description
Creates a new record for an updatable Recordset object.
Syntax recordset.AddNew The recordset placeholder is an object variable that represents an updatable Recordset object to which you want to add a new record. Remarks Use the AddNew method to create and add a new record in the Recordset object named by recordset. This method sets the fields to default values, and if no default values are specified, it sets the fields to Null (the default values specified for a table-type Recordset). After you modify the new record, use the Update method to save the changes and add the record to the Recordset. No changes occur in the database until you use the Update method.See Also Bookmark property, CancelUpdate method, Delete method, EditMode property, Index object, LastModified property, Move method, MoveFirst, MoveLast, MoveNext, MovePrevious methods, Seek method, Update method.
Specifics (Microsoft Access) When you use a bookmark in a Microsoft Access module, you must include an Option Compare Binary statement in the Declarations section of the module. A bookmark is a Variant array of Byte data, so the string comparison method for the module must be binary. If a bookmark is evaluated with a text-based string comparison method, such as the Option Compare Text statement or the default setting for the Option Compare Database statement, the current record may be set to an incorrect record. Example 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.Sub AddNewX()
Dim dbsNorthwind As Database
Dim rstEmployees As Recordset
Dim strFirstName As String
Dim strLastName As String
Set dbsNorthwind = OpenDatabase("Northwind.mdb")
Set rstEmployees = _
dbsNorthwind.OpenRecordset("Employees", dbOpenDynaset)
' Get data from the user.
strFirstName = Trim(InputBox("Enter first name:"))
strLastName = Trim(InputBox("Enter last name:"))
' Proceed only if the user actually entered something
' for both the first and last names.
If strFirstName <> "" and strLastName <> "" Then
' Call the function that adds the record.
AddName rstEmployees, strFirstName, strLastName
' Show the newly added data.
With rstEmployees
Debug.Print "New record: " & !FirstName & _
" " & !LastName
' Delete new record because this is a demonstration.
.Delete
End With
Else
Debug.Print _
"You must input a string for first and last name!"
End If
rstEmployees.Close
dbsNorthwind.Close
End Sub
Function AddName(rstTemp As Recordset,
strFirst As String, strLast As String)
' Adds a new record to a Recordset using the data passed
' by the calling procedure. The new record is then made
' the current record.
With rstTemp
.AddNew
!FirstName = strFirst
!LastName = strLast
.Update
.Bookmark = .LastModified
End With
End Function
Example (Microsoft Access)
The following example creates a new record in an Employees table and saves the changes:
Sub AddNewRecord(ByVal rst As Recordset, _
strLast As String, strFirst As String)
With rst
.AddNew ' Add new record.
!LastName = strLast ' Add data.
!FirstName = strFirst
.Update ' Save changes.
End With
End Sub