Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

do while condition vb6

Tags:

while-loop

vb6

I have a little vb6 program:

Private Sub Form_Load()
    Dim varTemp As Variant
    Dim string1 As String

    Dim x As Integer
    x = 0

    dialog.Filter = "toate fisierele(*.*) | *.*"
    dialog.Flags = cdlOFNAllowMultiselect Or cdlOFNLongNames Or cdlOFNExplorer

    'open the window to select files
    dialog.ShowOpen

    varTemp = Split(dialog.FileName, vbNullChar)

    Do While (varTemp(x) <> "")

    string1 = varTemp(x)
    x = x + 1

    Loop

    Unload Form1
    End

End Sub

I want the Do While to loop until it reaches the end of varTemp. However, when I choose two files from the dialog and "Do While" is hit with x = 3 I get "Run-time error '9': Subscript out of range". What condition should the "Do While" loop have to loop until the end of varTemp? Thank you.

like image 679
peter Avatar asked Jul 12 '26 10:07

peter


1 Answers

You can use this instead:

Do While x <= UBound(varTemp)

Since varTemp will be an array, this will loop until you hit the last element in the array.

In case the user cancels the selection, and varTemp is empty, you may check for an empty string before looping, like this:

If varTemp <> vbNullString Then 
    Do While x <= UBound(varTemp)

        string1 = varTemp(x)
        x = x + 1

    Loop
End If
like image 90
LittleBobbyTables - Au Revoir Avatar answered Jul 18 '26 00:07

LittleBobbyTables - Au Revoir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!