473,320 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to run a module using a menu bar

jpr
Hello,

I have created menu bars for my access application and now would like
to transfer many pieces of code in modules so that they can run using
macros. I beleive this is the only way I can make run my code from the
menu bar. Correct?

I am having problems with the modules since I have never used them and
would like some help.

Here is a sample of code I now have on a push button in a form:

Dim mydb As DAO.Database, MyRs As DAO.Recordset
Dim strCode As String
Dim strFilter As String
Dim stDocName As String
Dim stLinkCriteria As String

Set mydb = CurrentDb
Set MyRs = mydb.OpenRecordset("master")

stDocName = "MASTER"

stLinkCriteria = "[SSN]=" & "'" & Me![SSN] & "'"
DoCmd.OpenForm stDocName, , , stLinkCriteria
If Not IsNull(DLookup("[SSN]", "TEMPLATES", "[SSN] = '" & Me!SSN &
"'")) Then
Forms("EFORMS").Visible = False
Else
If IsNull(DLookup("[SSN]", "TEMPLATES", "[SSN] = '" & Me!SSN & "'"))
Then
DoCmd.OpenQuery ("Appendtemplates")
End If
End If

If Not IsNull(DLookup("[SSN]", "195", "[SSN] = '" & Me!SSN & "'")) Then
Forms("EFORMS").Visible = False
Else
If IsNull(DLookup("[SSN]", "195", "[SSN] = '" & Me!SSN & "'")) Then
DoCmd.OpenQuery ("Append195")
End If
End If

If Not IsNull(DLookup("[SSN]", "795", "[SSN] = '" & Me!SSN & "'")) Then
Forms("EFORMS").Visible = False
Else
If IsNull(DLookup("[SSN]", "795", "[SSN] = '" & Me!SSN & "'")) Then
DoCmd.OpenQuery ("Append795")
End If
End If

If Not IsNull(DLookup("[SSN]", "21", "[SSN] = '" & Me!SSN & "'")) Then
Forms("EFORMS").Visible = False
Else
If IsNull(DLookup("[SSN]", "21", "[SSN] = '" & Me!SSN & "'")) Then
DoCmd.OpenQuery ("Appendssa21tax")

End If
End If
Forms!eforms.lstPreInterview.Value = Null
DoCmd.Close
DoCmd.OpenForm ("Eforms")
DoCmd.RunMacro ("CloseEforms")
DoCmd.OpenForm ("Eforms")

End Sub

What I need is to add this code to a module, and make it run using a
manu bar (I assume via macro). Thanks.

Aug 26 '06 #1
4 8651
jpr wrote:
Hello,

I have created menu bars for my access application and now would like
to transfer many pieces of code in modules so that they can run using
macros. I beleive this is the only way I can make run my code from the
menu bar. Correct?
Incorrect. 8)
What I need is to add this code to a module, and make it run using a
manu bar (I assume via macro). Thanks.
Generally, code that sits in standard modules is available all the time,
regardless of what forms are opened. The sub you have with your button
on a form is in the form module for that form and is only available when
your form is open.

First create a standard module and save it. Cut your procedure from the
form module it is in and paste it to the standard module and save your mdb.

To be able to run your procedure from a menu bar control, it must be a
function. Change it from Sub Whatever to:

Public Function fProcedure()

<blah blah>

End Function

To make this run from your menu, go to the customize dialog, right click
the menu control and choose properties.

In the on action control, put in:

=fProcedure()

You can also include arguments as well.

Hope this gets you going.
--
Tim http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "Whatcha doin?" - Ditto "TIM-MAY!!" - Me
Aug 27 '06 #2
jpr
thanks for your help. I have followed your advise but still getting an
error message:
"The expression you have entered has a function that MS Office can't
fint"

Any idea of what is wrong based on my code? THanks.
Tim Marshall wrote:
jpr wrote:
Hello,

I have created menu bars for my access application and now would like
to transfer many pieces of code in modules so that they can run using
macros. I beleive this is the only way I can make run my code from the
menu bar. Correct?

Incorrect. 8)
What I need is to add this code to a module, and make it run using a
manu bar (I assume via macro). Thanks.

Generally, code that sits in standard modules is available all the time,
regardless of what forms are opened. The sub you have with your button
on a form is in the form module for that form and is only available when
your form is open.

First create a standard module and save it. Cut your procedure from the
form module it is in and paste it to the standard module and save your mdb.

To be able to run your procedure from a menu bar control, it must be a
function. Change it from Sub Whatever to:

Public Function fProcedure()

<blah blah>

End Function

To make this run from your menu, go to the customize dialog, right click
the menu control and choose properties.

In the on action control, put in:

=fProcedure()

You can also include arguments as well.

Hope this gets you going.
--
Tim http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "Whatcha doin?" - Ditto "TIM-MAY!!" - Me
Aug 28 '06 #3
jpr wrote:
thanks for your help. I have followed your advise but still getting an
error message:
"The expression you have entered has a function that MS Office can't
fint"
The function (do NOT use a sub, it MUST BE a function) is spelt
incorrectly, or is not public or is not saved in a standard module.
Also, the = sign and the closing parenthesis must be used.

