Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-30-2016, 04:53 AM
unwanted unwanted is offline Using check boxes and moving to start of document Windows 7 64bit Using check boxes and moving to start of document Office 2013
Novice
Using check boxes and moving to start of document
 
Join Date: May 2016
Posts: 2
unwanted is on a distinguished road
Default Using check boxes and moving to start of document

Hello I just have a couple of questions about checkboxes.

I'm currently putting in a kind of multiple choice quiz into a document. I have some questions with the check boxes bellow them and at the end a button to press to submit it and one to reset it.

I used the follow VBA on the submit button where the Correct answer was the 2nd choice:

If checkbox2.Value = True Then
answer1.Caption = "Correct"
Else
answer1.Caption = "Incorrect"
End If



Now if the correct box is checked then it says "Correct" and if any of the incorrect boxess are checked then it says "Incorrect" like it should, however, if both the correct and an incorrect box are checked it still comes up "Correct". I only want it to say "Correct" if only the correct answer is chosen and nothing else.

I have got around the problem with the following code:

If checkbox2.Value = True Then
answer1.Caption = "Correct"
Else
answer1.Caption = "Incorrect"
End If

If checkbox1.Value = True Then
answer1.Caption = "Incorrect"
End If

If checkbox3.Value = True Then
answer1.Caption = "Incorrect"
End If

If checkbox4.Value = True Then
answer1.Caption = "Incorrect"
End If

That works fine so if the wrong box or any other boxes are checked along with the correct box it will display "Incorrect" but is quite cumbersome (and will be quite time consuming to do it over multiple documents with lots of questions) so is there a way to add a kind of clause to the statement so it does something like:

If checkbox2 = True AND checkboxes 1,3,4 = False Then
answer1.Caption - "Correct"
Else
answer1.Caption = "Incorrect"
End If


Secondly I have some questions where you have to select two or three of the answers, so if in the example above the answers were boxes 2 and 4, how would I make it display "Correct" only when both boxes are checked and "Incorrect" if one of the incorrect are checked or there is only one box checked?

The best I can come up with is this, where it works fine if both correct answers are chosen, no correct answers are chosen or only checkbox2 is chosen (as in it displays "Incorrect" because both answers arn't chosen), but if I have checkbox4 chosen and any incorrect answer or no other answer, it displays "Correct" instead of "Incorrect".

If checkbox2.Value = True Then
answer1.Caption = "Correct"
End If

If checkbox4.Value = True Then
answer1.Caption = "Correct"
Else
a8.Caption = "Incorrect"
End If

If checkbox1.Value = True Then
answer1.Caption = "Incorrect"
End If

If checkbox3.Value = True Then
answer1.Caption = "Incorrect"
End If


And lastly I can't seem to find anything with google for doing something as simple as making the "submit" button also go to the top of the document when it is clicked.

Resolution: use an "And" statement within the IF e.g.

Code:
If q1c.Value = True And q1a = False And q1b = False Then
   a1.Caption = "Correct"
Else
  a1.Caption = "Incorrect"
End If
I tried that before and it wouldn't accept the "And" but now it is accepting it no problem.

Last edited by unwanted; 05-30-2016 at 11:40 PM. Reason: resolution
Reply With Quote
  #2  
Old 05-30-2016, 05:15 PM
Guessed's Avatar
Guessed Guessed is offline Using check boxes and moving to start of document Windows 10 Using check boxes and moving to start of document Office 2013
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,218
Guessed has much to be proud ofGuessed has much to be proud ofGuessed has much to be proud ofGuessed has much to be proud ofGuessed has much to be proud ofGuessed has much to be proud ofGuessed has much to be proud ofGuessed has much to be proud ofGuessed has much to be proud ofGuessed has much to be proud of
Default

I would be trying to design a way of tagging each checkbox with the expected value. For instance, the macro loops across all the checkboxes to check if, in each case, the value matches the tag.
I think the only way to tag a legacy form checkbox is to use its bookmark value. For instance, you could set the bookmark values of Q1a0, Q1b0, Q1c1, Q1d0 such that the last character tells you which one(s) need to be true. Then the loop can compare the checkbox values to this information and arrive at the correct/incorrect decision. For example the following code
Code:
Sub CheckCbs()
  Dim aFF As FormField, bAnswer As Boolean
  bAnswer = True
  For Each aFF In ActiveDocument.FormFields
    Debug.Print aFF.Range.Bookmarks(1).Name, Right(aFF.Range.Bookmarks(1).Name, 1) = aFF.CheckBox.Value
    bAnswer = bAnswer And Right(aFF.Range.Bookmarks(1).Name, 1) = aFF.CheckBox.Value
  Next aFF
  MsgBox bAnswer
End Sub
If you put each set of check boxes into their own table cell then you could adjust the macro to interrogate only the current cell range which would enable every checkbox macro to use the same code each time.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 05-30-2016, 11:36 PM
unwanted unwanted is offline Using check boxes and moving to start of document Windows 7 64bit Using check boxes and moving to start of document Office 2013
Novice
Using check boxes and moving to start of document
 
Join Date: May 2016
Posts: 2
unwanted is on a distinguished road
Default

Thank you for you're response.

I started trying to work out how to do that then thought i'd try what I was doing yesterday one more time and now I have it working with using an AND statement e.g.

Code:
If q1c.Value = True And q1a = False And q1b = False Then
   a1.Caption = "Correct"
Else
  a1.Caption = "Incorrect"
End If
Only shows correct if only the correct box or boxes are checked.

I tried the same thing yesterday but it kept giving me a
Compile error:
Expected: Then or GoTo

When it would get up to the first "And" but today it is accepting it fine.

Still a little more cumbersome than I would like but at least it works properly and is still much shorter than before.

My question now would be, for the reset button I list each checkbox and give it a .Value of false, would there be a way to just tell it to give every checkbox a .Value of false instead of having to list every single checkbox?

Thank you for your time.

Last edited by unwanted; 05-30-2016 at 11:39 PM. Reason: formatting
Reply With Quote
  #4  
Old 05-31-2016, 09:15 PM
Guessed's Avatar
Guessed Guessed is offline Using check boxes and moving to start of document Windows 10 Using check boxes and moving to start of document Office 2013
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,218
Guessed has much to be proud ofGuessed has much to be proud ofGuessed has much to be proud ofGuessed has much to be proud ofGuessed has much to be proud ofGuessed has much to be proud ofGuessed has much to be proud ofGuessed has much to be proud ofGuessed has much to be proud ofGuessed has much to be proud of
Default

To avoid listing every checkbox explicitly, just work out how to define the range where all the checkboxes sit. For instance, putting one question's checkboxes AND the clear button into a table cell will allow you to reuse the same code for each question.
Code:
Private Sub ClearQ_Click()
  Dim aFF As FormField
  For Each aFF In Selection.Range.Cells(1).Range.FormFields
    If aFF.Type = wdFieldFormCheckBox Then aFF.Result = False
  Next aFF
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro (or something) to run Spell Check within rich text content boxes in lock document NMBELL Word 8 12-21-2015 04:09 PM
Word2010 check boxes and plain text content control boxes in same table fcsungard Word 5 06-01-2012 01:16 AM
Using check boxes and moving to start of document Check boxes display funkyspirit Word 3 05-23-2012 08:39 AM
Using check boxes and moving to start of document Check Boxes jellyrolls Office 4 07-15-2011 08:07 PM
Problem with Developer Form -- Check Boxes and Text Boxes PCC Word 1 05-16-2009 05:22 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:56 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2022, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2022 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft