Form checkboxes: value assignment

C

colvind

Beginning Access user here. Had a question that one would think
would
be easy, but I am unable to figure out how to do it.

I have setup my table with fields. I have created a form and have
those fields linked to controls on the form. The form looks the way
I
want it to.


The structure is that of a Quality Assurance form. It's got eight
sections and a number of questions under each section. These
questions are simply checkboxes that are checked if the person being
QA'd did it or left unchecked if not.


I'd like to be able to assign point values to each checkbox question,
such as if the box is checked, it's worth 4 points; if not, it's
worth
0 points.


I'd like the form to add up the points gained in each section, then
the form's point total at the bottom of the form.


It sounds like this should be straightforward, but it probably isn't.
I'm not adverse to scripting or whathave you.


Any input would be good.


Thanks.
 
M

Mr B

Hopefully here is something that will assist you in accomplishing the results
you want. The over all concept is to create a user defined function that
will calculate the "score" based on the options selected. You will need code
to accomplish this.

I have taken a more simple approach to this. There are more involved
methods for doing this, but since you said that you do not have much
experience, this may be easier for you to understand.

First, with you form open in design view, display the properties of your
form. Select the "Event" tab. The "On Current" event will be the first
event listed. Place your cursor in the text box for this event then click
the down arrorw box at the end of the row and select "[Event Procedure]" from
the list. Next, click the button with the three dots (to the right of the
down arrow).

You will be taken to the VBA window and your cursor should be in the Current
event of the form:

Private Sub Form_Current()

End Sub

Make sure that the following two lines are at the very top of this screen:
Option Compare Database
Option Explicit

Just below these lines copy and place the following code:

'start of code below this line
Function CalcScore()
Dim lngCurScore As Long
Dim lngItem1Val As Byte
Dim lngItem2Val As Byte
Dim lngItem3Val As Byte
Dim lngItem4Val As Byte

'set a value for the first check box
If Me.chkbxItem1 = True Then
lngItem1Val = 5
Else
lngItem1Val = 0
End If
'set a value for the next check box
If Me.chkbxItem2 = True Then
lngItem2Val = 4
Else
lngItem2Val = 0
End If
'set a value for the next check box
If Me.chkbxItem3 = True Then
lngItem3Val = 2
Else
lngItem3Val = 0
End If
'set a value for the next check box
If Me.chkbxItem4 = True Then
lngItem4Val = 3
Else
lngItem4Val = 0
End If
Me.txtScore = lngItem1Val + lngItem2Val + lngItem3Val + lngItem4Val
End Function
'end of code to copy

In this code I have only placed code for 4 check boxes. However, you can
add code for additional check boxes by simply copying the "If ..." statement
for each check box and renaming the item to reflect the name of your
controls. You will also need to declare a variable (Dim lngItem4Val As Byte)
and changing the number in the variable name to reflect your item name. You
will also need to add the variable to the statement where all of the other
variables are being added together to produce the total.

Now make the following changes in the code above:
Change "chkbxItem1" to the actual name of your first check box.
Change "chkbxItem2" to the actual name of your second check box.
Change "chkbxItem3" to the actual name of your third check box.
Change "chkbxItem4" to the actual name of your fourth check box.
Change the "txtScore" to the actual name of the text box on your form where
the score is to be displayed.

Now, go back to your form. Select your first check box. In the Properties
window, locate the "After Update" event. Place your cursor in the text box
for this event then click the down arrorw box at the end of the row and
select "[Event Procedure]" from the list. Next, click the button with the
three dots (to the right of the down arrow).

You will be taken to the VBA window and your cursor should be in the After
Update event of the first Checkbox (in this example my first check box is
named "chkbxItem1") :

Private Sub chkbxItem1_AfterUpdate()

End Sub

Now place the name of the user defined function in the Sub routing for the
Current event of the form which you create when you started to create the
code above.

Private Sub Form_Current()
CalcScore
End Sub

Place the name of the user defined function in this sub routine:

Private Sub chkbxItem1_AfterUpdate()
CalcScore
End Sub

