Solved

SQL Statement fails

Posted on 2012-03-20
15
328 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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
 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
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…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

733 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