Solved

Check a list of string in a input parameter

Posted on 2015-02-03
6
75 Views
Last Modified: 2015-02-03
Is there a smarter way to code the following ? Ts

Private Function IsPrice(stCode As String) As Boolean

On Error Resume Next

If InStr(stCode, "9K") > 0 Then
    IsPrice = True
    Exit Function
ElseIf InStr(stCode, "14K") > 0 Then
    IsPrice = True
    Exit Function
ElseIf InStr(stCode, "16K") > 0 Then
    IsPrice = True
    Exit Function
ElseIf InStr(stCode, "18K") > 0 Then
    IsPrice = True
    Exit Function
ElseIf InStr(stCode, "10K") > 0 Then
    IsPrice = True
    Exit Function
End If
0
Comment
Question by:AXISHK
  • 2
  • 2
  • 2
6 Comments
 
LVL 49

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 200 total points
ID: 40585720
HI,

You could try

Private Function IsPrice(stCode As String) As Boolean

If stCode Like "*9K*" Or stCode Like "*14K*" Or _
        stCode Like "*16K*" Or stCode Like "*18K*" Or stCode Like "*10K*" Then
    IsPrice = True
Else
    IsPrice = False
End If
End Function

Private Function IsPrice(stCode As String) As Boolean

IsPrice = (stCode Like "*9K*" Or stCode Like "*14K*" Or _
        stCode Like "*16K*" Or stCode Like "*18K*" Or stCode Like "*10K*")
End Function

Open in new window

Regards
0
 
LVL 29

Expert Comment

by:gowflow
ID: 40585780
Try this

Private Function IsPrice(stCode As String) As Boolean
Dim sPrice As String

sPrice = "9K,14K,16K,18K,10K"
If InStr(1, stCode, sPrice) <> 0 Then
    IsPrice = True
Else
    IsPrice = False
End If

End Function

Open in new window


gowflow
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 40585793
bear in mind that depending on what one of the above 3 you choose, you can get TRUE returned for:-

9K
9K,14
16Kilo
118Kettles

Is this what you wanted?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 49

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 200 total points
ID: 40585799
@gowflow

your code will only work if you swap sPrice and stCode and only if the string test consists only of 9K or 14K, etc.
Private Function IsPrice(stCode As String) As Boolean
Dim sPrice As String

sPrice = "9K,14K,16K,18K,10K"
If InStr(1, sPrice, stCode) <> 0 Then
    IsPrice = True
Else
    IsPrice = False
End If

End Function

Open in new window

0
 
LVL 29

Accepted Solution

by:
gowflow earned 300 total points
ID: 40585901
Sorry I missed something it should be this

Private Function IsPrice(stCode As String) As Boolean
Dim sPrice As String
Dim vPrice
Dim I As Long

sPrice = "9K,14K,16K,18K,10K"
vPrice = Split(sPrice, ",")

For I = LBound(vPrice) To UBound(vPrice)
    If InStr(1, stCode, vPrice(I)) <> 0 Then
        IsPrice = True
        Exit For
    End If
Next I

End Function

Open in new window


gowflow
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 40586083
As the questioner does not explain EXACTLY what the contents of stCode could be at the point of calling the function then all anybody can do is make a guess at what is wanted.

IF the only contents would be an exact strink of "9K"  or "14K" or "16K" or "10K" etc then none of the above are optimal.  What happens when your stock codes change or you add a new one?

If indeed you only have and only ever will have those 5 stock codes then something like the above would work.

However if your stock is fluid and liable to change the I would suggest that you read the available stock codes into an array from the database and validate against those each time.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now