Now repeat this for each of the check boxes that you have created code for.
You should have an "If" statement for each check box that you want to
evaluate and you should have code for the AfterUpdate event for each of the
check boxes.

You will now need to look at each of the "If ..." statements and change the
value that is assigned to the variable for each check box control based on
the options selected. Looking at the first "If..." statement:

'set a value for the first check box
If Me.chkbxItem1 = True Then
'if the checkbox is true give the score 5 points
lngItem1Val = 5
Else
'if the checkbox is false add nothing to the score
lngItem1Val = 0
End If

The next line added all of the values together to create the total score:

Me.txtScore = lngItem1Val + lngItem2Val + lngItem3Val + lngItem4Val

The "txtScore" is just the name of the text box where the score is to be
dislayed. To the right of the equal sign is each of the variables being
added together to create the score.

I do hope that you will be able to use this to move along with your project.

I you need additional help, just post back here.
 
C

colvind

Hopefully here is something that will assist you in accomplishing the results
you want.  The over all concept is to create a user defined function that
will calculate the "score" based on the options selected.  You will need code
to accomplish this.

I have taken a more simple approach to this.  There are more involved
methods for doing this, but since you said that you do not have much
experience, this may be easier for you to understand.

First, with you form open in design view, display the properties of your
form.  Select the "Event" tab.  The "On Current" event will be the first
event listed.  Place your cursor in the text box for this event then click
the down arrorw box at the end of the row and select "[Event Procedure]" from
the list.  Next, click the button with the three dots (to the right of the
down arrow).  

You will be taken to the VBA window and your cursor should be in the Current
event of the form:

Private Sub Form_Current()

End Sub

Make sure that the following two lines are at the very top of this screen:
Option Compare Database
Option Explicit

Just below these lines copy and place the following code:

'start of code below this line
Function CalcScore()
Dim lngCurScore As Long
Dim lngItem1Val As Byte
Dim lngItem2Val As Byte
Dim lngItem3Val As Byte
Dim lngItem4Val As Byte

'set a value for the first check box
If Me.chkbxItem1 = True Then
    lngItem1Val = 5
Else
    lngItem1Val = 0
End If
'set a value for the next check box
If Me.chkbxItem2 = True Then
    lngItem2Val = 4
Else
    lngItem2Val = 0
End If
'set a value for the next check box
If Me.chkbxItem3 = True Then
    lngItem3Val = 2
Else
    lngItem3Val = 0
End If
'set a value for the next check box
If Me.chkbxItem4 = True Then
    lngItem4Val = 3
Else
    lngItem4Val = 0
End If
Me.txtScore = lngItem1Val + lngItem2Val + lngItem3Val + lngItem4Val
End Function
'end of code to copy

In this code I have only placed code for 4 check boxes.  However, you can
add code for additional check boxes by simply copying the "If ..." statement
for each check box and renaming the item to reflect the name of your
controls. You will also need to declare a variable (Dim lngItem4Val As Byte)
and changing the number in the variable name to reflect your item name.  You
will also need to add the variable to the statement where all of the other
variables are being added together to produce the total.

Now make the following changes in the code above:
Change "chkbxItem1" to the actual name of your first check box.
Change "chkbxItem2" to the actual name of your second check box.
Change "chkbxItem3" to the actual name of your third check box.
Change "chkbxItem4" to the actual name of your fourth check box.
Change the "txtScore" to the actual name of the text box on your form where
the score is to be displayed.

Now, go back to your form. Select your first check box.  In the Properties
window, locate the "After Update" event. Place your cursor in the text box
for this event then click the down arrorw box at the end of the row and
select "[Event Procedure]" from the list.  Next, click the button with the
three dots (to the right of the down arrow).  

You will be taken to the VBA window and your cursor should be in the After
Update event of the first Checkbox (in this example my first check box is
named "chkbxItem1") :

Private Sub chkbxItem1_AfterUpdate()

End Sub

Now place the name of the user defined function in the Sub routing for the
Current event of the form which you create when you started to create the
code above.

Private Sub Form_Current()
CalcScore
End Sub

Place the name of the user defined function in this sub routine:

Private Sub chkbxItem1_AfterUpdate()
CalcScore
End Sub

