Custom Events (1 Viewer)

Frothingslosh

Premier Pale Stale Ale
Local time
Today, 16:30
Joined
Oct 17, 2012
Messages
3,276
Okay, I'm trying to use the calendar sub form I found HERE http://www.access-programmers.co.uk/forums/showthread.php?t=218962 in post #4.

My problem is that I've not worked with custom events before, and can't figure out how to catch when DateChanged triggers, and how to access the new date it generates.

I've placed the calendar form into a main form as a subform, included the suggested code (and used the indicated textbox name to make sure everything worked), but I can't find any indication that the event actually fires. The event handler listed in the post (
Code:
Private Sub Form_Load()
Set calendarForm = Me.frmCalendar.Form
End Sub

and

Private Sub calendarForm_DateChanged(newDate As Date)
txtApptmntDate = newDate
End Sub
) never actually triggers (I checked using a breakpoint). I know the calendar form itself is working properly, and it even has a Value property from which I can pull the selected date as long as the system is aware that a date has been selected. (I've tested via a command button that pulls the updated value manually.) It's determining when that happens that's stumping me, and any help would be much appreciated.

And the built-in date picker really isn't an option - as far as I'm aware, it only becomes available when a textbox is bound to a date field, and this is for a form for the user to select an end date for a 7-day report, meaning no bound field to work with.
 

sneuberg

AWF VIP
Local time
Today, 13:30
Joined
Oct 17, 2014
Messages
3,506
And the built-in date picker really isn't an option - as far as I'm aware, it only becomes available when a textbox is bound to a date field, and this is for a form for the user to select an end date for a 7-day report, meaning no bound field to work with.

You can get a date picker in an unbound field as I've done in the attached database by changing the Format in the textbox's properties to a date format.

Unfortunately so far as I know the Date Picker doesn't fire any events. :mad:
 

Attachments

  • DatePickerInBoundForm.accdb
    352 KB · Views: 72

Frothingslosh

Premier Pale Stale Ale
Local time
Today, 16:30
Joined
Oct 17, 2012
Messages
3,276
Huh. Didn't work in the database I'm working on. Might have something to do with it having been converted from 2003 to 2007 format.

I'll keep fiddling with that, because I'd much rather use the date picker.

That said, if you know any good references for using custom events, I'd love to read up on how to use them for the future. I know that you have to include a parameter when you define them, but everything I've seen says that they absolutely do not actually PASS that parameter (or do anything ELSE with them, from what I can see). Still, I may have uses in the future for custom events that simply flag certain occurences.

Anyway, thanks!
 

missinglinq

AWF VIP
Local time
Today, 16:30
Joined
Jun 20, 2003
Messages
6,423
You can get a date picker in an unbound field as I've done in the attached database by changing the Format in the textbox's properties to a date format.

That's correct...don't know why the OP is having trouble with that!

Unfortunately so far as I know the Date Picker doesn't fire any events

That's not correct! One of the advantages of the new (as of v2007) DatePicker, is that using it is the same as physically entering the date, as opposed entering the date via code, as you had to do with a ActiveX Calendar or with a calendar based on a Form.

Linq ;0)>
 

Frothingslosh

Premier Pale Stale Ale
Local time
Today, 16:30
Joined
Oct 17, 2012
Messages
3,276
I can't tell you why I was having trouble either. I *KEPT* having trouble, even after this post, until I created a new empty front end and imported all the forms, queries, reports, macros, and modules. THAT fixed the freaking problem with the date picker not showing up.

And yes, I know the advantages of the date picker - that's why I preferred it in the first place. The event, however, in my experience, isn't fired by the date picker itself, but the data change in the field. (Yes, it's semantics, as they're basically the same thing.)

Still doesn't answer my original question, though, about how to catch a custom event fired in a subform, even if we've long since fixed the actual problem I was having. :)
 

missinglinq

AWF VIP
Local time
Today, 16:30
Joined
Jun 20, 2003
Messages
6,423
I *KEPT* having trouble, even after this post, until I created a new empty front end and imported all the forms, queries, reports, macros, and modules. THAT fixed the freaking problem with the date picker not showing up.

That pretty much shows that you had corruption, at some level, as doing that is the first defense when corruption is suspected!


Still doesn't answer my original question, though, about how to catch a custom event fired in a subform

