0

Here is what I have, I'm trying to take fields from an Access form (data comes from one linked sql table) and insert them into another linked sql table:

StrSQL = "INSERT INTO [dbo_Expense_Projection] ([StatusID],[TravellerUFID],[Email]) " & _
          VALUES(" & Me.StatusID & ", " Me.TravellerUFID & ", " Me.SubmitterUFID & ", " Me.email & ")
DoCmd.RunSQL StrSQL

But I am getting this error

Compile error: Sub or Function not defined 

3 Answers 3

3

I think you are just missing some double quotes:

StrSQL = "INSERT INTO [dbo_Expense_Projection] ([StatusID],[TravellerUFID],[Email]) " & _
"VALUES(" & Me.StatusID & ", " Me.TravellerUFID & ", " Me.SubmitterUFID & ", """ & Me.email & """)"
DoCmd.RunSQL StrSQL

You can try to print the contents of StrSQL and check the query before running it:

Debug.Print StrSQL

but I prefer not to create SQL strings with concatenated values (what happens if Me.StravellerUFID contains a double quote?)

I would suggest you to insert data using DAO:

Dim rs as Recordset
Set rs = CurrentDb.OpenRecordset("dbo_Expense_Projection")
rs.AddNew
rs!StatusID = Me.StatusID
rs!TravellerUFID = Me.TravellerUFID
' ...other fields
rs.Update
rs.Close
Sign up to request clarification or add additional context in comments.

1 Comment

There is a lot to be said for parameters. They scale better if you need to change the back-end.
0

There are also some ampersands missing in the SQL-String:

StrSQL = "INSERT INTO [dbo_Expense_Projection] ([StatusID],[TravellerUFID],[Email]) " & _
          "VALUES(" & Me!StatusID & ", " & Me!TravellerUFID & ", " & Me!SubmitterUFID & ", """ & Me!email & """)"

And I think you should use exclamation mark between "Me" and fieldname. But I do not want to argue with the experts here about that... ;)

Comments

0

Here is what ended up working:

Dim StrSQL As String

StrSQL = "INSERT INTO dbo_Expense_Projection (StatusID,TravellerUFID,Email) 
          VALUES('" & Form!StatusID & "','" & Form!TravellerUFID & "','" & Form!Email & "')"

DoCmd.SetWarnings False
DoCmd.RunSQL StrSQL
DoCmd.SetWarnings True

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.