Solved

WithEvents in Access 2000

Posted on 2001-09-12
15
717 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:taplin
  • 5
  • 5
  • 2
  • +3
15 Comments
 
LVL 2

Expert Comment

by:hotbudare
Comment Utility
I'd like you to be more specific as to what do you want to do
0
 
LVL 57

Expert Comment

by:Jim Dettman (Microsoft MVP/ EE MVE)
Comment Utility
Jay,

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

Jim.
0
 
LVL 2

Author Comment

by:taplin
Comment Utility
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
0
 
LVL 57

Expert Comment

by:Jim Dettman (Microsoft MVP/ EE MVE)
Comment Utility
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.
0
 
LVL 2

Expert Comment

by:hotbudare
Comment Utility
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

0
 
LVL 2

Author Comment

by:taplin
Comment Utility
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...
0
 
LVL 57

Expert Comment

by:Jim Dettman (Microsoft MVP/ EE MVE)
Comment Utility
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.
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 2

Author Comment

by:taplin
Comment Utility
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
0
 
LVL 57

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 200 total points
Comment Utility
Jay,

  Sorry I'm not more up on the withevents.  I've never really used it all that much.  But from what I have done with it, I can see nothing wrong with what you were trying to do, other then I would think that you would want to trap an event off the control directly.  I guess one point I should get clear is what you meant by:

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

  RaiseEvent ValueChanged("SomeNewValue")
"

 in your original question.  I was assuming that this code is in the AfterUpdate event of the control.

  I think also that you need to step back and look at the process.  While I'm not saying you can't do what your trying to do, there are simpler ways to do it. For example, from the calender control subform, you can call a method (sub procedure) in the parent just by doing:

  Parent.<method here>

  from any event procedure of the calendar control.  Creating a event sink gets you out of doing the above, but that's all it will do.

  Also keep in mind that Access is not VB and handles it forms entirely different.  For example, controls don't have handles, there are no control arrays, most ActiveX controls won't work because of a limited OLE container, etc.  I can appreciate that you are probably a very good programmer, but I'm just saying that you can't compare Access to C++ or VB.

 I can walk you through the steps for linking directly to the control if you wish, but I see nothing wrong with what you were doing at the form level.

Jim.

0
 
LVL 2

Author Comment

by:taplin
Comment Utility
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
0
 
LVL 57

Expert Comment

by:Jim Dettman (Microsoft MVP/ EE MVE)
Comment Utility
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.
0
 
LVL 2

Author Comment

by:taplin
Comment Utility
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!
0
 
LVL 1

Expert Comment

by:Moondancer
Comment Utility
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.
http://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.
http://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.
http://www.experts-exchange.com/questions/Q.20175079.html
http://www.experts-exchange.com/questions/Q.20180074.html
http://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.
0
 
LVL 54

Expert Comment

by:nico5038
Comment Utility
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)
0
 
LVL 5

Expert Comment

by:Netminder
Comment Utility
Per recommendation, force-accepted by
Netminder
CS Moderator
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now