Now repeat this for each of the check boxes that you have created code for.  
You should have an "If" statement for each check box that you want to
evaluate and you should have code for the AfterUpdate event for each of the
check boxes.

You will now need to look at each of the "If ..." statements and change the
value that is assigned to the variable for each check box control based on
the options selected. Looking at the first "If..." statement:

'set a value for the first check box
If Me.chkbxItem1 = True Then
    'if the checkbox is true give the score 5 points
    lngItem1Val = 5
Else
    'if the checkbox is false add nothing to the score
    lngItem1Val = 0
End If

The next line added all of the values together to create the total score:

Me.txtScore = lngItem1Val + lngItem2Val + lngItem3Val + lngItem4Val

The "txtScore" is just the name of the text box where the score is to be
dislayed.  To the right of the equal sign is each of the variables being
added together to create the score.

I do hope that you will be able to use this to move along with your project.

I you need additional help, just post back here.
--
HTH

Mr B
askdoctoraccess dot com

Mr B,
Thanks for your help. I appreciate your explicit instructions. I
understand most of this as I was coding it.
I am getting an error when going into Form view after getting this
getting this all setup
"Compile error:
Expected End Sub"

Above you mentioned the following lines:
Private Sub Form_Current()

End Sub

Does the scripting you provided me go between these two lines? That
is, do these two lines "bookend" the code you provided?
Where does the coding you provided go in relation to these two lines?


Ling Adams:
Yes, each checkbox is worth a different numbers of points.

Thanks,
David C.
 
C

colvind

Every sub, such as

Private Sub Form_Current()

must be closed with a

End Sub

My guess is you've either left one off or you've entered or copied and pasted
one sub within another, so you've ended up with something like

Private Sub Form_Current()

Private Sub Form_Load()

End Sub

End Sub

with one sub within another, and thus have two sub headers without an EndSub
in between.

--
There's ALWAYS more than one way to skin a cat!

Answers/posts based on Access 2000/2003

Message posted via AccessMonster.comhttp://www.accessmonster.com/Uwe/Forums.aspx/access-forms/200809/1

Here's the script:
"Private Sub Form_Current()
Function CalcScore()
Dim lngCurScore As Long
Dim lngItem1Val As Byte
Dim lngItem2Val As Byte
Dim lngItem3Val As Byte
Dim lngItem4Val As Byte
Dim lngItem5Val As Byte
Dim lngItem6Val As Byte
Dim lngItem7Val As Byte
Dim lngItem8Val As Byte
Dim lngItem9Val As Byte
Dim lngItem10Val As Byte
Dim lngItem11Val As Byte
Dim lngItem12Val As Byte
Dim lngItem13Val As Byte
Dim lngItem14Val As Byte
Dim lngItem15Val As Byte
Dim lngItem16Val As Byte
Dim lngItem17Val As Byte
Dim lngItem18Val As Byte
Dim lngItem19Val As Byte
Dim lngItem20Val As Byte
Dim lngItem21Val As Byte

