#1
|
|||
|
|||
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 Last edited by unwanted; 05-30-2016 at 11:40 PM. Reason: resolution |
#2
|
||||
|
||||
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
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
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 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 |
#4
|
||||
|
||||
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 |
|
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 |
Check boxes display | funkyspirit | Word | 3 | 05-23-2012 08:39 AM |
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 |