Solved

Getting rid of ' in strings

Posted on 2002-04-19
6
193 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
Comment Utility
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
Comment Utility
you can use Replace function
For Example
strString = "'Hinke'"
strString = Replace(strString, "'", "")
MsgBox strString
0
 
LVL 14

Expert Comment

by:ajexpert
Comment Utility
Cut and Paste above code in VB and see the results
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 18

Expert Comment

by:deighton
Comment Utility
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
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
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…

772 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

11 Experts available now in Live!

Get 1:1 Help Now