UserControls, subforms and Events
What I found missing in Access – is UserControls. Fortunately – Access has subforms, and using subform you can do almost the same. But how to notify main form that something was changed in subform? In VB this task is solved with Events. What about Access? Here a sample how we can use Events in Access.
Make a new form, that will act as subform, say frmDateSelector. Declare event in its class module:
Public Event DateSelected(datFrom As Date, datTo As Date)
Add 2 textboxes for date entry, and add following code:
Private Sub txtFrom_AfterUpdate()
RaiseEvent DateSelected(Me.txtFrom, Me.txtTo)
End Sub
Private Sub txtTo_AfterUpdate()
RaiseEvent DateSelected(Me.txtFrom, Me.txtTo)
End Sub
So, once data was changed – we raise DateSelected event. How to catch this in a main form?
In main form class module declare subform object with events:
Private WithEvents ds As Form_frmDateSelector
Initiate it in main form Open event:
Private Sub Form_Open(Cancel As Integer)
Set ds = Me.frmDateSelector.Form
End Sub
That’s all!
Now, like in VB, you can select “ds” in top left combo of main form calss module, select DateSelected in right combo – and Access will insert an event procedure:
Private Sub ds_DateSelected(datFrom As Date, datTo As Date)
End Sub
You just need to modify your form’s rowsource or applyfilter – and that is!
Now you can use your frmDateSelector in other forms, similar to VB UserControl
Make a new form, that will act as subform, say frmDateSelector. Declare event in its class module:
Public Event DateSelected(datFrom As Date, datTo As Date)
Add 2 textboxes for date entry, and add following code:
Private Sub txtFrom_AfterUpdate()
RaiseEvent DateSelected(Me.txtFrom, Me.txtTo)
End Sub
Private Sub txtTo_AfterUpdate()
RaiseEvent DateSelected(Me.txtFrom, Me.txtTo)
End Sub
So, once data was changed – we raise DateSelected event. How to catch this in a main form?
In main form class module declare subform object with events:
Private WithEvents ds As Form_frmDateSelector
Initiate it in main form Open event:
Private Sub Form_Open(Cancel As Integer)
Set ds = Me.frmDateSelector.Form
End Sub
That’s all!
Now, like in VB, you can select “ds” in top left combo of main form calss module, select DateSelected in right combo – and Access will insert an event procedure:
Private Sub ds_DateSelected(datFrom As Date, datTo As Date)
End Sub
You just need to modify your form’s rowsource or applyfilter – and that is!
Now you can use your frmDateSelector in other forms, similar to VB UserControl
3 Comments:
Wow! Useful stuff!!!
Alex, thanks a lot!
my pleasure!
Brilliant.
I needed to trap the afterupdate event on my subform and this works a treat!
Post a Comment
<< Home