VB & MS SQL Server

Posted on 2002-03-05
Last Modified: 2010-05-02

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

Question by:chandua
  • 3
  • 2
LVL 70

Expert Comment

by:Éric Moreau
ID: 6843378
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,"'","''") & "')"
LVL 70

Expert Comment

by:Éric Moreau
ID: 6843388
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

Accepted Solution

lmanapure earned 15 total points
ID: 6843426

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 = ""
        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
sSQL = "Insert Into TableName(ColumnName)"
sSQL = sSQL & "Values(" & GetValidSQLString(str) & ")"

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why


Expert Comment

ID: 6861569
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.
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.

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

------------>  EXPERTS:  Please leave any comments regarding this question here on closing recommendations if this item remains inactive another three days.
Thank you everyone.
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.
LVL 70

Expert Comment

by:Éric Moreau
ID: 6861631
points are going to me. I have answered first.

Expert Comment

ID: 6866965
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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DIR issue 7 54
Access Object Property from VBA Module in Excel 2010 2 31
Set email body to html using vbscript 6 41
Formula problem with Excel attachment 6 27
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

770 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