Have no idea, there! Been developing in Access 16 years, now, and never even considered using a custom event! To be honest, cannot even imagine a valid need for one, in Access, but maybe someone else, out there can, and can help you!

Linq ;0)>
 

Frothingslosh

Premier Pale Stale Ale
Local time
Today, 16:30
Joined
Oct 17, 2012
Messages
3,276
That pretty much shows that you had corruption, at some level, as doing that is the first defense when corruption is suspected!

Actually, the first thing I did was backup, compact, and repair, which didn't do diddly-squat. Then I checked references, then tried creating an entirely new form. It was after beating my head against that for a while and asking here that I had the idea to just re-create the front end entirely.

Have no idea, there! Been developing in Access 16 years, now, and never even considered using a custom event! To be honest, cannot even imagine a valid need for one, in Access, but maybe someone else, out there can, and can help you!

Linq ;0)>

Young'un! I started working with it back in 95 with Access 2, and this was the first time I've ever seen a need to work with them. There's a reason I don't actually request help here very often. :p

I can see the potential of custom events, though, which is why I'm still interested in them despite my issue being resolved. I know full well that most things I can do with built-in events and working with the main and sub forms directly as a response, but more knowledge never hurts! Especially when a custom event may allow for a simpler, faster, or even just more elegant solution to an issue.
 

sneuberg

AWF VIP
Local time
Today, 13:30
Joined
Oct 17, 2014
Messages
3,506
I didn't even know there were such things as custom events. But I just watched 15 Minute YouTube Video
and I while it was interesting, no useful application for them occurred to me. I suppose it's good to know they exist.

Anyway, there are a few steps to get these setup. Maybe if you watched this video it might lead you in the right direction.
 

Frothingslosh

Premier Pale Stale Ale
Local time
Today, 16:30
Joined
Oct 17, 2012
Messages
3,276
Oh, I will. I just encountered the whole issue this morning at work, and they kind of frown on me watching videos on YouTube, even for something like this. :D
 

jdraw

Super Moderator
Staff member
Local time
Today, 16:30
Joined
Jan 23, 2006
Messages
15,379
Last edited:

Frothingslosh

Premier Pale Stale Ale
Local time
Today, 16:30
Joined
Oct 17, 2012
Messages
3,276
I'd suggest it, but tomorrow is my last day there. On Monday, I actually start working at one of the largest health insurance companies in the US as an Access programmer.
 

jdraw

Super Moderator
Staff member
Local time
Today, 16:30
Joined
Jan 23, 2006
Messages
15,379
Good stuff!
The links I suggested are an excellent reference/refresher.
Good luck in the new job.
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 06:30
Joined
Jan 20, 2009
Messages
12,851
I'd love to read up on how to use [custom events] for the future. I know that you have to include a parameter when you define them, but everything I've seen says that they absolutely do not actually PASS that parameter (or do anything ELSE with them, from what I can see). Still, I may have uses in the future for custom events that simply flag certain occurences.

Custom events are often misunderstood but are not much different from any others. They can include parameters that will pass with the event to the listener. The following code example connects a custom event with a parameter on one form to another form.

Declare the Event as Public in the declarations section of the form that raises the event.
Include and a line in a procedure or function to raise the custom event where you require it to be raised.

Code:
Option Explicit
Public Event CustomEvent(ByVal strParam As String)
  
Private Sub SomeProcedure()
     RaiseEvent CustomEvent("some text")
End Sub
Declare an object variable in the declarations section on the listening object module (Form, Report or Class Module).
Set the variable to the instance of the object, usually in the Form_Load (or Class_Initialize procedure in the case of a custom class object).
Define the actions of the subform event procedure in the listening object module.

Code:
Option Explicit
Private WithEvents EventForm As Form_formobjectname
  
Private Sub Form_Load()
     Set EventForm = Forms!forminstancename
End Sub
  
Private Sub EventForm_CustomEvent(ByVal strParam As String)
     Debug.Print strParam
End Sub
In the case of a subform raising the event the Set line for the form instance name would be:
Code:
Set EventForm = Forms!mainform.subformcontrol.Form
That is all there is to it.
 

Frothingslosh

Premier Pale Stale Ale
Local time
Today, 16:30
Joined
Oct 17, 2012
Messages
3,276
Yep, the video linked above cleared things up. My biggest issue had been that my initial source was, well, WRONG about how to code them. :)
 

Users who are viewing this thread

Top Bottom