Results 1 to 30 of 30

Thread: Subscript out of range

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2004
    Posts
    279

    Exclamation Subscript out of range

    VB Code:
    1. Dim strString5 As String
    2.     Winsock1(Index).GetData strString5
    3.     If Not CheckUsrBan(strString5) Then
    4.         Winsock1(Index).SendData "blahno"
    5.     ElseIf CheckUsrBan(strString5) Then
    6.         Winsock1(Index).SendData "blahyes"
    7.     End If
    And "CheckUsrBan" function:
    VB Code:
    1. Private Function CheckUsrBan(ByVal pstruser As String) As Boolean
    2.     Open_dbcon
    3.     Set cmdCommand = New ADODB.Command
    4.     With cmdCommand
    5.         Set .ActiveConnection = dbcon
    6.         .CommandType = adCmdText
    7.         .CommandText = "SELECT COUNT(*) AS Total FROM roguespear_ban WHERE user = '" & pstruser & "' "
    8.         Set adoRec = .Execute
    9.         CheckUsrBan = (adoRec.Fields.Item("Total").Value > 0)
    10.         Set adoRec = Nothing
    11.     End With
    12.     Set cmdCommand = Nothing
    13.     Close_dbcon
    14. End Function
    Anyone see a reason why either of those would error "Subscript out of range" ?

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    May 2004
    Posts
    279

    Re: Subscript out of range

    Could you post an example? please

  3. #3
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Subscript out of range

    Your problem might be the following:

    Winsock1(Index) - Index indicates that you have control array (multiple controls of the same type sharing the same name) of Winsock1. Do you really have a control array? If yes then Index might be greater than Winsock1.UBOUND so check what you have and then check value of that index by stepping through your code (F8).

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2004
    Posts
    279

    Re: Subscript out of range

    i have 2 Winsock controls. 1 named sckListen and the other sckAccept. sckAccept has the Index and sckListen doesn't. I don't think that could be the problem because it checks through all the other checks fine it just errors "Subscript out of range" on the code I posted in my first post.

  5. #5
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Subscript out of range

    Quote Originally Posted by Tantrum3k
    i have 2 Winsock controls. 1 named sckListen and the other sckAccept. sckAccept has the Index and sckListen doesn't. I don't think that could be the problem because it checks through all the other checks fine it just errors "Subscript out of range" on the code I posted in my first post.
    I'm afraid you don't understand: you must know the value of that Index you pass to Winsock1 control.

    Say you only have Winsock1(1) control on your form but actual Index value is say ZERO. So, since there are no such control on your form then compiler will generate that error - that's all, very simple. All you have to do is check what do you ahve and what do you pass by DEBUGGING, DEBUGGING and again DEBUGGING so you will find wich exactly line generates that error.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    May 2004
    Posts
    279

    Re: Subscript out of range

    I just tripple checked everything but I see nothing wrong with the winsock controls / arrays.

  7. #7

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    May 2004
    Posts
    279

    Re: Subscript out of range

    well is it possible to delete a string in memory?

  9. #9

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    May 2004
    Posts
    279

    Re: Subscript out of range

    to be honest i dont know what you guys are really saying. i avoid the question because if i say something stupid i wil probably get bashed for being a "n00b" like on other boards. so i ask a different question.

  11. #11
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Subscript out of range

    Put Option Explicit up in the General Declarations part of the code window, which is the very top. Should be the first line of code of them all. This will force you to declare your variables, and you can see if any non declared variables or objects exist this way cause it'll return an error on the line it occurs.

    Now to avoid people calling you a "n00b", lets keep this simple. Do you only have 2 Winsock controls on your form, yes or no? And if you do, are they named sckAccept and sckListen?

  12. #12
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Subscript out of range

    You can set VB Options. Check the box that say "Requires Variable Declaration" and it will put Option Explicit in for you for all new modules, forms, and what-have-you. It won't add them to existing code, though. You have to do that yourself.

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    May 2004
    Posts
    279

    Re: Subscript out of range

    I use Option Explicit all the time

    Do you only have 2 Winsock controls on your form, yes or no? And if you do, are they named sckAccept and sckListen?
    Yes, 2 winsock controls - 1 named sckListen and the other named sckAccept. (sckAccept(0) - 0 as Index).

  14. #14
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Subscript out of range

    Ok eliminate the index from the properties menu from both Winsock controls. Make it not have any number. Next, eliminate anything that has (Index) in your code. Best to go up in the menu to Edit > Replace... and replace (Index) with an empty space. That should do it.

  15. #15
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Subscript out of range

    It looks like you have something called Winsock(Index) in your code.
    Post all of your code.

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    May 2004
    Posts
    279

    Re: Subscript out of range

    i took out the sckAccept(0)/Index and just kept sckListen. i had to put an index on scklisten because this program accepts multiple connections via winsock. now it wont accept connections.

    heres the code of it all even with 2 winsock controls:

    listen:
    VB Code:
    1. Public Const ServerPort = 4500
    2.  
    3. Private Sub Form_Load()
    4.     sckListen.LocalPort = ServerPort
    5.     sckListen.Listen
    6.  
    7.     If Not InitAcceptSockets Then
    8.         MsgBox "ERROR Can't create accept sockets!", vbCritical, "Error"
    9.         Unload Me
    10.         End
    11.     End If
    12. End Sub
    connectionrequest/accept:
    VB Code:
    1. Private Sub sckListen_ConnectionRequest(ByVal requestID As Long)
    2.     Dim aFreeSocket As Integer
    3.     aFreeSocket = GetFreeSocket
    4.  
    5.     If aFreeSocket = 0 Then
    6.         MsgBox "Server reached maximum connections", vbExclamation
    7.         sckAccept(0).Accept requestID
    8.         MsgBox "Server reached maximum connections", vbExclamation
    9.         DoEvents
    10.         sckAccept(0).Close
    11.     Else
    12.         bSocketStatus(aFreeSocket) = True
    13.         sckAccept(aFreeSocket).Accept requestID
    14.         DoEvents
    15.         DebugText "IP " & sckAccept(aFreeSocket).RemoteHostIP
    16.         RefreshComboBox
    17.     End If
    18. End Sub
    dataarrival:
    VB Code:
    1. Private Sub sckAccept_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    2. Dim sData As String
    3. Dim strString1 As String
    4. Dim strString2 As String
    5. Dim parts() As String, i As Long
    6.  
    7. sckAccept(Index).GetData sData
    8.  
    9. parts = Split(sData, ";")
    10. For i = 0 To UBound(parts)
    11. strString1 = parts(0)
    12. strString2 = parts(1)
    13.  
    14.     If Not Login(strString1, strString2) Then
    15.         sckAccept(Index).SendData "upno"
    16.     ElseIf Login(strString1, strString2) Then
    17.         sckAccept(Index).SendData "upyes"
    18.     End If
    19.  
    20. Exit For
    21. Next i
    22.  
    23.     Dim strString3 As Long
    24.     sckAccept(Index).GetData strString3
    25.     If Not CheckBan(strString3) Then
    26.         sckAccept(Index).SendData "hdnotbanned"
    27.     ElseIf CheckBan(strString3) Then
    28.         sckAccept(Index).SendData "hdisbanned"
    29.     End If
    30.  
    31.     Dim strString4 As String
    32.     sckAccept(Index).GetData strString4
    33.     If Not CheckBan2(strString4) Then
    34.         sckAccept(Index).SendData "hd2notbanned"
    35.     ElseIf CheckBan2(strString4) Then
    36.         sckAccept(Index).SendData "hd2isbanned"
    37.     End If
    38.  
    39.     Dim strString5 As String
    40.     sckAccept(Index).GetData strString5
    41.     If Not CheckUsrBan(strString5) Then
    42.         sckAccept(Index).SendData "usrbanno"
    43.     ElseIf CheckUsrBan(strString5) Then
    44.         sckAccept(Index).SendData "usrbanyes"
    45.     End If
    46. End Sub
    close:
    VB Code:
    1. Private Sub sckAccept_Close(Index As Integer)
    2.     bSocketStatus(Index) = False
    3.     DebugText "Client " & Index & " (" & sckAccept(Index).RemoteHostIP & ") disconnected"
    4.     sckAccept(Index).Close
    5. End Sub
    Any reason for "Subscript out of range" in DataArrival ?

  17. #17
    Frenzied Member Devion's Avatar
    Join Date
    Sep 2000
    Location
    The Netherlands
    Posts
    1,049

    Re: Subscript out of range

    is sckAccept indexed? (In other words; In the designer window.. Click on the winsock icon of SckAccept and check if there is a number behind Index) - If not.. Add a 0

    and before splitting data first do:

    VB Code:
    1. if instr(sData, ";") then
    2.   ' your code here
    3. else
    4.  ' The data sent didn't contain any seperatable data. Do something else here.
    5. end if

    And ehh... Multiple GetData's from the winsock obj? Nice... I hope you got your client timing it right then

  18. #18
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Subscript out of range

    Devion, I don't know if you have been reading, but he does not need a control array. He only has two winsock controls, and they both have different names.

    Get rid of the (0)'s, the (Index)'s, and anything that says Index As Integer. Also go to the properties window to both the sockets, and where it says Index, get rid of any number in there and make it blank. Do not put 0 or anything.

  19. #19
    Frenzied Member Devion's Avatar
    Join Date
    Sep 2000
    Location
    The Netherlands
    Posts
    1,049

    Re: Subscript out of range

    So far I can make up from his code that he's trying to make a server application that can host multiple connections.. Removing the index on the Winsock will make it *****Peer :P

  20. #20
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Subscript out of range

    I edited my above post. He only has 2 winsock controls.

  21. #21
    Frenzied Member Devion's Avatar
    Join Date
    Sep 2000
    Location
    The Netherlands
    Posts
    1,049

    Re: Subscript out of range

    Maybe the author should post what line it actually triggers the error on....
    Makes things easier

  22. #22
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Subscript out of range

    Quote Originally Posted by Tantrum3k
    to be honest i dont know what you guys are really saying. i avoid the question because if i say something stupid i wil probably get bashed for being a "n00b" like on other boards. so i ask a different question.
    No no no - VBF is different and that's why we are what we are ... (if that makes sense).
    You just go ahead and post your question but try to stay "in tact" with the original. For new ques open new thread, otherwise it may get messy and people stop responding.
    Can you upload your project so we can look at it?

  23. #23
    Frenzied Member Devion's Avatar
    Join Date
    Sep 2000
    Location
    The Netherlands
    Posts
    1,049

    Re: Subscript out of range

    I doubt anyone would openly call you a noob.. VBForums is different.. we are all one big family bunch of VB Geeks passing experience from one to another helping everyone learn something new every day.

  24. #24

    Thread Starter
    Hyperactive Member
    Join Date
    May 2004
    Posts
    279

    Re: Subscript out of range

    I think i found the problem.

    This:
    VB Code:
    1. Dim parts() As String, i As Long
    2.  
    3. parts = Split(blah, ";")
    4. For i = 0 To UBound(parts)
    MSDN says:
    You declared an array but didn't specify the number of elements. For example, the following code causes this error:

    Dim MyArray() As Integer
    MyArray(8) = 234 ' Causes Error 9

    Visual Basic doesn't implicitly dimension unspecified array ranges as 0 10. Instead, you must use Dim or ReDim to specify explicitly the number of elements in an array.
    Could it be because "Dim parts() As String" doesn't have a number in parts(#) or something along those lines....?

  25. #25
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Subscript out of range

    No, that shouldn't be a problem unless string nthat you want to split is empty. Arrays without dimention are called dynamic and that is a typical example of usage since numebr of items is unknown. Check this sample:
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim strText$, arItems() As String
    3.  
    4.     strText = "a;b;c;d;e;f;g"
    5.     If Not Trim(strText) = "" Then
    6.         arItems = Split(strText, ";")
    7.         Debug.Print UBound(arItems) 'should be 6
    8.     End If
    9.  
    10. End Sub

  26. #26

    Thread Starter
    Hyperactive Member
    Join Date
    May 2004
    Posts
    279

    Re: Subscript out of range

    From other site:
    LBound and UBound functions

    If you try to use an element of an array which is too large or too small, you'll get a "Subscript out of range" error. Use the LBound and UBound functions to help avoid that.

    Dim sMessage(0 To 6)
    sMessage(6) = "Today is Saturday"
    iResult% = UBound(sMessage) 'iResult = 6
    iResult% = LBound(sMessage) 'iResult = 0
    sResult$ = sMessage(UBound(sMessage))'iResult = "Today is Saturday"
    Control Arrays

    Like variable arrays, control arrays have the same name with a unique subscript. You create a control array at design time by naming one control and giving another control of the same type the same name. When you attempt this, VB will prompt you with "You already have a control named (name), do you want to create a control array?" Obviously, you click yes and it's done. To add more controls to the control array, simply give it the same name and the next available subscript will be automatically added.

    With control arrays, we normally don't say subscript, but rather the subscript is stored in the Index property of the control. Once the control array has been created, you can change the Index of a control at will, but it's a good idea to keep them consecutive for use in For...Next loops.

    Dim i As Integer
    For i% = 0 To 49
    txtInput(i%).Text = ""
    Next i%
    Can someone give me an example of a control array that causes error "Subscript out of range" please?

  27. #27
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Subscript out of range

    How about just an array?
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.   Dim t()
    5.   Dim x As Integer
    6.   x = t(1)
    7. End Sub

  28. #28

    Thread Starter
    Hyperactive Member
    Join Date
    May 2004
    Posts
    279

    Re: Subscript out of range

    VB Code:
    1. Dim strString1 As String
    2. Dim strString2 As String
    3. Dim parts() As String, i As Long
    4.  
    5. parts = Split(sData, ";")
    6. For i = 0 To UBound(parts)
    7. strString1 = parts(0)
    8. strString2 = parts(1)
    9. Exit For
    10. Next
    I took all that "parts" and "For" stuff out and it no longer errors "Subscript out of range". Does anyone see why that "For" stuff is causing the error?

  29. #29
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Subscript out of range

    If it doesn't find a ";" it will error.

  30. #30

    Thread Starter
    Hyperactive Member
    Join Date
    May 2004
    Posts
    279

    Re: Subscript out of range

    I found another way to do what I'm trying to do.
    VB Code:
    1. Dim blah As String
    2. Dim buf As String
    3. buf = String(32, 0)
    4. blah = recvString(lngSocket, buf, Len(buf), 0)
    5. While blah = True ' while waiting to receive data
    6. DoEvents
    7. Wend
    8.  
    9. If blah = True Then ' if blah(recvString) receives data then...
    10. MsgBox "Received Data: " & buf
    11. End If
    Is that correct? Or am I doing it wrong? I have a feeling I am

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width