Home

Creating a Record in the Microsoft Access Object Library

 

Description

To perform data entry in the Microsoft Access Object Library, can call the AddNew() method of the Recordset class. Here is an example:

Private Sub cmdDataEntry_Click()
    Dim curDatabase As Object
    Dim rstStudents As Object
    
    Set curDatabase = CurrentDb
    Set rstStudents = curDatabase.OpenRecordset("Students")
    
    rstStudents.AddNew

    Set rstStudents = Nothing
    Set curDatabase = Nothing
End Sub

To specify the new value of a column, assign it to the desired Value property of the Field object. Here is an example:

Private Sub cmdDataEntry_Click()
    Dim curDatabase As Object
    Dim rstStudents As Object
    
    Set curDatabase = CurrentDb
    Set rstStudents = curDatabase.OpenRecordset("Students")
    
    rstStudents.AddNew
    rstStudents("FirstName").Value = "Helene"
    
    Set rstStudents = Nothing
    Set curDatabase = Nothing
End Sub

After adding a new record, you must ask the record set to receive the new value. To support this, the Recordset class is equipped with a method named Update. Therefore, call this method after specifying a value for each column.

Examples

Here is an example that adds a record made of one value:

Private Sub cmdCreateRecord_Click()
    Dim curDatabase As Object
    Dim rstStudents As Object
    
    Set curDatabase = CurrentDb
    Set rstStudents = curDatabase.OpenRecordset("Students")
    
    rstStudents.AddNew
    rstStudents("FullName").Value = "Helene Mukoko"
    rstStudents.Update
    
    Set rstStudents = Nothing
    Set curDatabase = Nothing
End Sub
 
 

Here is example that adds a new record to a table, using two fields:

Private Sub cmdCreateTable_Click()
    Dim curDatabase As Object
    Dim tblStudents As Object
    Dim fldFirstName As Object, fldLastName As Object

    ' Get a reference to the current database
    Set curDatabase = CurrentDb
    ' Create a new table named Students
    Set tblStudents = curDatabase.CreateTableDef("Students")
    
    Set fldFirstName = tblStudents.CreateField("FirstName", dbText)
    tblStudents.Fields.Append fldFirstName
    
    Set fldLastName = tblStudents.CreateField("LastName", dbText)
    tblStudents.Fields.Append fldLastName
    
    ' Add the Students table to the current database
    curDatabase.TableDefs.Append tblStudents
End Sub

Private Sub cmdDataEntry_Click()
    Dim curDatabase As Object
    Dim rstStudents As Object
    
    Set curDatabase = CurrentDb
    Set rstStudents = curDatabase.OpenRecordset("Students")
    
    rstStudents.AddNew
    rstStudents("FirstName").Value = "Helene"
    rstStudents("LastName").Value = "Mukoko"
    rstStudents.Update
    
    Set rstStudents = Nothing
    Set curDatabase = Nothing
End Sub

Here is an example that adds a Boolean value:

Private Sub cmdCreateTable_Click()
    Dim curDatabase As Object
    Dim tblEmployees As Object
    Dim colFullName As Object
    Dim colIsMarried As Object

    Set curDatabase = CurrentDb
    Set tblEmployees = curDatabase.CreateTableDef("Employees")
    
    Set colFullName = tblEmployees.CreateField("FullName", dbText)
    tblEmployees.Fields.Append colFullName
    Set colIsMarried = tblEmployees.CreateField("IsMarried", dbBoolean)
    tblEmployees.Fields.Append colIsMarried
    
    ' Add the Students table to the current database
    curDatabase.TableDefs.Append tblEmployees
End Sub

Private Sub cmdCreateRecord_Click()
    Dim curDatabase As Object
    Dim rstStudents As Object
    
    Set curDatabase = CurrentDb
    Set rstStudents = curDatabase.OpenRecordset("Employees")
    
    rstStudents.AddNew
    rstStudents("FullName").Value = "Patricia Katts"
    rstStudents("IsMarried").Value = True
    rstStudents.Update
    
    Set rstStudents = Nothing
    Set curDatabase = Nothing
End Sub
 
 
   
 

Home Copyright © 2009-2016, FunctionX, Inc.