Solved

Getting rid of ' in strings

Posted on 2002-04-19
6
194 Views
Last Modified: 2010-05-02
Hi everyone,

I have a variabel called strName and sometimes there will be an apostrofe (') in this string.
But don't want to save this '. Is there a way to get rid of these '.
So what I am looking for is a function.

Thanks
Hinke
0
Comment
Question by:hinkeltje
6 Comments
 
LVL 3

Accepted Solution

by:
Gunsen earned 300 total points
ID: 6953120
Replace() function in VB6

or

Sub ReplaceStr(St As String, ByVal SubSt As String, ByVal NewSt As String)
'  Replaces every occurence of SubSt in St with NewSt

  Dim I As Integer

  Do
    I = InStr(1, St, SubSt, 1)
    If I > 0 Then
      St = Left$(St, I - 1) + NewSt + Mid$(St, I + Len(SubSt))
    End If
  Loop Until I = 0
End Sub
0
 
LVL 14

Expert Comment

by:ajexpert
ID: 6953123
you can use Replace function
For Example
strString = "'Hinke'"
strString = Replace(strString, "'", "")
MsgBox strString
0
 
LVL 14

Expert Comment

by:ajexpert
ID: 6953129
Cut and Paste above code in VB and see the results
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 18

Expert Comment

by:deighton
ID: 6953132
why don't you want to save it?  If they are causing sql statements to fail, then you should double up the apostrophe in the sql string.

e.g

select * from tablew where name = 'O''Brien'
0
 
LVL 18

Expert Comment

by:mdougan
ID: 6953496
Another option, if you are saving these into a database and that is why you are trying to get rid of them, is to use the ado command object with ado parameter objects.  If you put the string into the value property of a parameter object and append it to a command object, then when you run the insert/update query through the command object, it will handle the single quote perfectly.  Actually, it handles all "problem" characters perfectly, you don't have to worry about wrapping strings in quotes or not wrapping numbers in quotes.  You don't have to worry about date formats or putting # signs around dates or anything like that.  It's all I use anymore.....

By the way, if you are getting rid of the ' in names like O'Brian, you're probably going to piss off a lot of people, if you ever decide to mailmerge out a letter with their name on it like OBrian.
0
 
LVL 5

Expert Comment

by:rkot2000
ID: 6953526
I am converting ' and " to ` and ` works fine with sql statements.

Public Function FilterString(ByVal lvsIn As String, _
                             ByVal lvlConversion As geConversion) As String
  lvsIn = Replace(lvsIn, Chr(44), " ")
  lvsIn = Replace(lvsIn, Chr(39), Chr(96))
  lvsIn = Trim$(Replace(lvsIn, Chr(34), Chr(96)))
 
  Select Case lvlConversion
    Case geConversion.awpProper
      FilterString = StrConv(lvsIn, vbProperCase)
    Case geConversion.awpLower
      FilterString = StrConv(lvsIn, vbLowerCase)
    Case geConversion.awpUpper
      FilterString = StrConv(lvsIn, vbUpperCase)
    Case geConversion.awpNone
      FilterString = lvsIn
  End Select
End Function
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

864 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

21 Experts available now in Live!

Get 1:1 Help Now