Solved

SQL Statement fails

Posted on 2012-03-20
15
325 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

947 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

18 Experts available now in Live!

Get 1:1 Help Now