By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,251 Members | 2,656 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,251 IT Pros & Developers. It's quick & easy.

Call function from macro using RunCode

P: 16
I have a function tha I have written which I am trying to run from a macro using Runcode. The macro that I am trying to call the function from is the OnClick event of a command button. Basically, I want to print a different page of my reort dependent upon the value in the text control "Text28." But when I press the button, the code is function is not running and I can't figure out why. Any suggestions would be very much appreciated! Here is what I have for the function:
Expand|Select|Wrap|Line Numbers
  1. Public Function PrintOut()
  2.     If Text28 = "aaa" Then
  3.         DoCmd.PrintOut acPages, 2, 2, , 1
  4.     ElseIf Text28 = "bbb" Then
  5.         DoCmd.PrintOut acPages, 3, 3, , 1
  6.     ElseIf Text28 = "ccc" Then
  7.         DoCmd.PrintOut acPages, 4, 4, , 1
  8.     ElseIf Text28 = "ddd" Then
  9.         DoCmd.PrintOut acPages, 5, 5, , 1
  10. End If
  11. End Function
2 Weeks Ago #1
Share this Question
Share on Google+
5 Replies


Nauticalgent
P: 92
I noticed that the function you posted is public. Is it in a separate module than the form that has the command button?

If so, you will need to pass an argument to the function, in this case the value of Text28. Depending on your answer, I have some suggestion to make your code more efficient.
1 Week Ago #2

P: 16
Yes it is in a separate module
1 Week Ago #3

Nauticalgent
P: 92
Try:
Expand|Select|Wrap|Line Numbers
  1. Public Function PrintOut(strTest As String)
  2.     Select Case strTest
  3.         Case "aaa"
  4.             DoCmd.PrintOut acPages, 2, 2, , 1
  5.         Case "bbb"
  6.             DoCmd.PrintOut acPages, 3, 3, , 1
  7.         Case "ccc"
  8.             DoCmd.PrintOut acPages, 4, 4, , 1
  9.         Case "ddd"
  10.             DoCmd.PrintOut acPages, 5, 5, , 1
  11.         Case Else
  12.     End Select
  13. End Function
Then on the OnClick() event of your button, use this code:
Expand|Select|Wrap|Line Numbers
  1. Call PrintOut(Me.Text28)
  2.  
1 Week Ago #4

P: 16
Thank you so much for your help
1 Week Ago #5

Nauticalgent
P: 92
You're very welcome, happy to help.
1 Week Ago #6

Post your reply

Sign in to post your reply or Sign up for a free account.