Link to home
Start Free TrialLog in
Avatar of taplin
taplin

asked on

WithEvents in Access 2000

I am TRYING to raise an event from a subform to the parent form.  Is this possible?  One would think it is.

In my "sub-form", I have an event declaration:

   Public Event ValueChanged(NewValue as Variant)

In the code of my sub-form, I raise this event:

   RaiseEvent ValueChanged("SomeNewValue")

I'm pretty sure I'm all set with that side of the problem.  Where I get a little mixed up is in the parent form, where the subform is placed.

I create an object variable of type Form_TestSubForm... my form is named TestSubForm, and the code module for it is named Form_TestSubForm, and this is my only choice in the dropdown list when I'm typing:

   Private Withevents TestForm As Form_TestSubForm

So, this seems to be okay, because the event list shows the ValueChanged event for my TestForm object.  Here's where I run into a problem.  What do I set TestForm equal to... i.e. I thought I had it when I said "Set TestForm = TestSubForm.Form", because it didn't give me an error... but it didn't work, either.

HELP!

Jay
Avatar of hotbudare
hotbudare

I'd like you to be more specific as to what do you want to do
Avatar of Jim Dettman (EE MVE)
Jay,

  Your withevents is in a class module correct?  It won't work in a regular code module.

Jim.
Avatar of taplin

ASKER

JDettman:

I am not terribly familiar with Access programming.  I am more of a VB programmer.  I was under the impression that the form's code module would be a class module, similar to Visual Basic's form code being a class itself.  I don't get an error declaring or raising events from the code... wouldn't Access warn me?

hotbudare:

I am trying to create a subform that is unbound that hosts a calendar control.  Whenever the user changes the value of the calendar (changes dates), I want to raise an event from that form.  The reason I am choosing forms is because I am having serious window drawing issues with just the calendar control on the main form.  Suffice it to say I want it - need it - to function this way.

I am aware I do not have to raise events, but I would prefer to, as this is a much better form of communication than calling out to a procedure in the parent via referencing the parent.

Help!!!!

Thanks,
Jay
Jay,

  Yes, form and report modules are class modules.  It just wasn't clear to me where you had you weithevents.

  I haven't played around with this, but try:

Private Withevents TestForm As Form

 instead of:

Private Withevents TestForm As Form_TestSubForm

and see if that works.  I'm not sure that it will. Subforms in Access are a bit strange.  It's like there not even there.  For example, screen.activeform will return the main form name if executed from code in a subform.  And a subform will not show in the forms collection either.

Let me know if that works.  If not, I'll try and set something up here.
Jim.
You have to declare a calendar control variable type, not a form. The change event is associated to this control and NOT to the form itself. OTH, your code is creating and then "sinking" the event and, in your case you dont' need to, since this event does already exists.
Now, if I understood you right, I'm suposing you want your MAIN form to respond to a change made in the calendar control. Please correct me if I'm wrong.
I added a calendar control "CalControl" to form F1 and then added F1 to form F2 as CalSubForm. The coded in this commetn belongs to F2.
The problem is that this code fails in the first line of the Form_Load event procedure.
I've used this very same approach before but never on ActiveX controls.

I hope someone else could help. I'm done for today. It's late here and I'm very tired, but I'll give another shot tomorrow.

"EOC*************************
Option Compare Database
Option Explicit

Dim WithEvents Cal As MSACAL.Calendar

Private Sub Cal_AfterUpdate()

    Beep
    MsgBox "Calendar was changed!"
End Sub

Private Sub Form_Load()

    Set Cal = Me.CalSubForm.Form.Controls("CalControl")
    Cal.AfterUpdate = "[Event procedure]"
End Sub

Private Sub Form_Unload(Cancel As Integer)

    On Error Resume Next
    Set Cal = Nothing
End Sub
"EOC*************************

HTH/EQTA
T.S.U. Mario Osorio
Punto Fijo, Falcon, Venezuela

Avatar of taplin

ASKER

hotbudare:

I appreciate your advice... for the sake of my own pride, however, I do understand what you wrote... I had to put the calendar on a form because the calendar control itself wasn't drawing properly when overlaying subforms and such.  I'm trying to use it as a "dropdown" of a sort, similar to the way True DBInput controls work.

So, what I was doing was capturing the events from the control, and then hoping to use RaiseEvent to raise an even from within the calendar control event's itself.

Thank you for your help so far...
Still the same process though, no?  He's created a custom event at form level and creating a sink for the form, so he should get notification on the parent form.

Granted, going for an event on the control itself is more logical, but I don't understand why what he has now would not work.

Jay:
  Do you care if it's a form level event or one for the control?

