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

Call function from macro using RunCode

P: 20
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
Aug 7 '19 #1
Share this Question
Share on Google+
5 Replies


Nauticalgent
P: 93
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.
Aug 11 '19 #2

P: 20
Yes it is in a separate module
Aug 13 '19 #3

Nauticalgent
P: 93
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.  
Aug 13 '19 #4

P: 20
Thank you so much for your help
Aug 13 '19 #5

Nauticalgent
P: 93
You're very welcome, happy to help.
Aug 13 '19 #6

Post your reply

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