Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to loop through records three times but WHILE does not work

Status
Not open for further replies.

shaunacol

Programmer
Jan 29, 2001
226
GB
I have a form that contains the a question with 3 multiple choice answers. The form records the persons answer as 'Answer1' and also holds a copy of the correct answer called 'Correct'. If I use the following code behind a button on the form then it works but when there are no more questions left then there is just a blank form. Therefore I tried putting a WHILE statement around it so that it runs only 3 times (for the three questions in the database) but that did not work. I want to run the code below for the three questions but then once the person has gotten them correct I want to do a further action (open a new form)

If Correct = Answer1 Then
DoCmd.RunMacro "correct answer"
Else
DoCmd.RunMacro "incorrect answer"
End If

PS the macros simply show msg box saying correct or incorrect and if correct then move to next record.

Many thanks for your help
 
We can't see any "WHILE statement". Also, it would really help if you provided some field names and datatypes.

Are there always three questions? Are these three records? Are you expecting to step through all records and open as many msgbox()s?

Duane
Hook'D on Access
MS Access MVP
 
I took the while statement out because it did not work. I named an integer and gave it a value of 3 and tried to run thro code above while integer was more than 3. But it just got stuck in the loop or ran through the entire code only 3 times regardless of a correct or incorrect answer.

In answer to your questions, there are 3 questions stored in a table called questions. Yes there are always 3.

What I am trying to do is allow a person the chance to choose a,b or c as an answer to each of the three questions. For each question the bound field 'correct' contains the correct value (a,b or c) and the unbound field in my form called 'answer1' stores the persons response. There is also a button which when pressed runs the code above. I hope this makes more sense.
 
How are ya shaunaco . . .

Some info on what the macro's [blue]correct answer[/blue] & [blue]incorrect answer[/blue] do would help.

[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
The correct answer macro shows message box saying 'you are correct' then moves to the next record/question. All the incorrect macro does is display a message box saying 'you are incorrect'
 
I named an integer and gave it a value of 3 and tried to run thro code above while integer was more than 3
And how did the integer get changed after each run?
This is why you should respond to Duane's post by actually showing the code that failed.


Randy
 
I don't have the code because it didn't work. I tried minus 1 from the integer after every correct answer but of course that did not work because if the person was wrong it was stuck in the loop. I then tried minus 1 after a correct or incorrect answer and that did not work because then the code is only run through 3 times regardless of a correct or incorrect answer. I yried lots of things that all failed
 

What is the structure of the table that contains the questions and answers?


Randy
 
I cannot see how a While loop would work for the situation you describe. You have one question with three possible out comes. A series of if statements, or if you are capable of it, a Select structure would suffice.


if Correct = Answer1 Then
DoCmd.RunMacro "correct answer"
Else
DoCmd.RunMacro "incorrect answer"
End If

if Correct = Answer2 Then
DoCmd.RunMacro "correct answer"
Else
DoCmd.RunMacro "incorrect answer"
End If

if Correct = Answer3 Then
DoCmd.RunMacro "correct answer"
Else
DoCmd.RunMacro "incorrect answer"
End If
 
In fact, at it's simplest would be:

if Correct=Answer1 OR Correct = Answer2 OR Correct=Answer3 Then
DoCmd.RunMacro "correct answer"
Else
DoCmd.RunMacro "incorrect answer"
End If

 


I yried lots of things that all failed
You really need to post your code that failed, in order for the experts to observe what you have tried.


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Thanks that's what I have already, I now want that code to work only for 3 questions. At the moment the form just goes blank after the third question has been answered correctly. I am thinking it is impossible so now trying to think of ways to do something with the blank form, ie hide a button which only becomes available when the form is blank. Hoping it might work....
 
Hang on I think VBa jock maybe onto something. Maybe I can use the unique Id of the question to do this.

I.e if question.I'd = 1, w or 3 then.....

I'll try that too
Thanks for help
 

Again...
What is the structure of the table that contains the questions and answers?


Randy
 
OK ive got it working with an embedded IF - thanks for the ideas, here is the code:

If Correct = Answer1 Then
DoCmd.RunMacro "correct answer"
If QuestionID = 4 Then
DoCmd.close
DoCmd.OpenForm "services"
End If
Else
DoCmd.RunMacro "incorrect answer"
End If
 
shaunacol . . .

Is the user allowed to answer all three questions [blue]randomly[/blue] or is their a [blue]set order[/blue] (q1 q2 q3)?

[blue]Your Thoughts! . . .[/blue]

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
My code was based on the assumption that the user hits some sort of "Next" or "OK" button which kicks in the code, ce he is testing one answer (his variable "correct") against the three possible choices, hence my opinion that a Do/While/Wend is the wrong structure.
 
Howdy vbajock . . .

Yeah ... I saw that in the beginning (can't be handled by a loop). I was looking for the method used to navigate to the other two questions before writing any code. This was never revealed. Oh well.

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Please see my very first post "If I use the following code behind a button on the form then it works".
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top