Forum Discussion
Have a calling sub macro call a sub macro ByRef passing variables back and forth
There is an error is in the calling routine. If you use the Call keyword, you must surround all arguments with a single set of parentheses:
Call AccountDescr1(Accountnumber, Accountinfo)
Those parentheses do not affect the passing mechanism (i.e., passing variables ByVal vs. ByRef). Alternatively, you could omit the Call keyword and code it this way:
AccountDescr1 Accountnumber, Accountinfo
A calling procedure can force an argument to be passed ByVal (effectively) by using parentheses, as noted here. But that is by using parentheses around individual arguments, such as any of these:
Call AccountDescr1((Accountnumber), Accountinfo)
Call AccountDescr1(Accountnumber, (Accountinfo))
Call AccountDescr1((Accountnumber), (Accountinfo))
AccountDescr1 (Accountnumber), Accountinfo
AccountDescr1 Accountnumber, (Accountinfo)
AccountDescr1 (Accountnumber), (Accountinfo)
I will point out that if you are not changing the value of a parameter variable within the called procedure, it is usually preferred to use the ByVal passing mechanism (the exceptions have to do with value-type arguments that use much memory, such as very large Strings or Struct[ure]s, and this is rarely important), and you can mix ByVal and ByRef among your parameters. So in your posted examples, Accountnumber and Accountnbr could be passed ByVal.
Edit: Strings are reference types, not value types; I keep forgetting that
Hi Snowman55. I have printed out your corrections & suggestions. Thank you.
If I create a new sub macro called AccountDesc with nothing in it, & edit it in my macro list it looks like this:
Sub AccountDesc()
‘ Comment: This is called subroutine
End Sub
I can exit out of it and the name stays in my macro list & I can edit it again adding code and execute it by running it from my macro list.
If I edit the called macro and insert ByRef or ByVal variables and exit out of it the macro disappears from my macro list and I can't find it or re-edit it so it's lost.
Sub AccountDesc(ByVal Accountnumber As Integer, ByRef Accountinfo As String)
‘ Comment: This is called subroutine #1
End Sub
After inserting a ByVal and/or a ByRef variable, the macro disappears once I get out of it. Does anyone know how I can have my macro stay in my list and display it's name so I can re-edit it and execute run it? Thank you for any help.
- HansVogelaarJun 21, 2023MVP
- TommeeJun 21, 2023Copper ContributorHi Hans,
I found it within Developer under Object Browser.
Thank you so much!- SnowMan55Jun 22, 2023Bronze ContributorNote that you can also get to the procedure code quickly by right-clicking its name in calling code, and selecting "Definition" from the popup (context) menu. This also works for variable names, and for the property names and method names of most objects (if they are built-in objects, it shows them in the Object Browser).