So, the following will cause the error you mentioned if entered into the
on action control:

fProcedure() <WRONG>

fProcedure <WRONG>

=fProcedure <WRONG>

You must enter:

=fProcedure() <CORRECT>

--
Tim http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "Whatcha doin?" - Ditto "TIM-MAY!!" - Me
Aug 28 '06 #4
Just for the record: you can't run a Module. A Module is a container for
code, and the code in a Module is organized into Procedures (Sub or
Function) and a General Area that can contain Variables and Constants.

You can run a Procedure that exists in a Module. You should NOT give a
Module and a Procedure the same name.

Larry Linson
Microsoft Access MVP
"jpr" <jp***@tin.itwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
Hello,

I have created menu bars for my access application and now would like
to transfer many pieces of code in modules so that they can run using
macros. I beleive this is the only way I can make run my code from the
menu bar. Correct?

I am having problems with the modules since I have never used them and
would like some help.

Here is a sample of code I now have on a push button in a form:

Dim mydb As DAO.Database, MyRs As DAO.Recordset
Dim strCode As String
Dim strFilter As String
Dim stDocName As String
Dim stLinkCriteria As String

Set mydb = CurrentDb
Set MyRs = mydb.OpenRecordset("master")

stDocName = "MASTER"

stLinkCriteria = "[SSN]=" & "'" & Me![SSN] & "'"
DoCmd.OpenForm stDocName, , , stLinkCriteria
If Not IsNull(DLookup("[SSN]", "TEMPLATES", "[SSN] = '" & Me!SSN &
"'")) Then
Forms("EFORMS").Visible = False
Else
If IsNull(DLookup("[SSN]", "TEMPLATES", "[SSN] = '" & Me!SSN & "'"))
Then
DoCmd.OpenQuery ("Appendtemplates")
End If
End If

If Not IsNull(DLookup("[SSN]", "195", "[SSN] = '" & Me!SSN & "'")) Then
Forms("EFORMS").Visible = False
Else
If IsNull(DLookup("[SSN]", "195", "[SSN] = '" & Me!SSN & "'")) Then
DoCmd.OpenQuery ("Append195")
End If
End If

If Not IsNull(DLookup("[SSN]", "795", "[SSN] = '" & Me!SSN & "'")) Then
Forms("EFORMS").Visible = False
Else
If IsNull(DLookup("[SSN]", "795", "[SSN] = '" & Me!SSN & "'")) Then
DoCmd.OpenQuery ("Append795")
End If
End If

If Not IsNull(DLookup("[SSN]", "21", "[SSN] = '" & Me!SSN & "'")) Then
Forms("EFORMS").Visible = False
Else
If IsNull(DLookup("[SSN]", "21", "[SSN] = '" & Me!SSN & "'")) Then
DoCmd.OpenQuery ("Appendssa21tax")

End If
End If
Forms!eforms.lstPreInterview.Value = Null
DoCmd.Close
DoCmd.OpenForm ("Eforms")
DoCmd.RunMacro ("CloseEforms")
DoCmd.OpenForm ("Eforms")

End Sub

What I need is to add this code to a module, and make it run using a
manu bar (I assume via macro). Thanks.

Aug 28 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Marcus Schneider | last post by:
I use PythonWin on WinXP. Every time I change a module, I have to leave PythonWin and re enter to make it notice I have made changes. I guess this is not the normal way to do that.. do I have to...
2
by: Marc Shapiro | last post by:
I am relatively new to python (I have used it on and off for a few small projects over the last few years) so I imagine that what I am trying to do has already been done, but practical experience,...
1
by: ulf | last post by:
Hello, After I got a FileNotFoundException in my real life CSharp code, I nailed it down to the following line: System.EnterpriseServices.ResourcePool rp = new...
0
by: Edward | last post by:
Access 2k FE/BE My application allows users to enter employee hours against rotas (i.e. who is rostered on, and when). They further want a function to output a report showing the number of...
1
by: THDWWSIJQUOK | last post by:
I have (or should I say "had") a module which I invested quite a few hours of work into. I was poking around trying to rename the module and instead wound up removing the darn thing. It's been a...
7
by: allyn44 | last post by:
Hello--I have the code below attached to a command button--it opens an error log form: DoCmd.OpenForm "ReportError" Forms!reporterror!RecordID = Me!RecID Forms!reporterror!PatientID = Me!PatId...
3
by: Henry Wu | last post by:
Hi, I have a MDIForm with a menu name mnuVideo that opens a childForm. My question is a newbie question, I was wondering how can I manipulate the mnuVideo that is in the MDIForm from the...
6
by: Shane Saunders | last post by:
I have a menu option that loads a form and displays infomation. if you go to main form and try to load something else from that same menu, it does a check to see if the form in exist and then...
1
by: bobano | last post by:
Hi everyone, I am writing a POP3 Client program in Perl. You connect to a POP3 Server and have a running conversation with the mail server using commands from the RFC 1939 Post Office Protocol....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.