Ben McCarty
Ben McCarty

Reputation: 3

Add record if it doesn't exist

I am trying to write a condition check that checks for the record to exist: if it does then it opens a form whereas if it doesn't it first adds a record and then opens the form.

I got the condition check to work but I cannot figure out how to add a record to the table.

The table has only two columns (ID, ID to Link). ID is an auto-number and is the primary key. ID to Link is only for linking budget information to the appropriate account.

The code results in a Runtime Error (3134, syntax error in INSERT INTO statement).

Private Sub Test_Budget_Click()

  If IsNull(DLookup("[ID to Link]", "TEST_Budget Information", "[ID to Link] = " & _
    [Forms]![Award Information]![Internal ID to Link] & "")) Then

      DoCmd.RunSQL "INSERT INTO TEST_Budget Information([ID to Link])" & _
        "VALUES ('" & [Internal ID to Link] & "')"
      MsgBox "Record does not exist"
  Else
      MsgBox "Record Exists"
  End If

End Sub

Upvotes: 0

Views: 385

Answers (1)

Oggy
Oggy

Reputation: 1666

You should wrap TEST_Budget Information in square brackets and you are missing a couple of spaces. Try this:

DoCmd.RunSQL "INSERT INTO [TEST_Budget Information] ([ID to Link])" & _
                " VALUES ('" & [Internal ID to Link] & "')"

I would recommend that in the future you avoid having spaces in your table and column names, as they can cause trouble.

Upvotes: 2

Related Questions