Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

replace space character with underscore

Question posted by: n8kindt (Familiar Sight) on July 4th, 2008 03:21 AM
hey guys, i have a function that can recognize a space character in a string... how can i get it to replace a space with a underscore character

Code: ( text )
  1. Public Function RemoveSpaces(strInput As String)
  2. ' Identifies spaces in a string of text
  3.    If InStr(txtEmail, " ") = 0 Then
  4.       msgbox "Spaces are not allowed)
  5.    Else: Exit Function
  6. End Function
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
Stewart Ross Inverness's Avatar
Stewart Ross Inverness
Forum Leader
962 Posts
July 4th, 2008
06:23 AM
#2

Re: replace space character with underscore
Hi Nate. The Replace function does this in one call. It is used like this:
Code: ( text )
  1. somestring = Replace (strInput, strFind, strReplace)

In your function you are mistakenly referring to an undeclared variable txtEmail in place of strInput. I correct this below, and change the name of the function slightly to reflect what it does.
Code: ( text )
  1. Public Function ReplaceSpaces(strInput As String) as String
  2. ' Replaces spaces in a string of text with underscores
  3. Dim Result as String
  4. Result = strInput
  5. If InStr(strInput, " ") = 0 Then
  6.     Result = Replace(strInput, " ", "_")
  7.     msgbox "Spaces have been replaced with underscores.")
  8. Endif
  9. ReplaceSpaces = Result
  10. End Function


-Stewart

Reply
n8kindt's Avatar
n8kindt
Familiar Sight
175 Posts
July 9th, 2008
06:35 PM
#3

Re: replace space character with underscore
Quote:
Originally Posted by Stewart Ross Inverness
Hi Nate. The Replace function does this in one call. It is used like this:
Code: ( text )
  1. somestring = Replace (strInput, strFind, strReplace)

In your function you are mistakenly referring to an undeclared variable txtEmail in place of strInput. I correct this below, and change the name of the function slightly to reflect what it does.
Code: ( text )
  1. Public Function ReplaceSpaces(strInput As String) as String
  2. ' Replaces spaces in a string of text with underscores
  3. Dim Result as String
  4. Result = strInput
  5. If InStr(strInput, " ") = 0 Then
  6.     Result = Replace(strInput, " ", "_")
  7.     msgbox "Spaces have been replaced with underscores.")
  8. Endif
  9. ReplaceSpaces = Result
  10. End Function


-Stewart


perfect. thank you for that. that helped me out a lot. i'm always amazed i can't see the forest for the trees when looking for functions. could the name replace be any more obvious? lol

anyways, for future reference, here is my final code that is error free. i also added similar exception for apostrophes.

Code: ( text )
  1. Public Function ReplaceSpaces(strInput As String) As String
  2. ' Replaces spaces in a string of text with underscores
  3. Dim Result As String
  4. Result = Nz(strInput, "")
  5. If InStr(strInput, " ") > 0 Then
  6.     Result = Replace(strInput, " ", "_")
  7. End If
  8. ' Deletes apostrophes
  9. If InStr(strInput, "'") > 0 Then
  10.     Result = Replace(strInput, "'", "")
  11.    
  12. End If
  13. ReplaceSpaces = Result
  14. Debug.Print Result
  15. End Function

Reply
Reply
Not the answer you were looking for? Post your question . . .
182,530 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Top Microsoft Access / VBA Forum Contributors