Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 please help

Author  Topic 

sql167
Starting Member

10 Posts

Posted - 2009-04-29 : 21:59:10
I have a VB6 form with text boxes, datagrid and command buttons. I have the following code in one of the command button which is to save data to sql table.

testcn.Execute ("insert into ENTRYNUMBER (CUSTNUM, COMPNAME, CUR, SHIPTOLOCAT, SHIPVIA, DOCTYPE, DOCNUM, DOCDATE, YEARPERIOD, PONUM, SPECINTRUT,ORDNUM) VALUES (" & txtCustomerNumber.Text & " , " & txtCompanyName.Text & ", " & txtCurrency.Text & ", " & txtShipToLocation1.Text & ", " & txtShipVia1.Text & ", " & txtDocumentType.Text & ", " & txtDocumentNumber.Text & ", " & txtDocumentDate.Text & ", " & txtYearPeriod.Text & ", " & txtPONumber.Text & ", " & txtSpecialInstruction.Text & ", " & txtOrderNumber.Text & ")")

I am getting the following error message when I run the query in sql2005;

Msg 128, Level 15, State 1, Line 1
The name " & txtCustomerNumber.Text & " is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.
)
'.

txtCustomerNumber.Text is a text box in my form. what's wrong?

whitefang
Enterprise-Level Plonker Who's Not Wrong

272 Posts

Posted - 2009-04-30 : 08:08:10
All non-numerical datatypes need to be enclosed in single quotations.
So it should be: '" & txtCustomerNumber.Text & "' IF CUSTNUM column is non-numerical datatype (like varchar etc). Same goes for the other columns.


Also, this is bad technique. Instead of concatenating sql strings with values, use command parameters.
Go to Top of Page

sql167
Starting Member

10 Posts

Posted - 2009-05-01 : 05:11:39
Thanks for your reply. I am new to programming, so I don't know much about vb6 and SQL. I will try it on Monday, could you show me an example of command parameters? Thanks alot.
Go to Top of Page

sql167
Starting Member

10 Posts

Posted - 2009-05-04 : 02:58:33
It was the single quote that I was missing, thanks whitefang. Before I post this question, I did try placing the single quote but I placed it after the double quote that's why it didn't work out.:(
Go to Top of Page
   

- Advertisement -