Array's, Loop's & Variable's... Help!

Hello Experts,

I am using this piece of code, and I'm getting an error.  It says variable not defined.  The problem it dislikes is the "i" right after "For".

    Dim ItemNum(1 To 5) As String
    Dim ItemQty(1 To 5) As Integer
    Dim ItemDesc(1 To 5) As String
    
    
    
    ItemNum(1) = txtItem1Num.Value
    ItemNum(2) = txtItem2Num.Value
    ItemNum(3) = txtItem3Num.Value
    ItemNum(4) = txtItem4Num.Value
    ItemNum(5) = txtItem4Num.Value
    
    ItemQty(1) = txtItem1Qty.Value
    ItemQty(2) = txtItem2Qty.Value
    ItemQty(3) = txtItem3Qty.Value
    ItemQty(4) = txtItem4Qty.Value
    ItemQty(5) = txtItem5Qty.Value
    
    ItemDesc(1) = txtItem1Desc.Value
    ItemDesc(2) = txtItem2Desc.Value
    ItemDesc(3) = txtItem3Desc.Value
    ItemDesc(4) = txtItem4Desc.Value
    ItemDesc(5) = txtItem5Desc.Value
    
    For i = 1 To 10
        If ItemQty(i) <> "" Then  ' if the qty is not a blank value then add the item
            str = str & "Item " & i & " - " & ItemNum(i) & " | " & ItemQty(i) & " | " & ItemDesc(i)
            str = str & "<br>"
        End If
        'if it was blank it will be skipped and the loop will move onto the next item (i will run from 1 to 10)
    Next i

Open in new window


Any ideas?  I don't know what im doing here.. lol

Thank you in advance for your help!

~ Geekamo
LVL 1
GeekamoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GeekamoAuthor Commented:
Btw, I don't think this matters - but this code is running in Outlook.
0
Meir RivkinFull stack Software EngineerCommented:
just add Dim i before the loop
    Dim ItemNum(1 To 5) As String
    Dim ItemQty(1 To 5) As Integer
    Dim ItemDesc(1 To 5) As String
    
    
    
    ItemNum(1) = txtItem1Num.Value
    ItemNum(2) = txtItem2Num.Value
    ItemNum(3) = txtItem3Num.Value
    ItemNum(4) = txtItem4Num.Value
    ItemNum(5) = txtItem4Num.Value
    
    ItemQty(1) = txtItem1Qty.Value
    ItemQty(2) = txtItem2Qty.Value
    ItemQty(3) = txtItem3Qty.Value
    ItemQty(4) = txtItem4Qty.Value
    ItemQty(5) = txtItem5Qty.Value
    
    ItemDesc(1) = txtItem1Desc.Value
    ItemDesc(2) = txtItem2Desc.Value
    ItemDesc(3) = txtItem3Desc.Value
    ItemDesc(4) = txtItem4Desc.Value
    ItemDesc(5) = txtItem5Desc.Value

    Dim i
    For i = 1 To 10
        If ItemQty(i) <> "" Then  ' if the qty is not a blank value then add the item
            str = str & "Item " & i & " - " & ItemNum(i) & " | " & ItemQty(i) & " | " & ItemDesc(i)
            str = str & "<br>"
        End If
        'if it was blank it will be skipped and the loop will move onto the next item (i will run from 1 to 10)
    Next i

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GeekamoAuthor Commented:
That solved that, thank you! (now onto more problems...!)
0
GrahamSkanRetiredCommented:
You wisely have Outlook Explicit in the declarations section of the module. This means that VB will check to see if the variable has been declared in a Dim statement. The advantage is that it acts as a type of spelling check. Misspelt variables can give errors that are hard to diagnose.

The 'Dim i' can be anywhere in the procedure. It is a matter of preference or convenience.
0
GeekamoAuthor Commented:
@ GrahamSkan,

Thanks for the tip!  I actually had NO CLUE what the Explicit code meant, other then I've seen it many times in my codes.

As for the Dim, yes I actually put it up near my other Dim's.

Btw, I  have another question posted - still involving same codes - but new error messages.

~ Geekamo
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Outlook

From novice to tech pro — start learning today.