PDA

View Full Version : String Handling help needed



Kendall
10-08-2010, 04:58 PM
Hi All,

I'm new to VBA, so please bear with me a bit here:) I'm working on updating/maintaining a small project, and am having a bit of trouble with some string handling. Platform is Access 2000.

There is a function that reads the input into a text field on a form, and does different actions based on what the input is. The following are valid inputs:

"1"
"X"
"01" to "99"
"Blend1", "Blend2", "Blend3"
"Crew"
"IM" to "IO"
"SC" to "SD"
23 character alphanumeric scanned from barcode

The input is treated in the program as a string. Now, the 23 character string used to start with an M, but was changed to start with either "01" or "02". Currently the code is handling the input via a Select Case, along the lines of

Case "1"
//do something
Case "01" to "98"
//do something
Case "99"
//do something
Case "IM" to "IO"
//do something
Case "SC" to "SD"
//do something
Case "Blend1" to "Blend3"
//do something
Case "Crew"
//do something
Case Left(string) = "M"
//do something

However, since the 23 character string now starts with either "01" or "02", the Case enters at the "01" to "98" test (which is incorrect).

The codes "01" to "98" are contained in a table, so I could perform a lookup there.

I'm trying to come up with an intelligent way to re-write this function, and hopefully validate the inputs while I'm at it, but I'm new to VBA and can't quite come up with a good solution. Most everything I thought of seemed rather a kludge and I want to do this as correctly as possible so the next guy doesn't have the mess that I do:)

Sorry if the question is a bit vague, my brain is fried after a few hours:)

Thanks in advance for any help/suggestions; if my question wording sucks, let me know and I'll try to post it better!

Thanks,

Kendall

hansup
10-09-2010, 07:42 AM
Other than the barcode, your cases are straightforward. If multiple cases have the same action, you can combine them into one Case statement, such as:Case "1", "01" To "98", "X"You might first test whether the string matches the pattern "0[12]?????????????????????". If it matches, do what you want for barcodes. If it doesn't match, use the Select Case for your other valid possibilities.Public Function Validator(ByVal pStr As String) As Boolean
Dim varValid As Variant

If pStr Like "0[12]?????????????????????" Then
'barcode action
varValid = True
Else
Select Case pStr
Case "1", "01" To "98", "X"
'some action
Case "99"
'a different action
Case Else
'no match found
varValid = False
End Select
If IsNull(varValid) Then
varValid = True
End If
End If
Validator = varValid
End Function

Kendall
10-25-2010, 05:42 PM
Hi,

Thanks for the reply, the LIKE "012..." was exactly what I was looking for.

In retrospect, it was kind of a dumb question, but at the time I couldn't see past my frustration with other parts of the code.

Thanks a lot!