Select case syntax and use of wildcards

421
3
10-07-2011 06:35 AM
EdRollason
New Contributor
I have a string variable (AssetID_Val) which is assigned from a previous element of code.  I want to compare the variable against some set values and assign another variable according to the value of AssetID_Val.

I assume that a select case is the best way of doing this but am not sure of the correct syntax.  I also want to introduce a wildcard character into the select case as AssetID_Val will have a variable number.

This is what I've come up with so far but it isn't working:

Select Case AssetID_Val

    Case Is = "ND_*"
    Set pStdAloneTbl = pMap2.StandaloneTable(0)

    Case Is = "LK_*"
    Set pStdAloneTbl = pMap2.StandaloneTable(1)

    Case Is = "PG_*"
    Set pStdAloneTbl = pMap2.StandaloneTable(2)

    Case Else
    MsgBox "Error"
    pEditor.AbortOperation
    Exit Sub

End Select
The * indicates a 3 digit number which will change each time.  I can't work out whether the error is with my Case syntax or with the introduction of the wildcard.  Help on both would be appreciated.


Cheers


Ed
Reply
0 Kudos
3 Replies
JamesCrandall
MVP Frequent Contributor
The * indicates a 3 digit number which will change each time.  I can't work out whether the error is with my Case syntax or with the introduction of the wildcard.  Help on both would be appreciated.

Ed


Ed,

If the first 3 characters are what you are interested in searching, meaning that anything before the _underscore is what you want to match, then you could just set a new string variable to the first 3 characters and run your Select Case on that.  This would mean that you are only looking to Select the first 3 chars (rather than attempting the wildcard, which I don't think will work).

Something like this:

           

            Dim AssetID_Val As String = "PG_123" 'this is manually set to test it out
            Dim srchString As String = Mid(AssetID_Val, 1, 3)

            Select Case srchString
                Case "ND_"
                    MsgBox(AssetID_Val & " Found!")
                Case "LK_"
                    MsgBox(AssetID_Val & " Found!")
                Case "PG_"
                    MsgBox(AssetID_Val & " Found!")
                Case Else
                    MsgBox("nothing found!")
            End Select



So, in the above example, the MsgBox would popup "PG_123 Found!" because the Select located that the first 3 characters in the AssetID_Val variable are "PG_".  If the AssetID_Val was = "LK_888888", then the msgBox would popul with "LK_888888 Found".

Hope this makes sense!


Here's how your full implementation might look:



Dim srchString As String = Mid(AssetID_Val, 1, 3)

Select Case srchString
    Case "ND_"
    Set pStdAloneTbl = pMap2.StandaloneTable(0)

    Case "LK_"
    Set pStdAloneTbl = pMap2.StandaloneTable(1)

    Case "PG_"
    Set pStdAloneTbl = pMap2.StandaloneTable(2)

    Case Else
    MsgBox "Error"
    pEditor.AbortOperation
    Exit Sub

End Select
Reply
0 Kudos
EdRollason
New Contributor
Hi James,

That seems to work fine, thanks!


Cheers


Ed
Reply
0 Kudos
JamesCrandall
MVP Frequent Contributor
Hi James,

That seems to work fine, thanks!


Cheers


Ed



Excellent!

Take Care,

james
Reply
0 Kudos