Full Version: Insert Into query in VBA
UtterAccess Discussion Forums > Microsoft® Access > Access Queries
steflevesque
I would like to insert datas from my form. Here is the code I have made:


Dim EvaluationID As Integer
Dim SkillsEvaluatorID As Integer
Dim SkillsElementId As Integer
Dim LevelsId As Integer
Dim Comments As String
Dim addquery As String

EvaluationID = Me.txtEvaluationID
SkillsEvaluatorID = Me.txtEvaluatorsEval
SkillsElementId = Me.SkillsElementsID
LevelsId = Me.cboGradeEvaluators
Comments = Me.txtcomments

addquery = "INSERT INTO SkillsEvalbyEvaluators (SkillsEvaluatorID, EvaluationID, EvaluatorID,SkillsElementsID, EvaluatorGradeID, Comments ) _
VALUES (" & EvaluationEvaluatorID & "," & EvaluationID & "," & ViewerID & "," & SkillsElementId & "," & LevelsId & "," & Comments & ")"


I am always getting a compile error on my string (for my query)... I don't really the syntax !

When my query is created, how I execute it ?

Thanks a lot,

Stéphanie
strive4peace
you need to delimit any string or date field

CODE
Comments --> "'" & Comments & "'"
DateField --> "#" & DateField & "#"
steflevesque
Thanks for your quick response...

I have changed my code but it still doesn't work...
When I have run the command, it gives me :
Error syntax in insert into statement

I am not sure that I have understood properly what to do with my insert into statement.

here is my code:

addquery = "INSERT INTO SkillsEvalbyEvaluators (SkillsEvaluatorID, EvaluationID, EvaluatorID, SkillsElementsID, EvaluatorGradeID, Comments )" & _
"VALUES(" & SkillsEvaluatorID & "," & EvaluationID & "," & ViewerID & "," & SkillsElementId & "," & LevelsId & "," & " ' " & Comments & " ' " & ")"""

DoCmd.RunSQL addquery



Thanks!

Stéphanie
elmtree
A few things... Assuming you squarred away the Dim variables with the variables you are trying to INSERT. Your code differs from your first post to the last post...
Just make sure that any variable that is being inserted is Dim'd (defined) above.

Anyway, I'm not sure if it will make a difference, but I don't think it's a good idea to give your variables names that are identical to field names within your table.
You can place an 'int' in front of Integer variables and 'str' in front of string variables.
Dim intSkillsEvaluatorID, Dim strComments...

Sometimes spacing within your SQL can make a difference.
I added a space after the last paren ')' in the first line -just before the & _.
I know that this makes a difference on a WHERE clause- I think if there is no space, Access sees it as one long string- and can't make sense of what is supposed to happen.

You might try ending your INSERT statement with a semi-colon.

Also, It also looks like you have 2 extra double-quotes at the end. Not sure if this will have an impact, but I don't think they are needed.

"INSERT INTO SkillsEvalbyEvaluators (SkillsEvaluatorID, EvaluationID, EvaluatorID, SkillsElementsID, EvaluatorGradeID, Comments ) " & _
"VALUES(" & intSkillsEvaluatorID & "," & intEvaluationID & "," & intViewerID & "," & intSkillsElementId & "," & intLevelsId & "," & " ' " & strComments & " ' " & ");"


I hope this helps.
WildBird
Another thing to do is step throught the code (F8 button) and after it creates the strAddQuery (good idea to name variables like this) is to go to immediate window and type in
?strAddQuery
and press enter. Then copy this and open a new query and go to SQL view, and paste this into it. Will often highlight what column is wrong.
strive4peace
another good idea is to define a string variable to hold the SQL statement that you construct. Print it to the debug window before you use it. That way, if there are problems, you can open the debug window, copy the sql and make a new query, run it, and let Access tell you where the problem is

CODE
dim strSQL as string
'this is exactly what you had -- I did not change anything
strSQL =  "INSERT INTO SkillsEvalbyEvaluators (SkillsEvaluatorID, EvaluationID, EvaluatorID,SkillsElementsID, EvaluatorGradeID, Comments ) _
VALUES (" & EvaluationEvaluatorID & "," & EvaluationID & "," & ViewerID & "," & SkillsElementId & "," & LevelsId & "," & Comments & ")"
Debug.print s
addquery=s


if there are errors, press CTGL-G for the debug window
copy the SQL
from the database window: make a new query
View, SQL from the menu
paste
Run !
WildBird
The bottom lines above should read
Debug.print strSQL
addquery = strSQL
, not Debug.print s
addquery = s

Just in case there was some confusion
strive4peace
thanks for that correction, WildBird ...
WildBird
I cant see typos and bugs in my own code and documentation, but seem to pick it up on others!
steflevesque
Thanks everyone !!

I have followed your advices and it finally works !!

Have a nice afternoon!

Stéphanie
WildBird
You're welcome!
strive4peace
you're welcome wink.gif happy to help YOU figure it out !
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.