Search code examples
ms-accessvba

Inserting values into a Microsoft Access database using a button on a form


Hi I am currently trying to added a few values inputted in form into my database using the provided line.

Private Sub btnInsert_Click()
    DoCmd.RunSQL "INSERT INTO Band (BandID, BandName, BandAddress, ContactName, PhoneNumber)"
End Sub

I am getting a 3431 runtime error. Where I am messing up here?


Solution

  • You don't need to include auto increment fields. But you to specify fields if you are not inserting every field value.

    INSERT INTO Band (fieldName1, fieldName2, fieldName3)
    VALUES (1, 'DEF', 'ABC')
    

    Alternative:

    sql ="INSERT INTO Band (fieldName1, fieldName2, fieldName3) " & _
    "VALUES (1, 'DEF', 'ABC') " & _
    "
    currentDb.execute sql