Solved

Check a list of string in a input parameter

Posted on 2015-02-03
6
74 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 48

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 48

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 200 total points
Comment Utility
@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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

762 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

9 Experts available now in Live!

Get 1:1 Help Now