Link to home
Start Free TrialLog in
Avatar of qsysopr
qsysopr

asked on

Help with Arrays

I have created an array of (5) 3char codes , ex: A15 B17 G97 R22 C87.
i want to be able to key a code in a text box and return a message in a label box
of  CORRECT (for matching one of the 5 above) or INCORRECT for not matching at all
Avatar of iProgram
iProgram

Dim ary(5) As String
Private Sub Command1_Click()
     Dim i Integer
     For i = 0 To 4
        If (ary(i) = Text1) Then Exit For
     Next i
     If (ary(i) = Text1) Then
        Label1 = "CORRECT"
     Else
        Label1 = "INCORRECT"
     End If
   
       
End Sub
Avatar of Éric Moreau
You can use this:

Private Sub Command1_Click()
Dim arrX As Variant
Dim strx As String

    arrX = Array("A15", "B17", "G97", "R22", "C87")
   
    strx = Join(arrX, " ")
   
    If InStr(1, strx, Text1.Text) > 0 Then
        MsgBox "Correct"
    Else
        MsgBox "Incorrect"
    End If
End Sub
emoreau, I think there's a problem with your way: won't it match things like "5B1" and "7G97R" as well as correct codes?
Not exactly, as he is JOINing with a space between members...

However it /will/ match '5 B1'  etc

To overcome this, test to see if the instr() result is modulo 4 - then you will know that it is a full member match on a member boundary.

Cheers
MC
right, superchook.

I think iProgram's method, while the least "flashy", is best because it's the easiest to understand. Which is important to whoever comes back to the code later to maintain it. Even if "whoever" is the original author, and "later" is tomorrow morning.
qsysopr, would it work to populate a list box with the correct codes and simply allow the user to choose one of them?
Hi qsysopr,
The other alternative is to use a collection rather than an array. This way yu can use the key property of the collection item to determine whether the item exists or not.

    Dim colTest As New Collection
    colTest.Add "A15", "A15"
    colTest.Add "B17", "B17"
    colTest.Add "G97", "G97"
    colTest.Add "R22", "R22"
    colTest.Add "C87", "C87"
    On Error Resume Next
    Dim blnExists As Boolean
    If Not colTest(Text1.Text) = Text1.Text Then
        blnExists = False
    Else
        blnExists = True
    End If
    MsgBox Text1.Text & IIf(blnExists, " Exists", " Doesn't Exist")
qsysopr..... You wouldn't be an AS/400 person, would you? :o)
caraf_g, didn't spot that one but I think you could be right
Avatar of qsysopr

ASKER

Sorry guys, none of these are working.

iProgram has the right idea i think, i will try to be more clear

i have a label box with my array in it, it consist of (5) 3 char codes, seperated by a space
the user is to input a code (not neccessarly 3 char's) and if it exists in the array
it will sent a "correct code" message to another label box, if not it sends a "incorrect code"


I like oldbie's idea of adding the array elements to a list or combobox.  This guarantees that the user will pick something valid.

Since the "array" seems to be simply a space-delimited string, split it and use the result to populate a listbox:

dim arrCodes() as string
dim intCntr as integer
StartArray = "A15 B17 G97 R22 C87"
arrCode = split(StartArray, " ")
' ensure that a combobox exists, named Combo1
for intCntr=0 to ubound(arrCode)
  Combo1.additem arrCode(intCntr)
next intCntr

Now you have a combobox with all of the choices.  If the user can only select from that list, set style=2-Dropdown List.

Avatar of qsysopr

ASKER

what if it was a very large array?  It would be redundant to code it twice.
I have allready listed the array in the label box, what i want the program
to do is search the array and see if the input is a match.

I am assuming i would have to use the instr function along with mid
to locate a space and count back 3 charachters.

is this correct thinking?
The above code works fine, except that instead of using the variable StartArray, use Label1.Caption (or whatever name you gave to label).

The split function handles the parsing for you so you don't have to use instr.  It simply saves time (if you have VB6.0), but you could also use instr like this:

if instr(" " & Label1.Caption & " ", " " & CodeToFind & " ") > 0 then
  msgbox "Found it!"
else
  msgbox "Not Found!"
endif

Basically, you ake emoreau's idea, but do it with more elementary commands:
* Ensure that your array has spaces around *every* element (concatenate spaces before and after the entire string)
* Build a temporary code that contains leading and trailing spaces
* scan (instr) the first string looking for the second.  

As long as a code does not contain spaces, it will be unique within the list, and doesn't necessary have to be three characters.

Try the above to see if it works for you.
Avatar of qsysopr

ASKER

can anyone see what i am doing wrong?


Private Sub cmdVerify_Button_Click()

Dim strArray(5) As String

Dim intcounter As Integer


intcounter = 1
For intcounter = 1 To 5
    intPosition = InStr(lblAllowable_Codes.Caption, " ")
    strArray(intcounter) = Mid(lblAllowable_Codes.Caption, intcounter * 4 - 3, 3)
        If strArray(intcounter) = Val(text1.Text) Then
          intcounter = intcounter + 1
            If (strArray(intcounter) = text1.Text) Then
                lblMessage_Out.Caption = "Correct Code"
            Else
                lblMessage_Out.Caption = "InCorrect Code"
 
            End If

        End If
Next intcounter
End Sub
On the line,

   strArray(intcounter) = Mid(lblAllowable_Codes.Caption, intcounter * 4 - 3, 3)
       
you're using intcounter rather than intPosition in your multiplication.

Also, intPosition will always point to the first space.

Try using the code I suggested in my previous comment to see if it works for you.
ASKER CERTIFIED SOLUTION
Avatar of rspahitz
rspahitz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Please update this and finalize it.  Expert closing recommendations appreciated.
Moondancer - EE Moderator
Recommend split:

iProgram: 30
emoreau: 30
rspahitz: 40
Thanks very much.  Points have been split.
Points for iProgram -> https://www.experts-exchange.com/jsp/qShow.jsp?qid=20316187
Points for emoreau -> https://www.experts-exchange.com/jsp/qShow.jsp?qid=20316188
Moondancer - EE Moderator