1

I created a Form in Access, and within this form there is a button which will allow the user to insert a record into a table ("Tbl")

The format of this table is: Report1_Field1 Report1_Field2 Report2_Field1 Report2_Field2 (and so on)

The Form will ask the user to: - Select the report name ("ReportName"); which could be "Report1" or "Report2" - Input a value for Field1 and Field2

The VBA code behind the button is as follows:

Private Sub ButtonUpdate
Dim NameReport as String
Dim FirstField as String
Dim SecondField as String

NameReport = ReportName
FirstField = ReportName & "_Field1"
SecondField = ReportName & "_Field2"

DoCmd.RunSQL "INSERT INTO tbl (FirstField, SecondField) VALUES (Field1, Field2)"

End Sub

I am however getting the Run-Time error 3127: The INSERT INTO statement contains the following unknown field name: 'FirstField'. Make sure you have typed the name correctly, and try the operations again.

Thoughts?

1 Answer 1

1

The SQL string is reading "FirstField" and "SecondField" as literal text instead of using your variables. Try this:

DoCmd.RunSQL "INSERT INTO tbl (" & FirstField & ", " & SecondField & ") VALUES (Field1, Field2)"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.