Solved

SQL Statement fails

Posted on 2012-03-20
15
323 Views
Last Modified: 2012-03-26
This is Access 2007 with a SQL Server 2005 back end. Within my application I have a routine called ExecuteSQL() which is used constantly to write strings of data to the SQL Server. It uses the .Execute method of an ADODB.command object  I have noticed today that this routine is failing on a statement which has got CR/LF pairs in the data being written.

However, if I paste the exact same SQL Statement into a Query window in SQL Management Studio, it executes without a problem.

If I then remove the CR/LF pairs, the  statement will execute within MsAccess.

My original statement looks like this:

Insert into RevisionDetails (RevisionRef, FieldName, OldValue,NewValue) Values(22981, 'Payment Details', '', 'Settlement of this invoice preferably using SWIFT
 should be made by paying our Bankers:
The Royal Bank of Scotland plc
International Payment London Operations
P.O. Box 34842 Islington High Street,
 LONDON N1  8XLSwift Address:
 RBOS GB 2LAccount  ')
0
Comment
Question by:TownTalk
  • 6
  • 3
  • 2
  • +3
15 Comments
 
LVL 25

Expert Comment

by:SStory
ID: 37742095
Can't you use an actual command object and do a parameter query--you know declare a command object, add parameter objects, set the text to a SQL string with parameter holders in it? It should clean up any data that isn't good and might just handle this for you.
0
 

Author Comment

by:TownTalk
ID: 37742139
@SSTory: My ExecuteSQL() routine is a general purpose routine which is used for writing to any combination of fields in any table. Surely with your approach I would have to write a different flavour of it for each usage?

My application has almost 100 tables in SQL Server. For several years I have had no problem with this routine.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 37742384
Please post your ADO code and the error message generated.
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 37742480
Does the Word Wrap *have* to be done in the SQL?

If it were me, I would create the main string, then create the "Wrapped" text segment.
Then concatenate them together at some later stage...


Juts a suggestion....

JeffCoachman
0
 

Author Comment

by:TownTalk
ID: 37742504
@acperkins: I have a routine called OpenDatabaseConnection which opens the ADO connection and creates the ADO command object.

Public Function ExecuteSQL(Criteria As String) As Boolean
           
    OpenDatabaseConnection
           
    DatabaseCommand.CommandText = Criteria
    DatabaseCommand.Execute
   
    ExecuteSQL=true
End Funtion

Like I said, i've been using this for years. Never had a problem like this before.
The error code is -2147217900
I could understand if we were talking about embedded quotes, but I would have thought that CRLF characters would be ok.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 37742579
but I would have thought that CRLF characters would be ok.
They are OK.  Provided they are embedded correctly.
0
 

Author Comment

by:TownTalk
ID: 37742596
Embedded correctly? what does that mean? i've never given them any special treatment previously. My users type the data into a Text Box in MS Access and then it gets saved.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 37742611
Fair enough.  Not sure how I can help then.
0
 
LVL 18

Accepted Solution

by:
UnifiedIS earned 500 total points
ID: 37744041
Can you do a replace function for your cr/lf?  replace it with a different reference to a new line like the char code or maybe a constant (VB has vbNewLine).
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 37744117
Again,


Does the Line feed *Have * to be done in SQL?
0
 
LVL 12

Expert Comment

by:kselvia
ID: 37745095
What is the width and datatype of your NewValues column? Your inserted text is 257 characters. If your column is 255 characters, the trailing 2 blank spaces may get trimmed in Mgt Studio but not in the ADO connection.
0
 

Author Comment

by:TownTalk
ID: 37746460
@kselva: In this case the field in the table is a varchar(2500)

@boag2000: I'm just trying to write into the database the characters that the User typed on the screen. But I take your point. I already have a routine to watch out for quote characters and double them up. I could modify that to swap CRLF characters for something else.

@UnifiedIS: I took a look  at vbnewline. In MsAccess it is a string constant containing ascii characters 13 and 10. So it is identical to CRLF
0
 
LVL 18

Expert Comment

by:UnifiedIS
ID: 37747155
On one that fails, can you loop through and identify every character that is there.  If a user is pasting a value, it may be bringing in something undesired that has no effect on the value.
0
 

Author Comment

by:TownTalk
ID: 37747883
hmmm.... this is getting stranger. I wrote a quck routine to go through all the records in my PaymentDetails table and make sure there were no characters there that I was unaware of. There weren't any.

Then from within that same routine, I called ExecuteSql() in exactly the same manner that caused the original issue, and I was surprised to find that the data including CRLFs was written to the table without a problem. I went back to the part of the code where the original problem was, and it still fails when trying to write exactly the same data.

Obviously something is different, but I can't see what yet. When I google the error code, most of the articles I am seeing are talking about illegal characters being to blame.
0
 

Author Closing Comment

by:TownTalk
ID: 37764881
I replaced CRLF pairs with a tilde (~) and all is well now. Thanks for your help
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now