For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic > January 2006 > Go To statement required?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Go To statement required?
Mick Whyte

2006-01-25, 6:56 pm

Hi

Am using messagebox vbyes, vbno buttons. Want the vbyes to rerun the
Form Load event. Have even tried to write a GoTo statement but
unsuccessful. Is there not a method such as refresh or redraw in VB6.
Guess I could write a loop that will keep running until no is selcted
but this seems a little heavy handed. Code is:

Private Sub Form_Load()
mainapp:

Dim test1 As Integer
Dim test2 As Integer
Dim test3 As Integer
Dim total As Integer

Dim inputholder
Dim messageholder

inputholder = InputBox("Please enter first mark")
test1 = inputholder
inputholder = InputBox("Please enter second mark")
test2 = inputholder
inputholder = InputBox("Please enter third mark")
test3 = inputholder
total = test1 + test2 + test3
messageholder = MsgBox("Your score is " & total)
messageholder = MsgBox("Would you like to try again?", vbYesNo)
If vbNo = True Then
Form1.Hide
End If

If vbYes = True Then
GoTo mainapp
End If
End Sub

Thanks

Mick

Jeff Johnson [MVP: VB]

2006-01-25, 6:56 pm


"Mick Whyte" <mick.whyte@gmail.com> wrote in message
news:1138205876.707934.113040@f14g2000cwb.googlegroups.com...

> Guess I could write a loop that will keep running until no is selcted
> but this seems a little heavy handed.


That's exactly what I was going to suggest. If your definition of "heavy
handed" is "logical" then I agree....


Ken Halter

2006-01-25, 6:56 pm

"Mick Whyte" <mick.whyte@gmail.com> wrote in message
news:1138205876.707934.113040@f14g2000cwb.googlegroups.com...
> Hi
>
> Am using messagebox vbyes, vbno buttons. Want the vbyes to rerun the
> Form Load event. Have even tried to write a GoTo statement but
> unsuccessful. Is there not a method such as refresh or redraw in VB6.
> Guess I could write a loop that will keep running until no is selcted
> but this seems a little heavy handed. Code is:


You're a bit off on your logic there...

> If vbYes = True Then


First of all, vbYes and vbNo are not variables so they'll never change.
Secondly, you wouldn't want to do this in Form_Load because the form isn't
showing at that point. You can force it to show by using Me.Show but it's
not really "correct".

Try this instead....
'==========
Private Sub Command1_Click()
Dim test1 As Integer
Dim test2 As Integer
Dim test3 As Integer
Dim total As Integer

Dim inputholder As String
Dim messageholder As VbMsgBoxResult

Do 'setup a loop

inputholder = InputBox("Please enter first mark")
test1 = inputholder
inputholder = InputBox("Please enter second mark")
test2 = inputholder
inputholder = InputBox("Please enter third mark")
test3 = inputholder
total = test1 + test2 + test3
Call MsgBox("Your score is " & total) 'no need to store the result of
an 'Ok Only' box
messageholder = MsgBox("Would you like to try again?", vbYesNo)

Loop Until messageholder = vbNo 'this'll run everything again unless the
user clicks No

Form1.Hide 'if you want the form to "go away", use Unload Me instead of
Form1.Hide

End Sub
'==========


David J Mark

2006-01-25, 6:56 pm


"Mick Whyte" <mick.whyte@gmail.com> wrote in message
news:1138205876.707934.113040@f14g2000cwb.googlegroups.com...
> Hi
>
> Am using messagebox vbyes, vbno buttons. Want the vbyes to rerun the
> Form Load event. Have even tried to write a GoTo statement but


No you don't.

> unsuccessful. Is there not a method such as refresh or redraw in VB6.


No kidding. You cannot goto an event. Just as you cannot walk through
time.

> Guess I could write a loop that will keep running until no is selcted
> but this seems a little heavy handed. Code is:


Skip that. Every time I see one of these "wait loops", I want to track down
the culprit and kick them down a staircase. There is a reason that it is
called "event based" programming.

>
> Private Sub Form_Load()
> mainapp:
>
> Dim test1 As Integer
> Dim test2 As Integer
> Dim test3 As Integer
> Dim total As Integer
>
> Dim inputholder
> Dim messageholder
>
> inputholder = InputBox("Please enter first mark")
> test1 = inputholder
> inputholder = InputBox("Please enter second mark")
> test2 = inputholder
> inputholder = InputBox("Please enter third mark")
> test3 = inputholder
> total = test1 + test2 + test3
> messageholder = MsgBox("Your score is " & total)
> messageholder = MsgBox("Would you like to try again?", vbYesNo)
> If vbNo = True Then


As mentioned, this makes no sense. Compare "messageholder" to vbYes and
vbNo.

> Form1.Hide
> End If


Why is this a form at all? It doesn't appear to need a form. Anyway, there
are several ways to loop in VB and you never need to use a Goto (it is the
first sign of shoddy code.) Start on page 1 of the documentation and get
familiar with basic looping. Write your loop in a module. You are not
ready for forms.

>
> If vbYes = True Then
> GoTo mainapp
> End If
> End Sub
>
> Thanks
>
> Mick
>



Alfie [UK]

2006-01-25, 6:56 pm

On 25 Jan 2006 08:17:56 -0800, "Mick Whyte" <mick.whyte@gmail.com>
wrote:
>Hi
>
>Am using messagebox vbyes, vbno buttons. Want the vbyes to rerun the
>Form Load event. Have even tried to write a GoTo statement but
>unsuccessful. Is there not a method such as refresh or redraw in VB6.
>Guess I could write a loop that will keep running until no is selcted
>but this seems a little heavy handed. Code is:
>

You're not actually checking the result that you get back from the
messagebox which is why it isn't working for you, also you don't need to
test for vbYes and vbNo seperately, just use;

If messageholder = vbYes Then
GoTo mainapp:
Else
Me.Hide
End If

If you actually want the app to close once you're done you can use
'Unload Me' instead of 'Me.Hide'.

A 'Do...Loop While messageholder = vbYes' would be a fine alternative,
not sure why you think looping is more heavy handed than using GoTo :)
--
Alfie
<http://www.delphia.co.uk/>
Perhaps we should lower our mental trousers and compare the size of our consciousness' ?

Phill W.

2006-01-27, 7:55 am


"Mick Whyte" <mick.whyte@gmail.com> wrote in message
news:1138205876.707934.113040@f14g2000cwb.googlegroups.com...

> Guess I could write a loop that will keep running until no is selected
> but this seems a little heavy handed.


Oh come on ... loops are what we do for a Living ...

[Module1.bas]
Private Sub Main()
Do
Call DoSum()
Loop Until MsgBox("Would you like to try again?", vbYesNo) = vbNo
End Sub

Private Sub DoSum()
Dim test1 As Integer
Dim test2 As Integer
Dim test3 As Integer
Dim total As Integer

test1 = CInt( "0" & InputBox("Please enter first mark") )
test2 = CInt( "0" & InputBox("Please enter second mark") )
test3 = CInt( "0" & InputBox("Please enter third mark") )

total = test1 + test2 + test3
MsgBox "Your score is " & Format( total )
End Sub

Or, better still, try experimenting with /Visual/ Basic and put some
Labels, TextBoxes and CommandButtons on your Form and see
what you can do with them ... ;-)

HTH,
Phill W.


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com