Subscript out of range in vba

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • n3wkid
    New Member
    • Jun 2014
    • 9

    Subscript out of range in vba

    Dear Master
    I face error in vba code ms access 2010.
    and message error is "Subscript out of range (9)"
    and normally I can't to solve this is.

    this the code
    Code:
    Private Sub show()
    Dim at, bb, url As String
    Dim bc, bd, be, j, bx As Variant
    Dim i, t As Integer
    Dim tbb, tb2 As Variant
    
    For i = 0 To 10
    Me("p" & i).Caption = ""
    Me("p" & i).Visible = False
    Next i
    status.Caption = ""
    
    If InternetGetConnectedState(0&, 0&) Then 
    
    url = "my_site.com/showdata_rev.php"
    at = fcSend("&page=" & fpage, url) 
    Text5 = at 
    If Text5 <> "" Then 
    
    f1.SourceObject = ""
    
    f1.Visible = False
    Text7.Visible = False
    
    Text5 = Replace(Text5, vbTab, "") 
    Text5 = Replace(Text5, vbCr, "") 
    Text5 = Replace(Text5, vbLf, "") 
    bb = ""
    bx = Split(Text5, "<>")
    bd = Split(bx(0), "~") 
    tbb = "try_"
    
    hp_tb (tbb)
    
    DoCmd.RunSQL "CREATE TABLE " & tbb _
    & " (idemployee Number,rectime datetime," _
    & " FirstName Text(255), MiddleName Text(255)," _
    & " LastName Text(255), Placebirth Text(255)," _
    & " datebirth datetime, passport text(10)," _
    & " 1starrivedate datetime," _
    & " leavedate datetime,address text(255)," _
    & " email1 text(255),telp number,status text(50),nationality text(255));" ', DateBirth DateTime);"
    
    Set db = CurrentDb
    For i = 0 To (UBound(bd) - 1)
    bb = ""
    
    bc = Split(bd(i), "|") 
    For t = 0 To UBound(bc)
    bb = bb & cek_isi(HtmlToText(bc(t)), IsNumeric(HtmlToText(bc(t)))) & ", " 
    Next t
    bb = Trim(bb)
    bb = Left(bb, (Len(bb) - 1))
    db.Execute "insert into " & tbb & " values" _
    & " (" & bb & ")"
    Next i
    
    db.Close
    Set db = Nothing
    
    bd = ""
    bd = Split(bx(1), " ")
    For i = 0 To (UBound(bd) - 1)
    Me("p" & i).Caption = bd(i)
    Me("p" & i).Visible = True
    
    Next i
    
    status.Caption = Trim(bx(2))
    
    If UBound(bd) < 1 Then 
    bb = "Nothing found data"
    f1.Visible = False
    Text7.Visible = True
    Else
    
    f1.SourceObject = "f1"
    f1.Form.RecordSource = tbb
    f1.Visible = True
    End If
    Else
    bb = "Wrong Destination" 
    End If
    
    For i = 0 To 10
    If Me("p" & i).Caption = "" Then
    Me("p" & i).Visible = False
    End If
    
    Next i
    
    Else
    MsgBox "Connection Internet is problem." & vbCrLf _
    & "Please try again" & vbCrLf _
    & "with other session", , ":: Disconnect"
    End If
    
    
    End Sub
    and error in line

    bd = Split(bx(1), " ")

    Many Thanks for help me
  • Seth Schrock
    Recognized Expert Specialist
    • Dec 2010
    • 2965

    #2
    The split function returns an array, so if you are assigning its values to a variable, you need to declare the variable as an array. You do this by including opening and closing parentheses after the variable name.
    Code:
    Dim strArray() As String
    This declares it with an unspecified number of array "slots". If you put a number in between the parentheses, then you would be specifying the number of slots you want in your array. In this case though, you don't want the number.

    Another note about declaring variables, when you declare multiple variables on one line, you still have to specify the variable type for each variable or it will be declared as a variant. So in your case, your variables are being declared as
    Code:
    Dim at As Variant, bb As Variant, url As String
    Dim bc As Variant, bd As Variant, be As Variant, j As Variant, bx As Variant
    Dim i As Variant, t As Integer
    Dim tbb As Variant, tb2 As Variant
    While it does still work, it is much better to not use variants when you don't need to.

    Comment

    • n3wkid
      New Member
      • Jun 2014
      • 9

      #3
      I have change with your declare but new error show again in line

      Code:
      Option Compare Database
      Option Explicit
      Dim db As Database
      
      ----- cut code
      Private Sub show()
      
      Dim at As Variant, bb As Variant, url As String
      Dim bc As Variant, bd As Variant, be As Variant, j As Variant, bx As Variant
      Dim i As Variant, t As Integer
      Dim tbb As Variant, tb2 As Variant
      
      cut code . . . (same with code before, I needn't write again here(post Reply))
      
      'error show again in line (error 13) type mismatch
      
       bb = ""
       bx = Split(Text5, "<>")
       bd = Split(bx(0), , "~")  <----- here error again
      Please, what the next?

      Comment

      • Seth Schrock
        Recognized Expert Specialist
        • Dec 2010
        • 2965

        #4
        You still didn't declare your variable as an array as shown in the my first code block. Also, my variable declarations weren't my suggestion, but the equivalent of what your code was already. So for example, your i variable doesn't need to be a variant, but should probably be an integer like your t variable. I haven't gone through the rest of the code to see how the other variables are used to know how they should be declared, but I'm guessing that you can do that for the most part.

        Comment

        Working...