Question:
visual basic looping problem?
malibuxdreams
2007-12-15 23:29:54 UTC
i have this code. i want it to loop until the user entered No. though, when the user presses no, it still repeats. How do i fix it?

heres the code:


Option Explicit

Private Sub cmdAsk_Click()

Dim MyValue, Response
Dim UserInput
Dim i As Integer

Do Until i = vbNo

UserInput = InputBox("Ask A Question:", "I know the answer to everything!")
If UserInput = "" Then
MsgBox "You did not ask a question."
Else

Randomize
MyValue = Int((6 * Rnd) + 1)
MsgBox MyValue
Response = MsgBox("Ask another question? ", vbYesNo)



Me.Caption = i
Loop

End If
End Sub
Four answers:
Zurahn
2007-12-15 23:41:56 UTC
I'm not a VB programmer, but it looks as though it should be



Do Until Response = vbNo



instead of



Do Until i = vbNo



As i is never assigned to the value of the prompt.
Owl
2007-12-16 03:25:34 UTC
Interesting software code...try this and see if that doesn't make a difference:



Private Sub cmdAsk_Click()



Rem Option Explicit



Dim MyValue, Response

Dim UserInput

Dim i As Integer

QUESTION:

UserInput = InputBox("Ask A Question:", "I know the answer to everything!")

Do

If UserInput = "" Then

MsgBox "You did not ask a question."

Else



Randomize

MyValue = Int((6 * Rnd) + 1)

MsgBox MyValue

Response = MsgBox("Ask another question? ", vbYesNo)

If Response = vbYes Then GoTo QUESTION

If Response = vbNo Then Exit Do



Me.Caption = i

End If



Loop
TRON
2007-12-15 23:55:52 UTC
The biggest problem i find with your code there is that you have nested stuff:



DO

IF

LOOP

END IF



Tabulating your code usually helps detect this kind of stuff. What you have there is pretty bad. The compiler should see this as a Do without a Loop statement, and a Loop without a Do statement. You want to have the End If inside your loop.



DO

-> Do some stuff.

-> IF

--> In case it is true do something.

-> ELSE

--> In other cases, do something else.

-> END IF

-> Do some more stuff.

LOOP



The above should give you an idea on how to structure your code and how to properly tabulate it.



As others have said before, you kinda of need to have i get a value for the loop's condition to actually be a condition.

Since i was declared to be 0, your loop will never execute (vbNo is equivalent to 0).

Use a:

Do

'' some stuff here

Loop Until

loop instead of the one you have. You want to avoid unneeded initializations and you need to loop at least once to get user input. It is bad programming (and I mean extremely bad programming) to set a variable equal to something for a conditional to be true. That is almost as bad as using GOTO, but not that bad.

That should give you an idea of what's happening there. Cheers.
lizzie
2016-05-24 08:16:04 UTC
I think u hav so many boxes in ur window so to check whether a box is empty or not when u click the 'ok' button do one thing, in your ok button click event sub okbutton_click() if txt_cost.text<>"" //do these stuff else exit sub end if end sub this will do instead of a loop as the text box is always empty the loop always bring the alert


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...