Skip to content

Adding a Record from a Form

The correct code to run when you want to add a record to a table with a value from a form.

Q:  Sarah Balthazor writes that she is getting an error message when trying to run the following code, intended to add a record to a table with a value from a form:

   Dim dbs As DAO.Database

   Dim rst As DAO.Recordset

   Dim intNoRecords As Integer

   Dim i As Integer

  

   Set dbs = CurrentDb

   Set rst = dbs.OpenRecordset(“Evaluations – Questions”)

   intNoRecords = Nz(Me![txtNoRecords].Value)

  

      For i = 1 To intNoRecords

        rst.[Question] = i  

        rst.AddNew

        rst.Update

      Next i

      rst.Close
 

A:  There are two problems with this code.  You need to use the bang (!), not the dot (.) for members of collections, such as fields in a recordset.  And the line setting the field value should be placed between the .AddNew and .Update lines:

      For i = 1 To intNoRecords

        rst.AddNew

        rst![Question] = i  

        rst.Update

      Next i

      rst.Close
 

About this author

Office 2024 - all you need to know. Facts & prices for the new Microsoft Office. Do you need it?

Microsoft Office upcoming support end date checklist.