Home

Creating a Record in DAO

 

Description

To create a new record, call the AddNew() method of the Recordset class. Here is an example of calling it:

Private Sub cmdDataEntry_Click()
    Dim curDatabase As DAO.Database
    Dim rstEmployees As DAO.Recordset
    
    Set curDatabase = CurrentDb
    Set rstEmployees = curDatabase.OpenRecordset("Employees ")
    
    rstEmployees.AddNew

    Set rstEmployees = 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 DAO.Database
    Dim rstStudents As DAO.Recordset
    
    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. Here is example of calling this method:

Private Sub cmdCreateTable_Click()
    Dim curDatabase As DAO.Database
    Dim tblEmployees As DAO.Recordset
    Dim fldFirstName As DAO.Field, fldLastName As DAO.Field

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

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

 

 
 
   
 

Home Copyright © 2009-2016, FunctionX, Inc.