|
-
Jan 26th, 2009, 12:41 PM
#1
Thread Starter
Junior Member
Help with insert query using VB6
I can't get the following insert statement to work using VB6. As you can see from the code, I am trying to insert dummy text data into the "Data_Cert_Results" table. Any ideas....?
My error upon calling Execute is:
Unable to execute -
(-2147217900: The name "aaaaaaa" is not permitted in this contect. Valid expressions are constants, contant, expressions, and (in some contects) variables. Column names are not permitted.
Dim mobjADOConn As adodb.Connection
Dim dbrs As adodb.Recordset
Dim sqlstrInsert As String
Dim td As String
Dim ca As String
Dim pc As String
Set mobjADOConn = New adodb.Connection
mobjADOConn.ConnectionString = "DSN=aaa;Uid=bbb;Pwd=ccc;"
mobjADOConn.Open
' Create a Recordsets by executing an SQL statement.
Set dbrs = mobjADOConn.Execute(sqlstr)
' Loop through the recordset and show the fields.
If dbrs.RecordCount >= 1 Then
dbrs.MoveFirst
End If
Do While Not dbrs.EOF
' Show recordset fields
td = "AAAA"
ca = "BBBB"
pc = "CCCC"
sqlstrInsert = "INSERT INTO " & "Date_Cert_Results (Type_of_Data, Check_Area, Product_Code) values (" & td & "," & ca + "," & pc & ")"
mobjADOConn.Execute (sqlstrInsert)
-
Jan 26th, 2009, 12:43 PM
#2
Re: Help with insert query using VB6
When inserting strings (text) into a database they need to be enclosed by single qoutes
Code:
sqlstrInsert = "INSERT INTO " & "Date_Cert_Results (Type_of_Data, Check_Area, Product_Code) values ('" & td & "','" & ca + "','" & pc & "')"
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Jan 26th, 2009, 12:50 PM
#3
Thread Starter
Junior Member
Re: Help with insert query using VB6
Great, thanks for the info....!
-
Jan 26th, 2009, 01:38 PM
#4
Thread Starter
Junior Member
Re: Help with insert query using VB6
Still something appears to be wrong once I build on what you said. I am now getting unclosed quotation mark after the character string ".
I do not see it, do you?
sqlstrInsert = "INSERT INTO " & "Data_Cert_Results (Data_Cert_Results_Id, Rn_Descriptor, Rn_Create_Date, Rn_Create_User, Rn_Edit_Date, Rn_Edit_User) values ('" & resultid & "','" & rndesc & "','" & rncreatedate & "','" & rncreateuser & "','" & rneditdate & "','" & rnedituser & "')"
-
Jan 26th, 2009, 01:39 PM
#5
Thread Starter
Junior Member
Re: Help with insert query using VB6
-
Jan 26th, 2009, 01:47 PM
#6
Re: Help with insert query using VB6
It's not just String values that need to be delimited in a certain way in SQL statements, similar applies to Date and other data types. For more info, see the article How do I use values (numbers, strings, dates) in SQL statements? from our Database Development FAQs/Tutorials (at the top of the Database Development forum)
What does sqlstrInsert contain when you get the error? (you can use Debug.Print sqlstrInsert to copy it to the Immediate window, for easy copy & paste here).
-
Jan 26th, 2009, 01:58 PM
#7
Thread Starter
Junior Member
Re: Help with insert query using VB6
Below is what sqlstrInsert contains..... My ID and User fields should display a hex value. From the output below, they display ' ?'. Could this be the problem?
INSERT INTO Data_Cert_Results (Data_Cert_Results_Id, Rn_Descriptor, Rn_Create_Date, Rn_Create_User, Rn_Edit_Date, Rn_Edit_User) values (' A','1 - Titles (ex. JR SR II MD).
Given & Middle Name/Initial comINSERT INTO Data_Cert_Results (Data_Cert_Results_Id, Rn_Descriptor, Rn_Create_Date, Rn_Create_User, Rn_Edit_Date, Rn_Edit_User) values (' A','1 - Titles (ex. JR SR II MD).
Given & Middle Name/Initial compounded in Given N','1/19/2009 1:15:12 PM',' ?','1/19/2009 1:15:12 PM',' ?')'
pounded in Given N','1/19/2009 1:15:12 PM',' ?','1/19/2009 1:15:12 PM',' ?')'
-
Jan 26th, 2009, 02:05 PM
#8
Re: Help with insert query using VB6
You seem to have tested it twice, and left the cursor in the middle of the text - can you please try again. 
The only thing that pops out at me is that it seem like you have ' after the final bracket, which is not what you should have (and is not in the code you posted before).
-
Jan 26th, 2009, 02:24 PM
#9
Thread Starter
Junior Member
Re: Help with insert query using VB6
Hi, here is what sqlstrInsert is set to within VB.
sqlstrInsert = "INSERT INTO " & "Data_Cert_Results (Data_Cert_Results_Id, Rn_Descriptor, Rn_Create_Date, Rn_Create_User, Rn_Edit_Date, Rn_Edit_User) values ('" & resultid & "','" & rndesc & "','" & rncreatedate & "','" & rncreateuser & "','" & rneditdate & "','" & rnedituser & "')'"
Below is what is shown with the debug.print:
INSERT INTO Data_Cert_Results (Data_Cert_Results_Id, Rn_Descriptor, Rn_Create_Date, Rn_Create_User, Rn_Edit_Date, Rn_Edit_User) values (' A','1 - Titles (ex. JR SR II MD).
Given & Middle Name/Initial compounded in Given N','1/19/2009 1:15:12 PM',' ?','1/19/2009 1:15:12 PM',' ?')'
-
Jan 26th, 2009, 02:29 PM
#10
Re: Help with insert query using VB6
The problem is as I said:
And it is due to this, which you added since you last posted the code:
Code:
... & rnedituser & "')'"
I don't know why you have ? for a couple of the fields, you should check the variables that those values are coming from.
-
Jan 26th, 2009, 09:42 PM
#11
Hyperactive Member
Re: Help with insert query using VB6
its hard to identify errors with those concatenation of strings. try to use other code..
let say
vb Code:
with dbrs .open "Select * from TableName" .addnew !fieldname = Value .close end with
________
Mazda rotary pickup history
Last edited by rothj0hn; Feb 15th, 2011 at 01:38 PM.
-
Jan 26th, 2009, 09:42 PM
#12
Hyperactive Member
Re: Help with insert query using VB6
i forgot to include the .update
-
Jan 27th, 2009, 06:40 AM
#13
Lively Member
Re: Help with insert query using VB6
Code:
private subcmd_click()
set rs = new adodb.recordset
rs.open"select * from tablename",cn,1,2
rs.addnew
!fieldname = textname.text
rs.update
set rs = nothing
end sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|