Jim.
Avatar of taplin

ASKER

Okay... I'll try to explain this again... I guess I haven't been clear.

Initially, I placed the calendar control on my form, which has other controls on it, and also a subform.  Whenever a particular control got focus, I showed the calendar control just below it, so that the user could select the date graphically.  I would get the events from the calendar control to update the text box, and when the user double-clicked the calendar or placed focus on another control, the calendar would disappear.

This worked exactly right (as would be expected), except that the calendar control did not draw correctly if overlaying the subform that was on that form.  The border of the control would appear under the subform, whereas the control contents would appear above the subform.  I did not test to see if this just happens when interacting with subforms or not, but regardless, I needed this calendar control to "pop up" partially above the subform.

So, to rectify this situation, I decided to place the calendar control on its own form, then place that form onto the main form I was working with.  In a sense, this form with the calendar on it would be a sub-form and also behave as a control.

The only problem is, I can't seem to get any code-defined events from this form with the calendar.  I have other solutions, such as going to VB or VC++ and developing a control that would draw properly (or just place the calendar on top of a real window control, such as a picture box, and wrap it all up as a new activex control), but I would prefer to not resort to external solutions (external to Access, I mean).  This database could be accessed by many different computers, and I really don't want to deal with the issue of deploying ActiveX controls to each client machine.

Thanks for your help guys... trust me, I am pretty well skilled in VB and such... I understand the COM philosophies and understand how controls and forms interact... I really feel like I'm just missing some little thing about how Access handles code-defined events.

Really, all I want to know is: if I raise an event in form-code, and that form is placed onto another form as a sub-form, HOW DO I TRAP FOR THAT EVENT?

Thanks,
Jay
ASKER CERTIFIED SOLUTION
Avatar of Jim Dettman (EE MVE)
Jim Dettman (EE MVE)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of taplin

ASKER

Jim,

Thanks for the information... yeah, I was beginning to gather that Access forms are handled completely different than VB forms (which, in turn, are handled completely different than Windows - C++ - forms).

The reason I prefer the method of raising events as compared to calling a method of the parent is that it forces the parent to be listening, rather than allowing the parent to be listening.  It is far more "reusable" to raise an event, rather than call a method directly.  Using your method, I would have to put an error handler in just in case the parent form doesn't have that particular procedure.  Along those lines, raising events works better because you can see from a simple dropdown what communication the control/form is using; that way, the programmer doesn't have to have knowledge, read documentation, or look at the control/form's code to identify what procedures are being called to communicate.

Anyway, to clarify my statement ""In the code of my sub-form, I raise this event:  RaiseEvent ValueChanged("SomeNewValue")", what I mean is that the calendar control fires an event (DateChanged, I believe).  In that event, I raise my event, ValueChanged.  This is just a method of passing the new value to whatever parent container is listening.

Anyway, I am beginning to think that I may have to resort to calling a procedure directly via the Parent.ProcedureName syntax, as much as I didn't want to.

If you have any thoughts, do let me know!  And again, thank you very much.

Jay
Jay,

 I'm hoping to have a little more free time this afternoo to play with this and re-visit it.  Which calendar control is it that you working with?

Jim.
Avatar of taplin

ASKER

I am using the MSCAL.OCX, the Calendar Control 8.0.

I appreciate your help.  Honestly, I haven't had any time to play with this either.  I spent about ten minutes with the WithEvents issue, and realizing that I didn't know enough about MS Access' way of doing things, I decided to post a question.  I think I'll be going back to it this afternoon also.

Thank you for your help!
This question appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you. You must tell the participants why you wish to do this, and allow for Expert response.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question. Again, you must tell the other participants why you wish to do this.

For special handling needs, please post a zero point question in the link below, include the question QID/link.
https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this Help Desk link for Member Guidelines, Member Agreement and the Question/Answer process:  Click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues.
https://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

To view your open questions, please click the following link(s) and keep them all current with updates.
https://www.experts-exchange.com/questions/Q.20175079.html
https://www.experts-exchange.com/questions/Q.20180074.html
https://www.experts-exchange.com/questions/Q.20182134.html


PLEASE DO NOT AWARD THE POINTS TO ME.  
 
------------>  EXPERTS:
 
Please leave any comments regarding this question here on closing recommendations if this item remains inactive another three days.
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange


P.S.  For year 2000 questions, special attention is needed to ensure the first correct response is awarded, since they are not in the comment date order, but rather in Member ID order.
for taplin

It's time to clean up this TA, so I will leave a recommendation in Community Support that this question is:
 - Answered by: JDettman  
Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

Nic;o)
Per recommendation, force-accepted by
Netminder
CS Moderator