Checkbox to fill in textbox - code newbie tearing hair out

BarryMK

4 strings are enough
Local time
Today, 17:05
Joined
Oct 15, 2002
Messages
1,350
I've built several Access databases for my employers but remain clueless re VBA code so would greatly appreciate any help. I’m sure this is a simple call but not to me.

Using Access 97 on form "abc" I have a check box called "ToBeChipped". As soon as this is checked I would like it to fill in a text box called "AdminFee" on the same form (this forms part of a column of fields calculating various costs). If the check box is True I would like the sum of £5 entered into the text box, If False the sum is £0.

How should I code this and most importantly, where should I place the code, please?
 
In the "OnChange" of "ToBeChipped":

If ToBeChipped = True then
AdminFee.Text = 5
Else
AdminFee.Text = 0
End if
 
Thanks Newman for your response. Now the bad news – On my version of Access 97 in the checkbox properties dialog box I don’t have the “on change” option. There are: before/after updates, on enter/exit, on click/dbl click, mouse and key events only.

I tried pasting the syntax directly into the before and after update fields in the dialog box and got: MS Access can’t find the macro “If to be chipped etc”

Putting the syntax into the event procedure called from before update thus:

Private Sub ToBeChipped_BeforeUpdate(Cancel As Integer)
If tobechipped = True Then
AdminFee.Text = 5
Else
AdminFee.Text = 0
End If

End Sub

gets a compile error box “variable not defined” no doubt I should dim something but I’m not sure what or how.
 
Why not just set the control source of AdminFee to =Iif([tobechipped] = True ,5,0))
 
Checkbox to fill in textbox - code newbie tearing hair

Thanks Rich

The problem is that the figure shown in the AdminFee textbox forms part of a calculation totalling costs as shown below

=[KenFeesIncl]+[Fine]+[AdminFee]


Kenfees Incl is the result of a calculation in a hidden field on the form, Fine is a default amount and Admin fee can be 5 or 15. I need the trigger for AdminFee to be generated from one of 3 checkboxes oneof which is ToBeChipped
 
Hi

The value of a checkBox is 0 or -1

So in the OnClick event of the ToBeChipped checkBox type this

If Me.ToBeChipped = -1 Then
Me.AdminFee.Value = 5
Else
Me.AdminFee.Value = 0
End If

Hope this helps

Col
 
How will the AdminFee become 0 if ToBeChipped is never clicked?
Your code needs to be in the Form OnCurrent event, in the after update events of your checkboxes just put Form_Current or Call Form_Current
 
Very true - thanks for spotting that Rich - I forgot that bit (its me age you know!!!)

Put the code in the OnCurrent event of the form as well so that it checks the value of the tickbox.

Col
:cool:
 
I'm overwhelmed with the amount of help here so many thanks to one and all, however I still get the compile error "Can't find the macro", as before, with this latest code.
 
I've just replaced the last pieces of code (to ensure I didn't mess up) and still get the compile error when checking the checkbox on an existing record where it is unchecked. However if I uncheck an existing checked record AdminFee changes to 0 and when I check it again the 5 reappears. must be getting close!
 
Col Presumably Access thinks the if statement is a macro?
 
In the OnCurrentEvent procedure on the Form put
If Me.ToBeChipped = True Then
Me.AdminFee = 5
Else
Me.AdminFee = 0
End If
In the AfterUpdate event procedure of your check box just type
Form_Current
you only need one copy of the code
 
Tried that Rich the check box works but nothing happens. Aaaargh
 
Your code behind the form should look something like
Private Sub Form_Current()
On Error GoTo Form_Current_Err
If Me.ToBeChipped = True Then
Me.AdminFee = 5
Else
Me.AdminFee = 0
End If

Form_Current_Exit:
Exit Sub

Form_Current_Err:
MsgBox Error$
Resume Form_Current_Exit

End Sub

Private Sub MyContol_Click()
Form_Current
End Sub
does your code look like this?
 
Rich I'll check that later - I'm in a meeting for the next hour or so. Cheers
 
Bingo! No it didn't look like that but it does now and it works. Thanks a bunch Rich and everyone else for your assistance.
 

Users who are viewing this thread

Back
Top Bottom