Run Time Error

Hello Experts,

I am using this code, which is returning a run-time error '13': Type mismatch.

    Dim ItemNum(1 To 5) As String
    Dim ItemQty(1 To 5) As Integer
    Dim ItemDesc(1 To 5) As String
    Dim i
    
    
    
    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?

Thank you in advance for your help!
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.

Meir RivkinFull stack Software EngineerCommented:
ItemQty is defined integer array - size 5.
in line 26 u loop from i=1 to 10, so when u reach 6 the error occur.
change it to:

For i = 1 To 5

Open in new window

0
GrahamSkanRetiredCommented:
Also 'str' is a special word in VB. Declare a variable:

Dim strText as String

and use that instead. Actually it would also be better to tighten the declaration of the 'i' variable:

Dim i as Integer
0
GeekamoAuthor Commented:
@ Sedgwick,

Hmm, ok, I fixed that problem.  But I still have a problem...

Here's a screenshot...

qty
In the screenshot, only look at the first column of qty's.  If I put a "1" into the first two lines, then it errors out on the 3rd array variable (not sure if my terminology is correct) (IE     ItemQty(3) = txtItem3Qty.Value)

If I put a "1" into the first three qty's, then it will error on the 4th array variable.

And then if I fill in all 5 qty's with "1", then it gives me a whole different error and highlights the entire line of code "        If ItemQty(i) <> "" Then  ' if the qty is not a blank value then add the item"

Any ideas?
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

GeekamoAuthor Commented:
Here is the updated code, but I still have problems with it.  :(

    Dim ItemNum(1 To 5) As String
    Dim ItemQty(1 To 5) As Integer
    Dim ItemDesc(1 To 5) As String
    Dim i As Integer
    
    
    
    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 5
        If ItemQty(i) <> "" Then  ' if the qty is not a blank value then add the item
            strText = strText & "Item " & i & " - " & ItemNum(i) & " | " & ItemQty(i) & " | " & ItemDesc(i)
            strText = strText & "<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
GrahamSkanRetiredCommented:
The variables in the ItemQty are integers, so cannot be blank.

Try

if ItemQty(i) <> 0 then
0
GeekamoAuthor Commented:
@ GrahamSkan,

Ok, that solves part of the problem.  After changing that the code does complete - IF - ALL of my quantities have a value.  But if maybe only 2 of the qty's have a value, then it errors on the 3rd variable.
0
GeekamoAuthor Commented:
Ok, here is the latest code...  After troubleshoot, it appears NONE of my QTY varaibles can be blank or it errors.

For my QTY, I put "1" for the first 3, and "0" for the last 2.  If every qty has a value, even including a "0" then the code executes perfectly.

What do I do?

    Dim ItemNum(1 To 5) As String
    Dim ItemQty(1 To 5) As Integer
    Dim ItemDesc(1 To 5) As String
    Dim i As Integer
    
    
    
    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 5
        If ItemQty(i) <> 0 Then  ' if the qty is not a blank value then add the item
            strText = strText & "Item " & i & " - " & ItemNum(i) & " | " & ItemQty(i) & " | " & ItemDesc(i)
            strText = strText & "<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
GrahamSkanRetiredCommented:
You would have a similar problem if you try to make the value equal to the .Text/.Value property of an empty text box. Ideally they should be set to default to 0, but you could do something like

ItemQty(1) = Val(txtItem1Qty.Value)
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:
@ GrahamSkan,

Solves the problem!!! Great!  Thank you so much.

Still have another problem though - unrelated to these issues.  Posting another Q.

~ Geekamo
0
SteveCommented:
hi there... have you considered:

For i = Lbound(ItemNum) To Ubound(ItemNum)
    ItemNum(i) = Me.Controls("txtItem" & i & "Num").Value
Next i
For i = Lbound(ItemQty) To Ubound(ItemQty)
    ItemQty(i) = Val(Me.Controls("txtItem" & i & "Qty").Value)
Next i
For i = Lbound(ItemDesc) To Ubound(ItemDesc)
    ItemDesc(i) = Me.Controls("txtItem" & i & "Desc").Value
Next i

Open in new window


Adding/Removing textboxes will become a tad easier.
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.