Generic question on running a program. Help?

gavin watson
01-05-2008, 01:03 PM
Hi all,

I'm a noob and i'm having terrible trouble trying to get somethign to work.

Whilst I seem to have acheived quite a lot, i'm getting stuck on a simple piece of coding.

I have a program I have written working fine, but I am trying to use a goto command, but its just not working...

Below is part of the code that is not working, i wonder if you can help me out.

I'm getting a 'compile error, label not defined' pointing to the Private Sub tmrtimedate_Timer() line.





'ALL THINGS TIME
Private Sub tmrtimedate_Timer()
Today = Now
lbltime.Caption = Time
lbldate.Caption = Date
lblday.Caption = Format(Today, "dddd")

If Time = "17:58:40" Then GoSub channeloneON

End Sub

channeloneON:
Text3.ForeColor = &HFF&
Text3.Text = "ON"
channel1 = "ON"
chan1 = 1
total = chan1 + chan2 + chan3 + chan4
Text7.Text = total
Beep
Return

I would quite happily use the goto command to do it also, but just thought gosub would be easier for me here.

I'm not sure if its related, but another thing im struggling on is that I dont know where to actually put the IF command, I am quite fluent in the old GWbasic, but where do you actually put random lines in a working program to, for example, to include the above IF statement, I only put it in the 'Private Sub tmrtimedate_Timer()' subroutine becuase thats the only place it worked (when not using goto, just a 'beep' command to check it actually worked'

Thank you in advance for your help.

Robert Collins
01-05-2008, 01:55 PM
First off your channeloneON: is outside of the Timer event. Put it before your End Sub statement and put a Exit Sub in front of the label channeloneON:.

Robert Collins
01-05-2008, 02:16 PM
You could also do this:

'
'
If Time = "17:58:40" Then
Text3.ForeColor = &HFF&
Text3.Text = "ON"
channel1 = "ON"
chan1 = 1
total = chan1 + chan2 + chan3 + chan4
Text7.Text = total
Beep
End If
'
'
End Sub

gavin watson
01-05-2008, 02:35 PM
hi robert, yes, that works fine, many thnaks for your assistance, much appreciated

gavin watson
01-05-2008, 02:59 PM
hmmm, ok, maybe i mneed more help...

This works fine:


'ALL THINGS TIME
Private Sub tmrtimedate_Timer()
Today = Now
lbltime.Caption = Time
lbldate.Caption = Date
lblday.Caption = Format(Today, "dddd")
If Time = "21:48:10" Then GoSub channeloneON
Exit Sub

channeloneON:
Text3.ForeColor = &HFF&
Text3.Text = "ON"
channel1 = "ON"
chan1 = 1
total = chan1 + chan2 + chan3 + chan4
Out &H378, total
Text7.Text = total
Beep
End Sub

but this doesnt?

'ALL THINGS TIME
Private Sub tmrtimedate_Timer()
Today = Now
lbltime.Caption = Time
lbldate.Caption = Date
lblday.Caption = Format(Today, "dddd")
If Time = "21:48:10" Then GoSub channeloneON
If Time = "21:48:20" Then GoSub channeloneOFF
Exit Sub

channeloneON:
Text3.ForeColor = &HFF&
Text3.Text = "ON"
channel1 = "ON"
chan1 = 1
total = chan1 + chan2 + chan3 + chan4
Out &H378, total
Text7.Text = total
Beep

channeloneOFF:
Text3.ForeColor = &HFF00&
Text3.Text = "OFF"
channel1 = "OFF"
chan1 = 0
total = chan1 + chan2 + chan3 + chan4
Out &H378, total
Text7.Text = total
Beep

End Sub

when i run the second version, only channeloneOFF works, and when the time hits both times, it triggers chenneloneOFF both times, instead of channeloneON (at the time it should...)

any further help?

Thank you again in advance

gavin watson
01-05-2008, 03:19 PM
sorry, didnt read your reply properly and figured out my mistake, for the interest of anyone else with a similar problrm, i had missed out an exit sub

the correct code is as follows:

'ALL THINGS TIME
Private Sub tmrtimedate_Timer()
Today = Now
lbltime.Caption = Time
lbldate.Caption = Date
lblday.Caption = Format(Today, "dddd")
If Time = "21:48:10" Then GoSub channeloneON

If Time = "21:48:20" Then GoSub channeloneOFF

Exit Sub

channeloneON:
Text3.ForeColor = &HFF&
Text3.Text = "ON"
channel1 = "ON"
chan1 = 1
total = chan1 + chan2 + chan3 + chan4
Out &H378, total
Text7.Text = total
Beep

