Module help

G

Gazza

Firstly sorry for the long post.

I have three forms for adding different items to three different tables and
I seem to need the same code to do similar things on the forms.

I was thinking of creating modules for the code that is the same so I don’t
have to write it on all three forms. The problem is I am not quite sure where
to start.

I have created a module called exitform and added the following code :

Public Sub exitform2()


Select Case KeyCode
Case vbKeyEscape
KeyCode = 0
DoCmd.Close

End Select

End Sub

I have then added the following code to the forms keydown event

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Call exitform
End Sub

But nothing seems to happen apart from the tab at the top with the form name
flashes once, so obviously there is an error in my code somewhere but I
haven`t a clue where it could be as the module code works (I had the code in
the on keydown event originally but moved it to a module as many forms will
use this code so I hopefully save myself a lot of typing)


Any help would be much appreciated

Thanks
Gareth
 
G

Gina Whipp

The first thing I see if the Procedure is called 'exitform2' but you are
calling 'exitform'.
 
T

Tom Wickerath

Gareth,

Please do not multipost in the future. You posted this in the
modules.dao.vba group as well. It is rarely necessary to post a question in
more than one group, but if you feel the need to do so, please cross-post
instead. A cross-post involves posting to more than one group at the same
time. Since you posted using the web interface, you would click on the
Advanced Options link in the lower right corner, when creating a new post, in
order to expose a text box that allows you to specify additional groups to
cross-post to.

http://www.microsoft.com/wn3/locales/help/help_en-us.htm#TipsForPosting


Tom Wickerath
Microsoft Access MVP
http://www.accessmvp.com/TWickerath/
http://www.access.qbuilt.com/html/expert_contributors.html
__________________________________________
 
R

RoyVidar

Gazza said:
Firstly sorry for the long post.

I have three forms for adding different items to three different
tables and I seem to need the same code to do similar things on the
forms.

I was thinking of creating modules for the code that is the same so I
don’t have to write it on all three forms. The problem is I am not
quite sure where to start.

I have created a module called exitform and added the following code
:

Public Sub exitform2()


Select Case KeyCode
Case vbKeyEscape
KeyCode = 0
DoCmd.Close

End Select

End Sub

I have then added the following code to the forms keydown event

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Call exitform
End Sub

But nothing seems to happen apart from the tab at the top with the
form name flashes once, so obviously there is an error in my code
somewhere but I haven`t a clue where it could be as the module code
works (I had the code in the on keydown event originally but moved
it to a module as many forms will use this code so I hopefully save
myself a lot of typing)


Any help would be much appreciated

Thanks
Gareth

In addition to what Gina Whipp says, you're calling exitform, while
the routine is called exitform2, there's at least one mor thing to
consider - you need to pass the keycode

Public Sub exitform2(MyKeyCode As Integer)

Select Case MyKeyCode
Case vbKeyEscape
MyKeyCode = 0
DoCmd.Close

End Select

End Sub

Without specifying ByVal/ByRef, the variable is passed by reference,
which means that the changes to the variable is passed back/reflected
in the calling sub.

In addition, I'm also a fan of being explicit about the close method,
in case the focus shifts, so I would probably also pass the form
name

So, a bit more explicit, one could for instance use

Public Sub exitform2(ByRef MyKeyCode As Integer, _
ByVal TheForm As String)

Select Case MyKeyCode
Case vbKeyEscape
MyKeyCode = 0
DoCmd.Close, acForm, TheForm

End Select

End Sub
 
R

RoyVidar

RoyVidar said:
Public Sub exitform2(ByRef MyKeyCode As Integer, _
ByVal TheForm As String)

Select Case MyKeyCode
Case vbKeyEscape
MyKeyCode = 0
DoCmd.Close, acForm, TheForm

End Select

End Sub

And then call the routine with;

Call exitform(KeyCode, Me.Name)
 
R

RoyVidar

RoyVidar said:
Public Sub exitform2(ByRef MyKeyCode As Integer, _
ByVal TheForm As String)

Select Case MyKeyCode
Case vbKeyEscape
MyKeyCode = 0
DoCmd.Close, acForm, TheForm

End Select

End Sub

This is to be the day of typos, I think - call the routine with;

Call exitform2(KeyCode, Me.Name)
 

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

Top