OPENMODULE (STRIPSTRING FUNCTION)

YANKEE

Registered User.
Local time
Today, 07:43
Joined
Nov 9, 1999
Messages
10
I want to call up a form and have the user enter an address and then go through the below module to remove all characters not wanted. The module works fine in degug test. I just can't figure out how I can link this function to my form. I set the properties of an address data item to afterupdate openmodule below. I am not sure if my address is being passed through the module. The afterupdate brings me to the module code and then stops there. It does not bring me back to my form and replace the address data item eliminating the stripstring characters.

Do I need to substitute the mystr word with my data item name? Any clue what I need to do?


Option Explicit

'Function StripString()
'Returns a string minus a set of specified chars.
Function StripString(mystr As Variant) As Variant
On Error GoTo StripStringError
Dim strchar As String, strholdString As String
Dim I As Integer
'exit if the passed value is null.
If IsNull(mystr) Then Exit Function
'exit if the passed value is not a string.
If VarType(mystr) <> 8 Then Exit Function
'check each value for invalid characters.
For I = 1 To Len(mystr)
strchar = Mid$(mystr, I, 1)
Select Case strchar
Case ".", ",", "-", "#"
'Do nothing
Case Else
strholdString = strholdString & strchar
End Select
Next I
' Pass back corrected string
StripString = strholdString
StripStringEnd:
Exit Function
StripStringError:
MsgBox Error$
Resume StripStringEnd
End Function
 
Just a guess, as I did not clearly understand the problem as you described it, but I think that you may have an error in the way you return the stripped string. Your method declaration says that you will return a Variant. But when you assign the return value to the function you use a string type.
Hope this helps
Chris
 
Hi,

I think Chris is right, here is what I use to strip punctuation.

Function rt_Strip(src As String) As String

On Error GoTo rt_Strip_Err

Dim i As Integer
Dim tmp As String
Dim cha As String
If Not IsNull(src) Then
For i = 1 To Len(src)
cha = Mid(src, i, 1)
If cha <> "," And cha <> " " And cha <> ":" And cha <> ";" And cha <> "." And cha <> "'" And cha <> "-" And cha <> "/" And cha <> "\" And cha <> "*" And cha <> "?" And cha <> "!" And cha <> Chr(34) Then
tmp = tmp & cha
End If
Next i
End If
rt_Strip = tmp
Exit Function

rt_Strip_Err:
rt_Strip = src
Exit Function

End Function

Hope it helps.
 
Thanks for the information. One more question? The user is entering an address into a form. I then run an afterupdate function to strip the not wanted punctuation. Can I create your function as a module and execute an afterupdate event procedure and pass the new stripped string back to the form?
Do I need to imbed my data item name (address) into the function code so it passes the corrected string back to the form?
 
Hello Yankee,

Yes you can use the code in a module, you simple send the imput (address) to the function. Thats what src is in the bit that goes
Function rt_Strip(src As String) As String

and the function returns the answer to the calling code.
rt_Strip = tmp

so lets say txtAddress is the text box you are using. The code in the After Update event would go.
Private Sub txtAddress_AfterUpdate()

On Error GoTo txtAddress_AfterUpdate_Err

Dim tmp As String

tmp = rt_Strip(Me.txtAddress)

Me.txtAddress = tmp

Exit Sub

txtAddress_AfterUpdate_Err:
MsgBox Error$, 16, "Error"
Exit Sub
End Sub
 
Thanks Robin and Chris for your help. It is starting to make sense. I am going to try to get this to work. Sometimes books and help menus don't cut it. Thats where personal experience rules.

Have a good day! Yankee
 

Users who are viewing this thread

Back
Top Bottom