UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Insert Into query in VBA    
 
   
steflevesque
post Jan 4 2005, 02:42 PM
Post #1

New Member
Posts: 18



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
Go to the top of the page
 
+
strive4peace
post Jan 4 2005, 02:50 PM
Post #2

UtterAccess VIP
Posts: 20,187
From: Colorado



you need to delimit any string or date field

CODE
Comments --> "'" & Comments & "'"
DateField --> "#" & DateField & "#"
Go to the top of the page
 
+
steflevesque
post Jan 4 2005, 03:51 PM
Post #3

New Member
Posts: 18



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
Go to the top of the page
 
+
elmtree
post Jan 4 2005, 05:58 PM
Post #4

UtterAccess Addict
Posts: 196
From: Seattle, WA



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.
Go to the top of the page
 
+
WildBird
post Jan 4 2005, 06:11 PM
Post #5

UtterAccess VIP
Posts: 3,050
From: Perth, Australia



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.
Go to the top of the page
 
+
strive4peace
post Jan 5 2005, 12:02 AM
Post #6

UtterAccess VIP
Posts: 20,187
From: Colorado



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 !
Go to the top of the page
 
+
WildBird
post Jan 5 2005, 10:37 AM
Post #7

UtterAccess VIP
Posts: 3,050
From: Perth, Australia



The bottom lines above should read
Debug.print strSQL
addquery = strSQL
, not Debug.print s
addquery = s

Just in case there was some confusion
Go to the top of the page
 
+
strive4peace
post Jan 5 2005, 12:50 PM
Post #8

UtterAccess VIP
Posts: 20,187
From: Colorado



thanks for that correction, WildBird ...
Go to the top of the page
 
+
WildBird
post Jan 5 2005, 01:05 PM
Post #9

UtterAccess VIP
Posts: 3,050
From: Perth, Australia



I cant see typos and bugs in my own code and documentation, but seem to pick it up on others!
Go to the top of the page
 
+
steflevesque
post Jan 5 2005, 01:20 PM
Post #10

New Member
Posts: 18



Thanks everyone !!

I have followed your advices and it finally works !!

Have a nice afternoon!

Stéphanie
Go to the top of the page
 
+
WildBird
post Jan 5 2005, 01:26 PM
Post #11

UtterAccess VIP
Posts: 3,050
From: Perth, Australia



You're welcome!
Go to the top of the page
 
+
strive4peace
post Jan 5 2005, 02:32 PM
Post #12

UtterAccess VIP
Posts: 20,187
From: Colorado



you're welcome (IMG:http://www.utteraccess.com/forum/style_emoticons/default/wink.gif) happy to help YOU figure it out !
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 21st May 2013 - 07:36 PM

Tag cloud: