943,907 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Nov 23rd, 2008
0

how to run a sub over again from a button click...

Expand Post »
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).

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub Command2_Click()
  2. Call Winsock2_Connect
  3. 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
Last edited by squidd; Nov 23rd, 2008 at 2:44 am.
Similar Threads
Reputation Points: 10
Solved Threads: 2
Junior Poster
squidd is offline Offline
100 posts
since Nov 2007
Nov 23rd, 2008
0

Re: how to run a sub over again from a button click...

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...

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. private bSomeSub as boolean
  2. private bSomeFunction as boolean
  3.  
  4. private sub SomeSub()
  5. on error goto Oops
  6. 'whatever is in the sub
  7. 'lots of code or not so much :p
  8. bSomeSub = true
  9. Oops:
  10. end sub
  11.  
  12. private function SomeFunction() as byte
  13. on error goto Oops
  14. 'whatever the function code is
  15. 'lots of code :p
  16. SomeFunction = resultingData
  17. bSomeFunction = true
  18. Oops:
  19. end Function

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

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. private sub cmdCommand_Click()
  2. if bSomeSub = false then call SomeSub
  3. if bSomeFunction = false then someResult = SomeFunction(someData)
  4. end sub
Reputation Points: 22
Solved Threads: 3
Light Poster
kain_mcbride is offline Offline
25 posts
since Nov 2008
Nov 23rd, 2008
0

Re: how to run a sub over again from a button click...

... 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...
Reputation Points: 22
Solved Threads: 3
Light Poster
kain_mcbride is offline Offline
25 posts
since Nov 2008
Nov 23rd, 2008
0

Re: how to run a sub over again from a button click...

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

Thanks for the help.
Reputation Points: 10
Solved Threads: 2
Junior Poster
squidd is offline Offline
100 posts
since Nov 2007
Nov 23rd, 2008
0

Re: how to run a sub over again from a button click...

Click to Expand / Collapse  Quote originally posted by squidd ...
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.
Last edited by squidd; Nov 23rd, 2008 at 10:10 pm.
Reputation Points: 10
Solved Threads: 2
Junior Poster
squidd is offline Offline
100 posts
since Nov 2007
Nov 24th, 2008
0

Re: how to run a sub over again from a button click...

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...
Reputation Points: 22
Solved Threads: 3
Light Poster
kain_mcbride is offline Offline
25 posts
since Nov 2008
Nov 24th, 2008
0

Re: how to run a sub over again from a button click...

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

Compile Error: Arguement not optional

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub Command2_Click()
  2. If bWinsock2_Connect = False Then Call Winsock2_Connect
  3. If bWinsock2_DataArrival = False Then Call Winsock2_DataArrival
  4. 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
Reputation Points: 10
Solved Threads: 2
Junior Poster
squidd is offline Offline
100 posts
since Nov 2007
Nov 24th, 2008
0

Re: how to run a sub over again from a button click...

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.
Reputation Points: 10
Solved Threads: 2
Junior Poster
squidd is offline Offline
100 posts
since Nov 2007
Nov 25th, 2008
1

Re: how to run a sub over again from a button click...

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

MSWinsockLib.StateConstants

others you might be interested in are

MSWinsockLib.ErrorConstants
MSWinsockLib.ProtocolConstants

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...
Reputation Points: 22
Solved Threads: 3
Light Poster
kain_mcbride is offline Offline
25 posts
since Nov 2008
Nov 26th, 2008
0

Re: how to run a sub over again from a button click...

*SOLVED BY KAIN*

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

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. 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:

[code/]
text2.text = Winsock2.state[/code

and for every subsequent line i simply added:

[code/]
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?

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. 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!
Last edited by squidd; Nov 26th, 2008 at 7:14 am.
Reputation Points: 10
Solved Threads: 2
Junior Poster
squidd is offline Offline
100 posts
since Nov 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Urgency for vb code...
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Unable to create Link in the Word, using VB6





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC