Home > Software design >  Improve INSERT-Access-VBA in accdb file
Improve INSERT-Access-VBA in accdb file

Time:10-17

I have the following code in my Access VBA program but it is giving me an error when I run the code. I created table with Table1 of name and I wanted to insert a new record into the table. But the code had a error. The Error is like this.

Object variable or With block Variable not set

Sub Insert()
    Dim db As Database
    Dim StrSQL As String
    StrSQL = "INSERT INTO Table1 VALUES ('1', 'Fuji')"
    db.Execute (StrSQL)
    db.Close
End Sub

CodePudding user response:

The simplest method is to take advantage of CurrentDb:

Sub Insert()

    Dim StrSQL As String

    StrSQL = "INSERT INTO Table1 VALUES ('1', 'Fuji')"
    CurrentDb.Execute StrSQL

End Sub

And if the first field is numeric, no quotes:

    StrSQL = "INSERT INTO Table1 VALUES (1, 'Fuji')"
  • Related