News, links, downloads, tips and tricks on Microsoft Access and related

About Me Search
Name:Alex Dybenko

Location:Moscow, Russia

Friday, May 19, 2006

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


Blogger Raider said...

Wow! Useful stuff!!!
Alex, thanks a lot!

3:09 PM  
Blogger Alex Dybenko said...

my pleasure!

3:10 PM  
Anonymous Vera. said...

I needed to trap the afterupdate event on my subform and this works a treat!

2:58 PM  

Post a Comment

<< Home