'set a value for the first check box
If Me.Ctl1_1 = True Then
lngItem1Val = 2
Else
lngItem1Val = 0
End If
'set a value for the next check box
If Me.Ctl1_2 = True Then
lngItem2Val = 2.5
Else
lngItem2Val = 0
End If
'set a value for the next check box
If Me.Ctl1_3 = True Then
lngItem3Val = 2
Else
lngItem3Val = 0
End If
'set a value for the next check box
If Me.Ctl1_4 = True Then
lngItem4Val = 2.5
Else
lngItem4Val = 0
End If
'set a value for the first check box
If Me.Ctl2_1 = True Then
lngItem5Val = 4
Else
lngItem5Val = 0
End If
'set a value for the next check box
If Me.Ctl2_2 = True Then
lngItem6Val = 2.5
Else
lngItem6Val = 0
End If
'set a value for the next check box
If Me.Ctl2_3 = True Then
lngItem7Val = 2
Else
lngItem7Val = 0
End If
'set a value for the first check box
If Me.Ctl3_1 = True Then
lngItem8Val = 4
Else
lngItem8Val = 0
End If
'set a value for the next check box
If Me.Ctl3_2 = True Then
lngItem9Val = 2
Else
lngItem9Val = 0
End If
'set a value for the first check box
If Me.Ctl4_1 = True Then
lngItem10Val = 12
Else
lngItem10Val = 0
End If
'set a value for the next check box
If Me.Ctl4_2 = True Then
lngItem11Val = 2
Else
lngItem11Val = 0
End If
'set a value for the first check box
If Me.Ctl5_1 = True Then
lngItem12Val = 12
Else
lngItem12Val = 0
End If
'set a value for the next check box
If Me.Ctl5_2 = True Then
lngItem13Val = 2
Else
lngItem13Val = 0
End If
'set a value for the next check box
If Me.Ctl5_3 = True Then
lngItem14Val = 2
Else
lngItem14Val = 0
End If
'set a value for the first check box
If Me.Ctl6_1 = True Then
lngItem15Val = 12
Else
lngItem15Val = 0
End If
'set a value for the next check box
If Me.Ctl6_2 = True Then
lngItem16Val = 4
Else
lngItem16Val = 0
End If
'set a value for the first check box
If Me.Ctl7_1 = True Then
lngItem17Val = 12
Else
lngItem17Val = 0
End If
'set a value for the next check box
If Me.Ctl7_2 = True Then
lngItem18Val = 2.5
Else
lngItem18Val = 0
End If
'set a value for the first check box
If Me.Ctl8_1 = True Then
lngItem19Val = 12
Else
lngItem19Val = 0
End If
'set a value for the next check box
If Me.Ctl8_2 = True Then
lngItem20Val = 2
Else
lngItem20Val = 0
End If
'set a value for the next check box
If Me.Ctl8_3 = True Then
lngItem21Val = 2
Else
lngItem21Val = 0
End If
Me.txtScore = lngItem1Val + lngItem2Val + lngItem3Val + lngItem4Val +
lngItem5Val + lngItem6Val + lngItem7Val + lngItem8Val + lngItem9Val +
lngItem10Val + lngItem11Val + lngItem12Val + lngItem13Val +
lngItem14Val + lngItem15Val + lngItem16Val + lngItem17Val +
lngItem18Val + lngItem19Val + lngItem20Val + lngItem21Val
End Function

End Sub"

I think the End Sub items are in the right place.
 
W

Walt

Mr B,

This question is right in line with a problem I'm having. I have a form
with check boxes on it but I want all the check boxes to go to one cell in a
table. I want a person to be able to select a check box from a total of 6.

For example: On my form I have check boxes 1, 2, 3 - 6. A person selects
#1 and the value gets input into the numbers cell in a table.

Is there a way to make sure only 1 check box is selected? I think I do that
with a rule but I don't know how.

Thanks

Mr B said:
Hopefully here is something that will assist you in accomplishing the results
you want. The over all concept is to create a user defined function that
will calculate the "score" based on the options selected. You will need code
to accomplish this.

I have taken a more simple approach to this. There are more involved
methods for doing this, but since you said that you do not have much
experience, this may be easier for you to understand.

First, with you form open in design view, display the properties of your
form. Select the "Event" tab. The "On Current" event will be the first
event listed. Place your cursor in the text box for this event then click
the down arrorw box at the end of the row and select "[Event Procedure]" from
the list. Next, click the button with the three dots (to the right of the
down arrow).

You will be taken to the VBA window and your cursor should be in the Current
event of the form:

Private Sub Form_Current()

End Sub

Make sure that the following two lines are at the very top of this screen:
Option Compare Database
Option Explicit

Just below these lines copy and place the following code:

'start of code below this line
Function CalcScore()
Dim lngCurScore As Long
Dim lngItem1Val As Byte
Dim lngItem2Val As Byte
Dim lngItem3Val As Byte
Dim lngItem4Val As Byte

