|
-
Aug 18th, 2005, 07:54 PM
#1
Thread Starter
Hyperactive Member
Subscript out of range
VB Code:
Dim strString5 As String
Winsock1(Index).GetData strString5
If Not CheckUsrBan(strString5) Then
Winsock1(Index).SendData "blahno"
ElseIf CheckUsrBan(strString5) Then
Winsock1(Index).SendData "blahyes"
End If
And "CheckUsrBan" function:
VB Code:
Private Function CheckUsrBan(ByVal pstruser As String) As Boolean
Open_dbcon
Set cmdCommand = New ADODB.Command
With cmdCommand
Set .ActiveConnection = dbcon
.CommandType = adCmdText
.CommandText = "SELECT COUNT(*) AS Total FROM roguespear_ban WHERE user = '" & pstruser & "' "
Set adoRec = .Execute
CheckUsrBan = (adoRec.Fields.Item("Total").Value > 0)
Set adoRec = Nothing
End With
Set cmdCommand = Nothing
Close_dbcon
End Function
Anyone see a reason why either of those would error "Subscript out of range" ?
-
Aug 18th, 2005, 08:01 PM
#2
Thread Starter
Hyperactive Member
Re: Subscript out of range
Could you post an example? please
-
Aug 18th, 2005, 08:10 PM
#3
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).
-
Aug 18th, 2005, 08:13 PM
#4
Thread Starter
Hyperactive Member
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.
-
Aug 18th, 2005, 08:26 PM
#5
Re: Subscript out of range
 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.
-
Aug 18th, 2005, 08:27 PM
#6
Thread Starter
Hyperactive Member
Re: Subscript out of range
I just tripple checked everything but I see nothing wrong with the winsock controls / arrays.
-
Aug 18th, 2005, 08:31 PM
#7
Re: Subscript out of range
You said you have 2 winsock controls: 1 named sckListen and the other sckAccept. I wonder then what is Winsock1(Index) stands for ???? Do you have such control at all ???? Do you have Option Explicit turned on ????
-
Aug 18th, 2005, 09:26 PM
#8
Thread Starter
Hyperactive Member
Re: Subscript out of range
well is it possible to delete a string in memory?
-
Aug 18th, 2005, 09:54 PM
#9
Re: Subscript out of range
What does it have to do with your original "problem" ?
-
Aug 18th, 2005, 10:41 PM
#10
Thread Starter
Hyperactive Member
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.
-
Aug 18th, 2005, 10:48 PM
#11
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?
-
Aug 18th, 2005, 10:50 PM
#12
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.
-
Aug 18th, 2005, 11:11 PM
#13
Thread Starter
Hyperactive Member
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).
-
Aug 19th, 2005, 12:12 AM
#14
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.
-
Aug 19th, 2005, 12:24 AM
#15
Re: Subscript out of range
It looks like you have something called Winsock(Index) in your code.
Post all of your code.
-
Aug 19th, 2005, 01:00 AM
#16
Thread Starter
Hyperactive Member
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:
Public Const ServerPort = 4500
Private Sub Form_Load()
sckListen.LocalPort = ServerPort
sckListen.Listen
If Not InitAcceptSockets Then
MsgBox "ERROR Can't create accept sockets!", vbCritical, "Error"
Unload Me
End
End If
End Sub
connectionrequest/accept:
VB Code:
Private Sub sckListen_ConnectionRequest(ByVal requestID As Long)
Dim aFreeSocket As Integer
aFreeSocket = GetFreeSocket
If aFreeSocket = 0 Then
MsgBox "Server reached maximum connections", vbExclamation
sckAccept(0).Accept requestID
MsgBox "Server reached maximum connections", vbExclamation
DoEvents
sckAccept(0).Close
Else
bSocketStatus(aFreeSocket) = True
sckAccept(aFreeSocket).Accept requestID
DoEvents
DebugText "IP " & sckAccept(aFreeSocket).RemoteHostIP
RefreshComboBox
End If
End Sub
dataarrival:
VB Code:
Private Sub sckAccept_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim sData As String
Dim strString1 As String
Dim strString2 As String
Dim parts() As String, i As Long
sckAccept(Index).GetData sData
parts = Split(sData, ";")
For i = 0 To UBound(parts)
strString1 = parts(0)
strString2 = parts(1)
If Not Login(strString1, strString2) Then
sckAccept(Index).SendData "upno"
ElseIf Login(strString1, strString2) Then
sckAccept(Index).SendData "upyes"
End If
Exit For
Next i
Dim strString3 As Long
sckAccept(Index).GetData strString3
If Not CheckBan(strString3) Then
sckAccept(Index).SendData "hdnotbanned"
ElseIf CheckBan(strString3) Then
sckAccept(Index).SendData "hdisbanned"
End If
Dim strString4 As String
sckAccept(Index).GetData strString4
If Not CheckBan2(strString4) Then
sckAccept(Index).SendData "hd2notbanned"
ElseIf CheckBan2(strString4) Then
sckAccept(Index).SendData "hd2isbanned"
End If
Dim strString5 As String
sckAccept(Index).GetData strString5
If Not CheckUsrBan(strString5) Then
sckAccept(Index).SendData "usrbanno"
ElseIf CheckUsrBan(strString5) Then
sckAccept(Index).SendData "usrbanyes"
End If
End Sub
close:
VB Code:
Private Sub sckAccept_Close(Index As Integer)
bSocketStatus(Index) = False
DebugText "Client " & Index & " (" & sckAccept(Index).RemoteHostIP & ") disconnected"
sckAccept(Index).Close
End Sub
Any reason for "Subscript out of range" in DataArrival ?
-
Aug 19th, 2005, 01:18 AM
#17
Frenzied Member
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:
if instr(sData, ";") then
' your code here
else
' The data sent didn't contain any seperatable data. Do something else here.
end if
And ehh... Multiple GetData's from the winsock obj? Nice... I hope you got your client timing it right then
-
Aug 19th, 2005, 06:06 AM
#18
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.
-
Aug 19th, 2005, 06:07 AM
#19
Frenzied Member
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
-
Aug 19th, 2005, 06:10 AM
#20
Re: Subscript out of range
I edited my above post. He only has 2 winsock controls.
-
Aug 19th, 2005, 06:15 AM
#21
Frenzied Member
Re: Subscript out of range
Maybe the author should post what line it actually triggers the error on....
Makes things easier
-
Aug 19th, 2005, 07:35 AM
#22
Re: Subscript out of range
 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?
