Problem using WithEvents

P

Paul

Hi,

I have a class module for automatically resizing controls on a form. The
class is called clsFormResize

The class module has a declaration: 'Private WithEvents frm as Form' and has
some code on frm_Resize which them modifies the positions/sizes of the
controls.

I set a module level variable to the class in a form that needs to use the
resize functionality, and I set a few details in the load event of the form.

The resize event correctly sinks (terminology?) to the class *only* if the
form itself has a Private Sub Form_Resize() block of code (even if there is
no actually code, just the sub, end sub pair).

This is not a huge problem, it is just annoying that the Form_Resize code
must be present in the form as i) you have to add that piece of code and ii)
you have to remember never to delete it out, even though there is not code
there to execute.

Is this normal behaviour? Is there a way around the problem?

Thanks

Paul
 
G

Graeme Richardson

Hi Paul, the form requires the "On Resize" property to contain the value
"[Event Procedure]".

Creating the procedure Form_Resize() is peforming this task for you.
Note that you will need to place at least a comment in the empty procedure
to prevent it disappearing on compiling the project.

Hope this helps, Graeme.
 
M

Marshall Barton

Paul said:
I have a class module for automatically resizing controls on a form. The
class is called clsFormResize

The class module has a declaration: 'Private WithEvents frm as Form' and has
some code on frm_Resize which them modifies the positions/sizes of the
controls.

I set a module level variable to the class in a form that needs to use the
resize functionality, and I set a few details in the load event of the form.

The resize event correctly sinks (terminology?) to the class *only* if the
form itself has a Private Sub Form_Resize() block of code (even if there is
no actually code, just the sub, end sub pair).

This is not a huge problem, it is just annoying that the Form_Resize code
must be present in the form as i) you have to add that piece of code and ii)
you have to remember never to delete it out, even though there is not code
there to execute.

Is this normal behaviour? Is there a way around the problem?


I don't think you need an actual event procedure. Just make
sure the form's OnResize property contains [Event Procedure]
 
S

Stephen Lebans

I always set the desired Form's event handler to "[Event Procedure]"
from within the code I use to sink the desired event.

--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.


Marshall Barton said:
Paul said:
I have a class module for automatically resizing controls on a form. The
class is called clsFormResize

The class module has a declaration: 'Private WithEvents frm as Form' and has
some code on frm_Resize which them modifies the positions/sizes of the
controls.

I set a module level variable to the class in a form that needs to use the
resize functionality, and I set a few details in the load event of the form.

The resize event correctly sinks (terminology?) to the class *only* if the
form itself has a Private Sub Form_Resize() block of code (even if there is
no actually code, just the sub, end sub pair).

This is not a huge problem, it is just annoying that the Form_Resize code
must be present in the form as i) you have to add that piece of code and ii)
you have to remember never to delete it out, even though there is not code
there to execute.

Is this normal behaviour? Is there a way around the problem?


I don't think you need an actual event procedure. Just make
sure the form's OnResize property contains [Event Procedure]
 
P

Paul

That sounds useful - can you post the code that does that? Is [Event
Procedure] just a text value?

It's a shame that Access does not quite sink the events properly - does
anyone know if this is resolved in Access 03?


Stephen Lebans said:
I always set the desired Form's event handler to "[Event Procedure]"
from within the code I use to sink the desired event.

--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.


Marshall Barton said:
Paul said:
I have a class module for automatically resizing controls on a form. The
class is called clsFormResize

The class module has a declaration: 'Private WithEvents frm as Form' and has
some code on frm_Resize which them modifies the positions/sizes of the
controls.

I set a module level variable to the class in a form that needs to use the
resize functionality, and I set a few details in the load event of the form.

The resize event correctly sinks (terminology?) to the class *only* if the
form itself has a Private Sub Form_Resize() block of code (even if there is
no actually code, just the sub, end sub pair).

This is not a huge problem, it is just annoying that the Form_Resize code
must be present in the form as i) you have to add that piece of code and ii)
you have to remember never to delete it out, even though there is not code
there to execute.

Is this normal behaviour? Is there a way around the problem?


I don't think you need an actual event procedure. Just make
sure the form's OnResize property contains [Event Procedure]
 
S

Stephen Lebans

From the Utilities sample MDB here:
http://www.lebans.com/utilities.htm

Option Compare Database
Option Explicit

Private WithEvents mcmdPrev As Access.CommandButton

Public Property Set ButtonPrev(pctrlButtonPrev As Access.CommandButton)
Set mcmdPrev = pctrlButtonPrev
mcmdPrev.OnClick = "[Event Procedure]"
End Property

