AddNew Method of ADO

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • salzan
    New Member
    • Feb 2008
    • 38

    AddNew Method of ADO

    Hello,
    I'm trying to insert a new record in my recordset using AddNew method. I can't figure out how to declare values for the controls in my recordset. Can someone give me an example.
    Thank you in advance for your help.
    Salzan
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Originally posted by salzan
    Hello,
    I'm trying to insert a new record in my recordset using AddNew method. I can't figure out how to declare values for the controls in my recordset. Can someone give me an example.
    Thank you in advance for your help.
    Salzan
    Here is a simple, generic Template you can use to Add Records to a Table via the AddNew Method, but first a couple of Assumptions:
    1. Table Name ==> Table1
    2. Fields in Table 1
      1. Field1 (TEXT)
      2. Field2 (LONG)
      3. Field3 (DATE)
    3. Methodology used: DAO

    [CODE=vb]
    Dim MyDB As DAO.Database, MyRS As DAO.Recordset

    Set MyDB = CurrentDb()
    Set MyRS = MyDB.OpenRecord set("Table1", dbOpenDynaset)

    With MyRS
    .AddNew
    ![Field1] = "Hello World!"
    ![Field2] = 876543
    ![Field3] = #12/12/1998#
    .Update
    End With

    MyRS.Close
    Set MyRS = Nothing[/CODE]

    Comment

    • Tetelestai
      New Member
      • Nov 2006
      • 34

      #3
      Or if not using the 'with' statement it would be

      MyRS![Field1]="text"

      Comment

      • salzan
        New Member
        • Feb 2008
        • 38

        #4
        Than you and if I'm not hardcoding the values I assume that I can reference the variable names containing the value, correct?

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #5
          Originally posted by salzan
          Than you and if I'm not hardcoding the values I assume that I can reference the variable names containing the value, correct?
          Yes, that is correct:
          [CODE=vb]
          Dim MyDB As DAO.Database, MyRS As DAO.Recordset

          Set MyDB = CurrentDb()
          Set MyRS = MyDB.OpenRecord set("Table1", dbOpenDynaset)

          With MyRS
          .AddNew
          ![Field1] = strStringVariab le
          ![Field2] = lngLongVariable
          ![Field3] = dteDateVariable
          .Update
          End With

          MyRS.Close
          Set MyRS = Nothing[/CODE]

          Comment

          Working...