Adrian Theo
Adrian Theo

Reputation: 11

Subscript Out off range vb6

I'm making an antivirus. Now I want my antivirus to change color, so I'm making a form and .ini file (A save). So after a long time coding with Modules and functions. I wan tot test it out until I click the setting form button. I have a subscript out of range error(9).

Private Sub GetSetting(Path As String)
Dim Line As String, tmp() As String, ValueX(10) As String, a As Byte
a = 0
If IsFileX(Path) = True Then
    Open Path For Input As #1
        Do
            
            Line Input #1, Line
            tmp = Split(Line, "=")
            If UBound(tmp) = 1 Then
                a = a + 1
                ValueX(a) = tmp(1)
                Ck1.value = CInt(Left(Replace(ValueX(a), " ", ""), 1))
            End If
                If UBound(tmp) = 1 Then
                a = a + 2
                ValueX(a) = tmp(1)
                Ck2.value = CInt(Left(Replace(ValueX(a), " ", ""), 1))
            End If
                If UBound(tmp) = 1 Then
                a = a + 3
                ValueX(a) = tmp(1)
                Ck3.value = CInt(Left(Replace(ValueX(a), " ", ""), 1))
            End If
                If UBound(tmp) = 1 Then
                a = a + 4
                ValueX(a) = tmp(1)
                Ck4.value = CInt(Left(Replace(ValueX(a), " ", ""), 1))
            End If
                If UBound(tmp) = 1 Then
                
                a = a + 5
                ValueX(a) = tmp(1)
                Ck5.value = CInt(Left(Replace(ValueX(a), " ", ""), 1))
            End If
                If UBound(tmp) = 1 Then
                a = a + 6
                ValueX(a) = tmp(1)
                Ck6.value = CInt(Left(Replace(ValueX(a), " ", ""), 1))
            End If
        Loop Until EOF(1)
    Close #1
Else
MsgBox " Read Antivirus Setting is ERROR !" & vbNewLine & "Because the file [ MurderAV.ini ] is not found!", vbCritical, "MurderAV Error"
End If
End Sub

The Highlighted part is the ValueX(a) in ck5. (Ck is checkbox) I'll photo it so it can be clearer:

Image

Upvotes: 0

Views: 324

Answers (1)

Ken Y-N
Ken Y-N

Reputation: 15009

The problem is that each If statement has an identical condition:

If UBound(tmp) = 1 Then

So, as you go through the code, you execute:

a = 0
a = a + 1
a = a + 2
a = a + 3
a = a + 4
a = a + 5

So a is 15, so you have:

ValueX(15) = tmp(1)

But since ValueX(10) As String, you are off the end of the array.

Upvotes: 2

Related Questions