Private Sub Class_Initialize()
Set mcmdPrev = Nothing
End Sub

Private Sub mcmdPrev_Click()
MsgBox "mcmdPrev_Click() happened!"
End Sub

--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.


Paul said:
That sounds useful - can you post the code that does that? Is [Event
Procedure] just a text value?

It's a shame that Access does not quite sink the events properly - does
anyone know if this is resolved in Access 03?


"Stephen Lebans"
wrote in message news:%[email protected]...
I always set the desired Form's event handler to "[Event Procedure]"
from within the code I use to sink the desired event.

--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.


Marshall Barton said:
Paul wrote:
I have a class module for automatically resizing controls on a
form.
The
class is called clsFormResize

The class module has a declaration: 'Private WithEvents frm as
Form'
and has
some code on frm_Resize which them modifies the positions/sizes
of
the
controls.

I set a module level variable to the class in a form that needs
to
use the
resize functionality, and I set a few details in the load event
of
the form.
The resize event correctly sinks (terminology?) to the class
*only*
if the
form itself has a Private Sub Form_Resize() block of code (even
if
there is
no actually code, just the sub, end sub pair).

This is not a huge problem, it is just annoying that the
Form_Resize
code
must be present in the form as i) you have to add that piece of
code
and ii)
you have to remember never to delete it out, even though there is
not
code
there to execute.

Is this normal behaviour? Is there a way around the problem?


I don't think you need an actual event procedure. Just make
sure the form's OnResize property contains [Event Procedure]
 
M

Malcolm Cook

Paul,

I too have found that this seems to be the required way with access.

However, there is a function to help you write the code. Look up -
CreateEventProc

You might use it as I did to write something such as attached snippet.

Good luck,

Malcolm Cook - (e-mail address removed)
Database Applications Manager - Bioinformatics
Stowers Institute for Medical Research - Kansas City, MO USA


Public Function CreateStubEventProcIfNeeded(mdl As Access.Module,
strEventName As String, strEventPfx As String, Optional strProcBody As
String = "")
' Purpose: creates for a form given its module, mdl,
' a blank (stub) event procedure (if it does not already exist), such as is
needed to sink
' event procedures.
' Usage: 'assuming your form is currently closed
' Dim strFormName as string
' Dim frm As Access.Form
' Dim mdl As Access.Module
' strFormName = "<your form name>"
' DoCmd.OpenForm strFormName, acDesign 'ensure it is open in design mode
' Set frm = Forms(strFormName)
' Set mdl = frm.Module
' with frm
' .OnOpen = CreateStubEventProcIfNeeded(mdl, "Open", "Form", "Set
mSAFForm = SAFFormOpen(Me.Form)")
' .AfterDelConfirm = CreateStubEventProcIfNeeded(mdl, "AfterDelConfirm",
"Form")
' .AfterInsert = CreateStubEventProcIfNeeded(mdl, "AfterInsert", "Form")
' .AfterUpdate = CreateStubEventProcIfNeeded(mdl, "AfterUpdate", "Form")
' etc...

CreateStubEventProcIfNeeded = "[Event Procedure]"
Dim startline As Long, startcolumn As Long, endline As Long, endcolumn As
Long
startline = 0
Dim lngReturn As Long
'mdl.Find " " & strEventPfx & "_" & strEventName & " ", startline,
startcolumn, endline, endcolumn
mdl.Find "Sub " & strEventPfx & "_" & strEventName, startline,
startcolumn, endline, endcolumn
If (startline = 0) Then
' Add event procedure.
lngReturn = mdl.CreateEventProc(strEventName, strEventPfx)
' Insert text into body of procedure.
If strProcBody = "" Then
strProcBody = "'Stub Auto Generated - Do Not Delete"
End If
mdl.InsertLines lngReturn + 1, vbTab & strProcBody
End If
End Function
 
M

Malcolm Cook

After having read Stephen Lebans posting, I must agree, it is not necessary
to have an actually event procedure for the event to fire. You must only
have "[Event Procedure]" as the value of the OnXXX form property. And, you
can do this effectively at the same point as "frm" gets set in
clsFormResize.

Cheers,

Malcolm
 
P

Paul

Thanks Malcolm,

I agree that Stephen Labans' solution appears best as I believe that as well
as being shorter, it will work if the project is compiled to an mde whereas
I suspect writing an event stub into a module will not.

Thanks everyone for your usefult tips.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top