Exit Sub

channeloneOFF:
Text3.ForeColor = &HFF00&
Text3.Text = "OFF"
channel1 = "OFF"
chan1 = 0
total = chan1 + chan2 + chan3 + chan4
Out &H378, total
Text7.Text = total
Beep

End Sub

thanks again robert

Robert Collins
01-05-2008, 07:38 PM
You're doing a GoSub but I don't see any Return statements. If all you want to do is what you are showing then just do a GoTo or if you want to return from that code then do a GoSub but put a Return in the subs.

If you do a GoSub and have no Return statement it would appear to me that you will be leaving the return address on the stack. Example:

GoSub sub-name 'VB puts the return address on the stack and jumps to sub-name.

In the sub you do some stuff but do not return and instead you exit the sub.

The return address is still on the stack. Now somewhere eventually it might catch up with you and bite you.

gavin watson
01-06-2008, 02:48 AM
hi robert, yes, i did wonder what happened about the return command. strangely though, it works fine, but i do understand your comment about it causing me trouble further on.

i will try changing the 'exit sub' for a 'return and see what happens.

thanks

gavin watson
01-06-2008, 02:59 AM
no, strangely that didnt work, i replaced the 'exit sub' with a return and got a 'return without gosub' error on running it...

DougT
01-06-2008, 03:41 AM
I suspect you changed the wrong 'Exit Sub'. You need 'Return' after the two 'Beep' statements. The 'Exit Sub' after the 'If Time = "21:48:20"... ' statement needs to remain there. The 'Exit Sub' after the first 'Beep' statement needs to be removed.

Roger_Wgnr
01-07-2008, 05:40 PM
You really should get away from using the Goto and GoSub statements.
Why not just do the following
'ALL THINGS TIME
Private Sub tmrtimedate_Timer()
Today = Now
lbltime.Caption = Time
lbldate.Caption = Date
lblday.Caption = Format(Today, "dddd")
If Time = "21:48:10" Then
Text3.ForeColor = &HFF&
Text3.Text = "ON"
channel1 = "ON"
chan1 = 1
total = chan1 + chan2 + chan3 + chan4
Out &H378, total
Text7.Text = total
Beep
ElseIf Time = "21:48:20" Then
Text3.ForeColor = &HFF00&
Text3.Text = "OFF"
channel1 = "OFF"
chan1 = 0
total = chan1 + chan2 + chan3 + chan4
Out &H378, total
Text7.Text = total
Beep
End If
End SubGoto And Gosub are old and should be avoided where possible in my understanding but that is my understanding and may not be shared by all.

AstroTux
01-07-2008, 06:20 PM
I think I'm just about to be 1,000,000 th person on this forum to say "why do you need to use GoSub or GoTo"? I'll try to refrain from derailing the thread and say the following:

In VB6, GoSub is dead. Just use a Sub and call it instead. It will auto-return to the calling sub when it completes.

e.g.:

Private Sub FirstSub()

Dim a As Integer
a = 5
Text1.Text = a
Call AddOne
' Execution will continue from here when it returns
a = Val(Text1.Text)
If a <> 6 Then
MsgBox "Didn't Add 1!", VbOkOnly, ""
' Execution shall return here and drop out of the IF statement
Else
MsgBox "The Call Worked!", VbOkOnly, ""
End If

End Sub

Private Sub AddOne()

Text1.Text = Val(Text1.Text) + 1
' Will auto-return to the calling sub FirstSub here and pick up where it left

End Sub


You could expand the test to incorporate a loop so it keeps trying until a = 6:

While a <> 6
If a <> 6 Then
MsgBox "Didn't Add 1! Shall Try Again!", VbOkOnly, ""
Call AddOne
' Execution will return here and drop out the IF statement, but the WHILE loop will still be in effect until a = 6
Else
MsgBox "The Call Worked!", VbOkOnly, ""
End If
Wend


A similar argument exists for GoTo, but it is still used (mainly in error handling I find, such as On Error Goto ErrHandler). I don't know why they didn't allow for On Error Call ErrSub instead.

Private Sub SomeSub()
On Error Goto ErrHandler

' Code Here

Exit Sub

ErrHandler:
Call EH

End Sub

...but that is about it.


GoSub was useful in the early days of BASIC, but it had been made redundant in the last few years (up to '99).

I hope the above has been educational, as well as explaining why you don't really use it anymore. :)

Best regards,
AstroTux.

gavin watson
01-08-2008, 07:56 PM
thank you all for your valued help

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum