Another problem I am looking into is the following:

How does one use a command button to run private sub's (again) or (at all)?

For instance if a website isn't available at the exact moment that it is being checked for certain information on it. I have it set to say "not there." Then, I want to have a "retry" command button appear (which it does) and call the same sub's that did the work b4 to check for that information again in case the site was just too busy or whatever...

I have been googling for the better part of a day on this. I cant seem to get this to work without errors (syntax).

Private Sub Command2_Click()
Call Winsock2_Connect
End Sub

That "should" go to the sub that I want to run again. Then after that I want it to go to another sub for 'data-arrival'... (But no point in that if I cant get the first one to work) again if the retry button is clicked. I hope this is clear enough to actually understand. :)

Any ideas? Thanks :)

Recommended Answers

All 10 Replies

well, one way to tell what subs were successful and which ones weren't would be to create a set of boolean flags... for example...

private bSomeSub as boolean
private bSomeFunction as boolean

private sub SomeSub()
 on error goto Oops
 'whatever is in the sub
 'lots of code or not so much :p
 bSomeSub = true
end sub

private function SomeFunction() as byte
 on error goto Oops
 'whatever the function code is
 'lots of code :p
 SomeFunction = resultingData
 bSomeFunction = true
end Function

then when you retest you run the sub you can check what still needs to run...

private sub cmdCommand_Click()
 if bSomeSub = false then call SomeSub
 if bSomeFunction = false then someResult = SomeFunction(someData)
end sub

... i really need to drink more coffee at night...

ok, since you're using the winsock control, you have a few options you can use... in this case, the name of your control is Winsock2 i think... anyway...

winsock2.state, check that out...

.state can be several things, 0 through to 9

0 - closed
1 - open
2 - listening
3 - connection pending
4 - resolving host
5 - host resolved
6 - connecting
7 - connected
8 - closing
9 - error

so, check it and see where you're at...

I will look into your solutions and get back to you.

Thanks for the help. :)

I will look into your solutions and get back to you.

Thanks for the help. :)

Well I am not having much luck with your code to test what needs to be run.

I have 2 Private Sub's that should run when a button is pressed.

They are the following:

Private Sub Winsock2_Connect() and....
Private Sub Winsock2_DataArrival(ByVal bytesTotal As Long)

I have tried it as:
private Winsock2_Connect as boolean
private Winsock2_DataArrival as boolean
and as
private Winsock2_Connect() as boolean
private Winsock2_DataArrival(ByVal bytesTotal As Long) as boolean

I have tried to set them up as you described in your first reply. I get an error of 'ambiguous name detected.'

~~Also, the winsock.state should be 'closed' as per the code telling it to close. I dont know why I sould need to use that bit. Maybe you could explain a little more clearly on what I would do to use it effectively. What I think you want me to do is:

1: Create an integer (Dim State As Integer)
2: Then print the state of the Winsock (Winsock2.State = State)
Print State (or whatever)

Other than that im not sure why you want me to try that.

Thank you for the help as always. :)

ambiguous name detected because you have to lead the variable name with a b (naming convention for boolean), otherwise you're matching the sub name completely which works as an abiguity...

as for the piece related to state; it was a different method to solve the problem... if you attempt to connect with winsock (control), the state changes during the connection attempt; if the connection attempt errors, the state will be set to 9... if you're trying to make it log in, you could loop while the state is not 7 or 9, if it's 7 then the connection has been successfully established, if it's 9, then it errored... you could also use it in combinatoin with something similar to your pause proceedure you'd listed before to set a time out of, say... 10 seconds....

anyway... :) just a thought...

Trying your first methos, I am still getting an error.

Compile Error: Arguement not optional

Private Sub Command2_Click()
If bWinsock2_Connect = False Then Call Winsock2_Connect
If bWinsock2_DataArrival = False Then Call Winsock2_DataArrival
End Sub

I dont need 'End If' statements in there either i suppose because I get an error if I try to put them in.

But i am hoping this will work.

I will text the 'state' method now i suppose just to see how that works.

Thanks again :)

I tried the 'state' approach and it does work. I would really like to see it as it does throughout the entire process line by line, but i dont know how to do that. What I mean is like make a textbox to show the conntion status throughout the entire process and with each change of state, then a new value is added to the text box while leaving the previous value.

I wonder though, is there a way just the use a command button to re-run the entire needed form minus the 'form load' without calling all of the subs?

I will look into that for right now i suppose.

well, unfortunately i don't remember how to convert an enum to its string value... not sure i ever knew actually ;). anyway; the enum set up for this is


others you might be interested in are


having your program automaticly list the status changes could be done by looping; but that takes up a fair bit of processor time... and basicly locks your program up; though if you had a connection loop, which i believe you do; you could update the status in the text box everytime you finished a loop... keep track of what your previous status was and only add the new status if it changes... to add it to a text box, on the next line, just set the text box to multiline and add the new status with vbCrlf (character return, line feed) to break the line... might also want to lock the text box so the user can't alter it's current contents...

you could also use a direct loop that just monitored the state for changes; this would basicly lock up the program until it decides it's finished with the conection attempt... you could break the loop upon achiving the status you want, an error, or a timeout...

hope that helps some, might just be confusing... not enough coffee yet for 3 am...

commented: very good +2


Welp, i got it fixed... I just did this:

Call Form_Load

I thought it would completely reload everything, and maybe it does, but it is so fast i cant tell its being done.

either way. solved. plus I did what you suggested and I can now see the status in the textbox by doing the following:

text2.text = Winsock2.state[/code

and for every subsequent line i simply added:

text2.text = text2.text & vbCrLf & Winsock2.state[/code

Works like a charm and I see 0,4,7,0... perfecto...

thanks for the help, added to your rep and am giving you the solved regardless of how I got there. :)

much appreciated!

p.s. do you have any idea why I get an error when I tried to run this sub call?

Call Winsock2_DataArrival

I could call the other sub's fine except this one. mabye because there was more to it?

Winsock2_DataArrival(ByVal bytesTotal As Long) <------

but when I added that I got a syntax error... just curious. thanks again!


well, you can't call DataArrival; its an event that fires when you recieve data through that winsock control... you can use it to tell your program what to do with set data, but as far as i know, you can't call it directly (without error handling and expecting to get an error)...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.