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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

InStr Function 1

Status
Not open for further replies.

delboy1295

Technical User
Nov 27, 2003
7
0
0
GB
A Simple questiob for you wonderful techies!

I want to check the validity of certain codes viz:-

Dim OKCode, StkCode As String
StkCode = Mid(StockItemForm.StockItem.Code, 1, 4)

OKCode = "CLT/"

If InStr(OKCode, StkCode) = 0 Then

MsgBox StkCode, vbCritical
cancel = True
End If
End Sub


I want this to be ok only if 'CLT/' is entered, but the above accepts 'C' or 'L' etc.

Also I have some 30 of these to check - how do I string them together?

Thanks in anticipation!

Delboy
 
1. the instr line should read:

if instr(stkcode,okcode)=true then

this will check to ensure the okcode is contained inside stkcode. At the moment you are trying to find the whole command inside the wanted letters.

2. When you say you have 30 to do, what do you mean? There are 30 acceptable commands, or you want to check 30 lines for 30 commands?

BB
 
1. Thanks for info BB.

2. I need to create a list of acceptable codes Eg:-

CLT/,FLT/,TPT/,VLN/ etc. Then I need to check that the first four letters on a new record contain them.

So going back to the program, the OKCode needs to be a list of the above. I get various errors depending how I list them Eg Brackets or commas.

Any Ideas
 
You say above you want to make sure the first four letters are the command. If this is the case you need to use left(mystring,4) instead. This will only check the left hand 4 letters, and ignore the command if it appears half way through a command.

Next, you need to split the string into an array.

Dim myArray() as string
dim counter as integer

for i=1 to len(myCommands)
if mid(myCommands,i,1)=","then counter=counter+1

next i

redim myArray(counter)

myArray=split(myCommands,",")

The above will put your comma seperated commands into the array. You can then iterate through the array, and find out if the users input starts with the command.

dim comNum as integer

for i=0 to ubound(myArray)
if left(stkCode,4)=myArray(i) then
comNum=i+1
end if
next i

select case comNum
case 0 'User input was invalid
msg="Invalid input"
case 1
msg = "Input was CTL/"
case else
msg = "Good input, not known"
end select

msgbox msg

The above will iterate through your list of commands, and then using the select case statement do something different for each input.

Hope this helped

BB
 
Option Explicit
Dim myArray() As String

Public Sub form_load()
myArray = Split("CLT/,FLT/,TPT/,VLN/", ",")
End Sub

Private Sub Command1_Click()
MsgBox CommandExists("CLT/Whatever goes here")
End Sub

Public Function CommandExists(stkCode As String) As Boolean
Dim i As Integer

For i = 0 To UBound(myArray)
If Left(stkCode, 4) = myArray(i) Then
CommandExists = True
End If
Next i
End Function
 
Many thanks Lplates! With a little personalising that was v useful!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top