Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing a string using paramarray 1

Status
Not open for further replies.

Melagan

MIS
Nov 24, 2004
443
US
I'm trying to write a function, originally to help someone in the JetSQL forum, but am having trouble due to my lack of skill with VB.

My goal is to loop through an array of strings and parse them out of the original string. Here is what I'm working with as of now:

Code:
Public Function SerialParse(ByVal myField As String, ParamArray myArgs() As Variant)

Dim newValue As String
Dim x As Integer

For Each x In myArgs
 newValue = Replace(myField, myArgs, "")
Exit For
Next x

End Function

I am getting a Datatype mismatch error on the Replace()function - I understand that the Replace() function wants string data types, but the ParamArray argument in my function can only be variant. Any ideas? Am I even approching this the correct way?

(Sample desired result in Immediate Window)
___
varTemp = "105A/B/C2026/B/-25"

?SerialParse(varTemp, "A/B/C", "/B")
1052026/-25
___


~Melagan
______
"It's never too late to become what you might have been.
 
Something like this ?
Public Function SerialParse(ByVal myField As String, ParamArray myArgs() As Variant)
Dim newValue As String
Dim x As Variant
newValue = myField
For Each x In myArgs
newValue = Replace(newValue, x, "")
Next x
SerialParse = newValue
End Function


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Code:
Public Function SerialParse(ByVal myField As String, ParamArray myArgs() As Variant)

Dim newValue As String
Dim x As Variant
newValue = myField

For Each x In myArgs
 newValue = Replace(myField, x, "")
Exit For
Next x

SerialParse = newValue
End Function

Right on - works perfect. I must admit that I had no idea how close I was to getting it right, and I REALLY apprecaite the tweaks you made to get it right. Thanks again PH!

~Melagan
______
"It's never too late to become what you might have been.
 
newValue = Replace([!]newValue[/!], x, "")

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I caught that - typo on my part!

I found a bug, however. There is something wrong with the loop as it only seems to be looping the first element of the array.
Immediate:
vartemp = "A/B123/A45"
?SerialParse(vartemp, "A/B", "/A")
123/A45


~Melagan
______
"It's never too late to become what you might have been.
 
With the EXACT function I posted (ac2003):
?SerialParse("A/B123/A45", "A/B", "/A")
12345

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I see - it was the exit for in my code that was messing it up. I thought "exit for" was required somewhere after "For Each", much like IF - END IF. I re-read the helpfile on FOR EACH though and found that exit for is optional, only required if you want to move to the line after "Next".

I have a pretty clear understanding now - thanks again. Star time =)


~Melagan
______
"It's never too late to become what you might have been.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top