'set a value for the first check box
If Me.chkbxItem1 = True Then
lngItem1Val = 5
Else
lngItem1Val = 0
End If
'set a value for the next check box
If Me.chkbxItem2 = True Then
lngItem2Val = 4
Else
lngItem2Val = 0
End If
'set a value for the next check box
If Me.chkbxItem3 = True Then
lngItem3Val = 2
Else
lngItem3Val = 0
End If
'set a value for the next check box
If Me.chkbxItem4 = True Then
lngItem4Val = 3
Else
lngItem4Val = 0
End If
Me.txtScore = lngItem1Val + lngItem2Val + lngItem3Val + lngItem4Val
End Function
'end of code to copy

In this code I have only placed code for 4 check boxes. However, you can
add code for additional check boxes by simply copying the "If ..." statement
for each check box and renaming the item to reflect the name of your
controls. You will also need to declare a variable (Dim lngItem4Val As Byte)
and changing the number in the variable name to reflect your item name. You
will also need to add the variable to the statement where all of the other
variables are being added together to produce the total.

Now make the following changes in the code above:
Change "chkbxItem1" to the actual name of your first check box.
Change "chkbxItem2" to the actual name of your second check box.
Change "chkbxItem3" to the actual name of your third check box.
Change "chkbxItem4" to the actual name of your fourth check box.
Change the "txtScore" to the actual name of the text box on your form where
the score is to be displayed.

Now, go back to your form. Select your first check box. In the Properties
window, locate the "After Update" event. Place your cursor in the text box
for this event then click the down arrorw box at the end of the row and
select "[Event Procedure]" from the list. Next, click the button with the
three dots (to the right of the down arrow).

You will be taken to the VBA window and your cursor should be in the After
Update event of the first Checkbox (in this example my first check box is
named "chkbxItem1") :

Private Sub chkbxItem1_AfterUpdate()

End Sub

Now place the name of the user defined function in the Sub routing for the
Current event of the form which you create when you started to create the
code above.

Private Sub Form_Current()
CalcScore
End Sub

Place the name of the user defined function in this sub routine:

Private Sub chkbxItem1_AfterUpdate()
CalcScore
End Sub

Now repeat this for each of the check boxes that you have created code for.
You should have an "If" statement for each check box that you want to
evaluate and you should have code for the AfterUpdate event for each of the
check boxes.

You will now need to look at each of the "If ..." statements and change the
value that is assigned to the variable for each check box control based on
the options selected. Looking at the first "If..." statement:

'set a value for the first check box
If Me.chkbxItem1 = True Then
'if the checkbox is true give the score 5 points
lngItem1Val = 5
Else
'if the checkbox is false add nothing to the score
lngItem1Val = 0
End If

The next line added all of the values together to create the total score:

Me.txtScore = lngItem1Val + lngItem2Val + lngItem3Val + lngItem4Val

The "txtScore" is just the name of the text box where the score is to be
dislayed. To the right of the equal sign is each of the variables being
added together to create the score.

I do hope that you will be able to use this to move along with your project.

I you need additional help, just post back here.
--
HTH

Mr B
askdoctoraccess dot com


Beginning Access user here. Had a question that one would think
would
be easy, but I am unable to figure out how to do it.

I have setup my table with fields. I have created a form and have
those fields linked to controls on the form. The form looks the way
I
want it to.


The structure is that of a Quality Assurance form. It's got eight
sections and a number of questions under each section. These
questions are simply checkboxes that are checked if the person being
QA'd did it or left unchecked if not.


I'd like to be able to assign point values to each checkbox question,
such as if the box is checked, it's worth 4 points; if not, it's
worth
0 points.


I'd like the form to add up the points gained in each section, then
the form's point total at the bottom of the form.


It sounds like this should be straightforward, but it probably isn't.
I'm not adverse to scripting or whathave you.


Any input would be good.


Thanks.
 
C

colvind

Mr B or Ling, do you see where I have a flawed line? I've gone over
it and seen no unclosed Sub.
Still getting the error.
 
M

Melody Oliver

Hi there, I found this so useful. I do have one instance that is giving me trouble. I have a form that has 5 subforms. 1 of these forms has two boxes that are checked by default. Is there a way to force it to calculate the total without clicking then reclicking the checkbox to activate calcscore funtion. I have tried before update, on enter, and on the form have also tried on enter and always have the same result. It will not calculate the total until I click the check box.

THanks!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top