views:

65

answers:

2

I have form that displays information on a project. Most of the fields on the form are bound to a Project table and display and update as bound fields do. However, I also have 10 checkboxes that come from a ProjectAudience table. Only if the box on the form is checked, is there is a matching record in the table. Therefor, I need to insert and delete records as these boxes are checked and unchecked.

I can do it at the checkbox level with the AfterUpdate event, but that would require 10 different events. I looking to do it at the Form level by deleting all records in the ProjectAudience table for that project and adding in the checked ones.

Basically, I'm looking for the opposite of the Form_Current event that will fire when either the record navigation button is fired or the form is closed. I'm trying to avoid either writing my own navigation buttons or adding a "SAVE" button to the form that the user must press. I want something automatic that will update this table when the user navigates away from the record.

Based on a comment below: Any combination of boxes from none to all can be checked not just a single box. Therefore, it is possible that I would wipe out all records and not insert any back... or add 10 records if every box was checked. (Also, I am using Microsoft Access 2003)

+1  A: 

Have you considered adding these checkboxes to an Option Group and using events from that group?

EDIT re Comment, Alternative approach, do not use an option group, but add code to an event line for all relevant options.

You do not need code for 10 different events, you can set the event line for, say, On Click to the name of a function, let us say:

On Click  : =TestMe()

Event Line

You then need a function:

Function TestMe()
    MsgBox "You clicked " & Screen.ActiveControl.Name
End Function
Remou
I may have coded the group incorrectly, but when I tried it, I was only able to check 1 box at a time. In the case above (and I will edit my original post), checking every combination of boxes from none to all is a possibility. So there could be anything from 0 records to 10.
Count Boxer
I have added a note.
Remou
But if I put my 10 check boxes in an option group, then I can only select 1 of the 10 boxes. I need the ability to check multiple boxes up to all 10. It isn't an event question at that point, the option group won't let me multi-select.
Count Boxer
I should have made it clear that this is an alternative approach. Select all ten option boxes and choose an event to set to the name of a function, On Click : =TestMe(), for example.
Remou
That is what I ended up doing. The AfterUpdate event for each checkbox calls a single function and passes in which checkbox they are (1, 2, etc up to 10) and if they are being checked or unchecked (0 or -1). The function will then insert or delete the appropriate record. I don't like this approach because it requires 10 AfterUpdate events. Yes, all 10 call the same function, but it is UGLY code. I wanted one AfterUpdate event to call one function.
Count Boxer
Did you not wish to set the afterupdate line in the property sheet to the name of a function, as I illustrated? You do not need 10 events, just refer to a function on the line.
Remou
@Remou: I'm confused about your "alternative" because Screen.ActiveControl returns the option group when the option group has the focus, not the control within the option group. I would say use your function as you suggest, but no within an option group. If that's what you meant, perhaps you could clarify.
David-W-Fenton
Alternative as in alternative to the option group. It was not clear from the original question whether more than one option could be selected. An option group is still viable for a person in a similar situation who only wishes to select one option.
Remou
A: 

You can use the BeforeUpdate method. However, if only the checkboxes have been changed this event will not fire since the record to which the form is bound didn't change.

I would advise to link events to the checkboxes. Create a function UpdateCheckbox(CheckboxID as integer) that does what you want and put this in the 'OnClick' event from the ckeckboxes: =UpdateCheckbox(1). Change the 1 for the different checkboxes.

birger