Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling an event in a subform from another form

Status
Not open for further replies.

LanceyD

Programmer
Jun 19, 2008
4
BR
Hello

I would like to call the AfterUpdate event in a subform from the code module of another form.

Main Form Name: frmEditOccupiedBeds
Container Control Holding Subform: ctlSubform
Subform Name: subfrmEditBeds
Subform Control Name: txtFirstNight

If you are wondering what I am doing, here is a quick rundown. A date picker is passed variables ( optional, so I can use the date picker anywhere ) such as form name, control name etc when initiated by the GotFocus Event of a bound textbox. The date is then inserted into the textbox as a date picker normally would. The afterupdate event of the textbox does not fire because it is populated by VBA, so I want to trigger the correct event for the correct subform based on the passed variables from the datepicker, dependent on whether there are variables. Therefore could someone please tell me the correct structure of the line of code I need to write.

Thanks in advance.
 
This is a bad design. You should move the code to a standard module. But if you really need to call an event on a subform.


call forms("form name").controls("subform control name").form.ProcedureName

and the event procedure needs to be public

example using northwind and the current event on the customer orders subform on the form customer orders

Public Sub test()
Call Forms("Customer Orders").Controls("Customer Orders Subform1").Form.Form_Current
End Sub
 
Hi MajP

I have to go into my office to try this, but first let me say that I describe myself as a programmer in the loosest possible way. I realise that it is bad design, but here's why. I haven't really coded for years as I now own a backpacker's hostel in Brazil. We had a local thief manage to get in so I have built a simple guest registration system that holds photgraphs of our guests and which beds they are in on the dates they are staying in specific beds. They show a card to eception with their bed number and the person on reception searches the bed number and up pops a photo of the card holder for the day the search is made. If the person holding the card is not the same as the photo then we have a "please explain" situation, unless it is a mistake by one of my staff, who all scream "not me". The bed history is displayed in a continuous bound subform ( The guest name is in the main form ) and needs to be editable, as guests often shorten or lengthen their stay, or move beds. To enforce better vigilance by my staff, each change is logged in tracking table, but given the afterupdate event can't be triggered by VBA in a bound form control, it needs to be triggered by the date picker. The code that calls the afterupdate event is in a normal code module, but that routine is called from the date picker.

If you have a better idea on how I can do this, please let me know, as I might sell this on to a few other hostel owners who take pictures with a camera, rename them, then manually save them to a hard drive. My program collects passport information and saves the photo with a concatenation of their name, nationality and passport number ( These three should give you a unique ID, but photos are saved to a folder structure of Year-Month-Day of their initial check-in, so unlikely to have 2 people of same name and nationality anyway )

Thanks
 

On the subform you have an after update event with code in it. Probably something like.

private sub someControl_afterUpdate()
a bunch of code
end sub

just have that event call a sub routine in a standard module instead:

private sub someControl_afterUpdate()
call yourCommonRoutine(someparameter, someotherparameter...)
end sub

Then in the standard moduel

public sub yourCommonRoutine(someParam as variant,somOtherParam as somethingElse)
your code here
end sub

now your date picker calls
your date picker event and the form event both call the same common subroutine.
 
The last line should read

both the forms after update event and the date pickers event call the same common procedure in a standard module. Maybe a third or fourth event can call the same procedure.
 
Hi MajP

The afterupdate event in the subform does not get triggered when the date is inserted into the control on it by the date picker via VBA. This goes to the crux of my problem, as the event only triggers if I manually enter the date.
 
Code:
The afterupdate event in the subform does not get triggered when the date is inserted into the control on it by the date picker via VBA
[code]
Yes, I fully understand that.

Most times you can choose to type a date in manually or use the date picker / calendar control to set the date. In this example I have a calendar control and a textbox that I synch up.  If I select a date in the textbox then it synchs in the calendar, if I click in the calendar the date appears in the textbox. But either event, the calendar controls click event or or the text box's after update event (manually) triggers a common sub routine.

[code]
Private Sub txtBxReservationDate_AfterUpdate()
    If Not IsNull(txtBxReservationDate) Then
        ocxCalendar.Value = txtBxReservationDate.Value
    Else
        ocxCalendar.Value = Date
    End If
    'since the user typed a date in
    call someCommonRoutine
End Sub

Private Sub txtBxReservationDate_Enter()
    If Not IsNull(txtBxReservationDate) Then
        ocxCalendar.Value = txtBxReservationDate.Value
    Else
        ocxCalendar.Value = Date
    End If
    'just used to synch the two controls
End Sub


Private Sub ocxCalendar_Click()
    txtBxReservationDate.Value = ocxCalendar.Value
    txtBxReservationDate.SetFocus
    call someCommonEvent        
End Sub
 
I understand where you are coming from. At the moment the calendar control is opened by the textbox´s GotFocus event, and am using the calendar control´s click event to fire the event in the subform, which I think is what you have shown above. The "commonevent" routine you mention is held in a separate module.

Unfortunately, I´ll have to get back to this on Sunday, as I´m heading down the coast to the beach now and am leaving the laptop here.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top