Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA / SQL ADODB - update statement with percent % symbol

Status
Not open for further replies.

PinkeyNBrain

IS-IT--Management
Dec 12, 2006
279
US
Having trouble with a % symbol in a SQL update statement. The following is largely striped down, but illustrates the issue
Code:
  set rs = New ADODB.Recordset
  With rs
    .ActiveConnection = con_ref          ' assume con_ref set up in preceding code
    .CursorLocation = adUseClient
    .CursorType = adOpenDynamic
    .LockType = adLockOptimistic
    sql_cmd = "UPDATE tblCalc SET buffer_name='CUT ID TOL' WHERE Order = '100254-1-1'"
    .Open sql_cmd
  End With
Problem is that the actual command I want to send is:
Code:
  sql_cmd = "UPDATE tblCalc SET buffer_name='CUT % ID TOL' WHERE Order = '100254-1-1'"
This generates the error, "String or binary data would be truncated". The % symbol is coming from another source and I don't have the option to remove or substitute the symbol. I've tried these:
Code:
  UPDATE tblCalc SET buffer_name='CUT \% ID TOL' WHERE MillOrder = '100254-1-1'
' The above generates the same error message
UPDATE tblCalc SET buffer_name='CUT ~% ID TOL' WHERE MillOrder = '100254-1-1' ESCAPE '~'
' This one generates a syntax error near ESCAPE
UPDATE tblCalc SET buffer_name='CUT ~% ID TOL' WHERE MillOrder LIKE '100254-1-1' ESCAPE '~'
' This goes back to generating the original error message
Working with SQL Server 2005 and MS Excel 2010.
 
Forgot to include that I've tried this as well
Code:
UPDATE tblCalc SET buffer_name='CUT [%] ID TOL' WHERE MillOrder = '100254-1-1'
 
Firstly, the ESCAPE clause can only be used with LIKE and NOT LIKE, hence the syntax error.

Secondly, I don't believe that the problem is the %. Check the underlying datatype for the buffer_name column; can I suggest that you make it text?
 
You don't need a recordset to Update.

Have you tried:

Code:
Dim strSQL As String

strSQL = "Update tblCalc SET buffer_name ..."[blue]
con_ref.Execute strSQL[/blue]

Have fun.

---- Andy
 
Kinda figured that about the LIKE/ESCAPE combo. Any of the reference examples I found always paired ESCAPE w/ LIKE them but didn't notice an 'only if' rule between them.

strongm - thanks for making me feel like such a novice:) While the column is text, it's defined as nchar(10) - The intended string is length 12. Hence the error message was trying to tell me exactly what I needed to know. Set to nchar(20) and whatdaya know - it works.

Andrzejek - thanks as well for the tip on con_ref.Execute. The strSQL command is built in another procedure which passes it into a canned routine to execute it. I suspect I can upgrade the canned routine to seperate off these types of commands and likely run a little faster if it isn't building the Recordset contruct.
 
PinkeyNBrain,
I use recordsets for SELECT statements only.
For UPDATE, INSERT, or DELETE, I use:
[tt]
strSQL = "UPDATE...." (Insert, Delete)
MyConnection.Execute strSQL[/tt]

Have fun.

---- Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top