Module help

G

Gazza

Firstly sorry for posting this twice i posted in the wrong section originally
and 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
 
S

Stuart McCall

Gazza said:
Firstly sorry for posting this twice i posted in the wrong section
originally
and 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

The reason nothing happens is this: the parameter KeyCode passed to the
Form_KeyDown procedure is passed by reference. This means (among other
things) that if you modify the value inside the procedure it is 'passed
back' to the calling code (in this case internal Access code). Because you
don't pass KeyCode along to your exitform sub, (a) your sub doesn't receive
the value, and (b) when you modify KeyCode, it's not passed back to Access.

So, to fix this, simply pass KeyCode by reference to your exitform routine:

Public Sub exitform(KeyCode As Integer)

and in the Form_KeyDown procedure, call it like this:

Call exitform(KeyCode)

(I'm assuming that exitform2 is a typo)
 
T

Tom Wickerath

Gareth,

Please do not multipost in the future. You posted this in the
microsoft.public.forms 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
__________________________________________
 
D

Dymondjack

Gazza,

I would first off suggest using a better naming scheme if you are going to
start playing with modules. Besides that, see below for a basic rundown on
how one would set this up:

In a Standard Module (I would name modKeyEval):

Public Function fCloseIfEscapeKeyHeld(sForm As String)
If KeyCode = vbKeyEscape Then DoCmd.Close acForm, sForm
End Function



In your Form_KeyDown event:

Private Sub Form_KeyDown(KeyCode as Integer, Shift As Integer)
fCloseIfEscapeKeyIsHeld Me.Name
End Sub



Or......
Do it all one line for each form and don't worry about using functions:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyEscape Then DoCmd.Close acForm, Me.Name
End Sub

HTH
-jack
 
G

Gazza

Ok i think im in way over my head here cause i have no idea what anyone means
with there answers.

Im new to access and especially modules and thought id give it a try as they
are supposed to make life easier but i think it may be easier typing it out
on seperate forms.

I thought you just wrote the code inside the module and then called it in
the form but obviously its not quite that easy

can anyone point me to some good resources on the web to explain them.

Thanks
Gareth
 
D

David W. Fenton

I thought you just wrote the code inside the module and then
called it in the form but obviously its not quite that easy

You may be having problems with scope. Some general points:

1. in a public module, if you don't declare scope, it's PUBLIC:

Sub Test()

That's equivalent to the usual:

Public Sub Test()

The only time a subroutine (or function) in a standalone module is
not going to be accessible from elsewhere is if you explicitly
declare it as private:

Private Sub Test()

That will be visible only to code called within the module itself.

2. code in a form module works the same -- it is public, but not in
the same way as code in modules. If have

Sub Test()

in a form module, it is not accessible when the form is not opened
in non-design view, and it has to be fully specified to be called
from outside the form's own module:

Call Forms!MyForm.Test()

The usual practice for form modules is to write private code, using
the Private keyword.

3. code that refers to data on a form cannot use the Me keyword if
it is in a standalone module. In a form module:

Me!MyField

refers to the field or control MyField on the form the module is
attached to. In a standalone module, the Me keyword has no meaning
(a class modules are different, and also have more complicated scope
rules, but those are not really relevant to a beginner!), so you
have to specify the form in full:

Forms!MyForm!MyField

Alternatively, you write your code in your public module so that you
can pass it a form reference in the subroutine's parameters. For
instance:

Public Sub Test(frm As Form)
MsgBox frm!MyField
End Sub

will work just fine if called from anywhere, as long as you pass it
a valid reference to an open form. If called from a public module or
the module of a form other than the one you're dealing with, you'd
call it thus:

Call Test(Forms!MyForm)

From within MyForm, you can call it this way:

Call Test(Me)

To summarize:

1. code within a standalone module is public, but knows nothing
about the Me keyword.

2. code within a form module recognizes Me, but can only be called
from outside the form when:
a. the form is open
b. the sub is public
c. the call is fully qualified with the form name

3. code in a standalone module can work with the contents of a form
if you fully specify the form or if you pass a valid reference to
the form into your code.

Don't hesitate to ask if you need more explanation.
 
G

Gazza

Thank you david for the explanation.

I think i understand where im going wrong so ill go and have a play around
with the modules and see how i get on (no doubt ill be back).

Thanks again
Gareth
 

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