-
Aug 19th, 2005, 07:37 AM
#23
Frenzied Member
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.
-
Aug 19th, 2005, 04:05 PM
#24
Thread Starter
Hyperactive Member
Re: Subscript out of range
I think i found the problem.
This:
VB Code:
Dim parts() As String, i As Long
parts = Split(blah, ";")
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....?
-
Aug 19th, 2005, 05:32 PM
#25
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:
Private Sub Command1_Click()
Dim strText$, arItems() As String
strText = "a;b;c;d;e;f;g"
If Not Trim(strText) = "" Then
arItems = Split(strText, ";")
Debug.Print UBound(arItems) 'should be 6
End If
End Sub
-
Aug 19th, 2005, 07:22 PM
#26
Thread Starter
Hyperactive Member
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?
-
Aug 19th, 2005, 07:41 PM
#27
Re: Subscript out of range
How about just an array?
VB Code:
Option Explicit
Private Sub Form_Load()
Dim t()
Dim x As Integer
x = t(1)
End Sub
-
Aug 19th, 2005, 11:57 PM
#28
Thread Starter
Hyperactive Member
Re: Subscript out of range
VB Code:
Dim strString1 As String
Dim strString2 As String
Dim parts() As String, i As Long
parts = Split(sData, ";")
For i = 0 To UBound(parts)
strString1 = parts(0)
strString2 = parts(1)
Exit For
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?
-
Aug 20th, 2005, 12:01 AM
#29
Re: Subscript out of range
If it doesn't find a ";" it will error.
-
Aug 20th, 2005, 07:26 PM
#30
Thread Starter
Hyperactive Member
Re: Subscript out of range
I found another way to do what I'm trying to do.
VB Code:
Dim blah As String
Dim buf As String
buf = String(32, 0)
blah = recvString(lngSocket, buf, Len(buf), 0)
While blah = True ' while waiting to receive data
DoEvents
Wend
If blah = True Then ' if blah(recvString) receives data then...
MsgBox "Received Data: " & buf
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|