Solved

VB & MS SQL Server

Posted on 2002-03-05
6
157 Views
Last Modified: 2010-05-02
Hi,

i am using VB as client.& SQL server as DB.
when i try to store this text "khdfkhsdkfh'sdfsdf'" in a varchar field i hit error "incorrect syntax near'sdfsdf'"

its bcos the single quote. but i need to store this value.
is it bcos the datatype is varchar???



0
Comment
Question by:chandua
  • 3
  • 2
6 Comments
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
no it is because it includes a quote. you need to do something like:
strV = "khdfkhsdkfh'sdfsdf'"
sql = "insert into t1(field1) values('" & replace(strv,"'","''") & "')"
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
and please maintain your other question:

Questions Asked 45
Last 10 Grades Given A A A A A B A A A B  
Question Grading Record 22 Answers Graded / 22 Answers Received
0
 
LVL 1

Accepted Solution

by:
lmanapure earned 15 total points
Comment Utility
Hi,

The problem is because of the ' character in your string.

It happens once in a while that the application users enter a string with a ' character.

You can use the following function in your applications to get a valid SQL string.

Private Function GetValidSQLString(ByVal SourceString As Variant) As String
    If IsNull(SourceString) Then
        GetValidSQLString = ""
    Else
        GetValidSQLString = "'" & Replace(SourceString, "'", "''", , , vbTextCompare) & "'"
    End If
End Function

A way to call this function may be:
str = "khdfkhsdkfh'sdfsdf'"
str = GetValidSQLString(str)
This will set str value equal to 'khdfkhsdkfh''sdfsdf'''
and the expression will become:

Insert Into Table(columnname) Values('khdfkhsdkfh''sdfsdf''')
Use it like this
Ex.
sSQL = "Insert Into TableName(ColumnName)"
sSQL = sSQL & "Values(" & GetValidSQLString(str) & ")"

Lokesh
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 1

Expert Comment

by:Moondancer
Comment Utility
47 questions asked, only 22 closed.... ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.
This question appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you. You must tell the participants why you wish to do this, and allow for Expert response.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question. Again, you must tell the other participants why you wish to do this.

For special handling needs, please post a zero point question in the link below, include the question QID/link.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this Help Desk link for Member Guidelines, Member Agreement and the Question/Answer process:  Click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues.
http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

To view your open questions, please click the following link(s) and update/finalize them all.

http://www.experts-exchange.com/jsp/qShow.jsp?ta=crystal&qid=20223690
http://www.experts-exchange.com/jsp/qShow.jsp?ta=crystal&qid=20265178
http://www.experts-exchange.com/jsp/qShow.jsp?ta=crystal&qid=20265195
http://www.experts-exchange.com/jsp/qShow.jsp?ta=crystal&qid=20274444
http://www.experts-exchange.com/jsp/qShow.jsp?ta=crystal&qid=20275198
http://www.experts-exchange.com/jsp/qShow.jsp?ta=crystal&qid=20180838
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mssql&qid=20133711
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mssql&qid=20267928
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mssql&qid=20272203
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mssql&qid=20268833
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20273717
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20271488
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20271453
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20266279
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20264617
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20258796
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20257478
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20255754
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20255748
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20242197
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20214627
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20193125
http://www.experts-exchange.com/jsp/qShow.jsp?ta=asp&qid=20181410
http://www.experts-exchange.com/jsp/qShow.jsp?ta=asp&qid=20148274
http://www.experts-exchange.com/jsp/qShow.jsp?ta=asp&qid=20008099

PLEASE DO NOT AWARD THE POINTS TO ME.  
 
------------>  EXPERTS:  Please leave any comments regarding this question here on closing recommendations if this item remains inactive another three days.
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange

P.S.  For year 2000 questions, special attention is needed to ensure the first correct response is awarded, since they are not in the comment date order, but rather in Member ID order.
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
points are going to me. I have answered first.
0
 
LVL 1

Expert Comment

by:Moondancer
Comment Utility
Points for emoreau  this TA 20277209

It was the Asker who chose and graded/closed this.  Fixed in the above, please comment.

Moondancer - EE Moderator
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

728 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

9 Experts available now in Live!

Get 1:1 Help Now