define a function alias in VBA, possible?

private declare auto function
private declare function vba 64-bit ptrsafe
visual basic lib
vba keywords
visual basic lib keyword
vba keywords pdf
how to define a function in vba access
excel vba user defined function argument description

I am using VBA behind MS Access Say I have global methods foo1 and foo2 that gets the same arguments but do different things. I know that in C++ I can assign a function an alias. Something like: instead of:

If (term) then
   foo1 arg1, arg2, arg3
   foo2 arg1, arg2, arg3
End If

I want to write:

Var new_func = Iff (term, foo1,foo2)
new_func arg1, arg2, arg3

Can I do this on vba?

Would Run suit?

new_func = IIf(term, "foo1", "foo2")
''new_func arg1, arg2, arg3

res = Run(new_func, "a", "b", 1)

More information:

Is it Possible to Assign an Alias to a Function?, I don't understand why you would need to have a VBA alias for an VBA function. API Aliases are for referring to a function or other object in a  Declare Statement In the following example, the Alias keyword is used to provide the name of the function in advapi32.dll, GetUserNameA, that getUserName is used in place of in this example. Function getUserName is called in sub getUser, which displays the name of the current user.

If the 2 functions were implemented in 2 different class modules you could give them the same name & call them across an interface. That's about as close as you can get. (and its not very close)

'empty interface class, name: IFoo
Public Function TheFunc(i As Integer) As Long
End Function
'class clsFoo1
Implements IFoo

Private Function IFoo_TheFunc(i As Integer) As Long
MsgBox "I'm clsFoo1"
End Function
'class clsFoo2
Implements IFoo

Private Function IFoo_TheFunc(i As Integer) As Long
MsgBox "I'm clsFoo2"
End Function
'module code;
Dim myFoo As IFoo
If var = 1 Then
    Set myFoo = New clsFoo1
    Set myFoo = New clsFoo2
End If

myFoo.TheFunc 12345

Alias Clause, Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" ( ByVal lpBuffer As String, ByRef nSize As Integer) As Integer Sub  Note that these are function aliases, not quite type hints. The Left function corresponds to the hidden B_Var_Left function, while the Left$ version corresponds to the hidden B_Str_Left function. In very early versions of VBA the $ sign isn't an allowed character and the function name had to be enclosed in square brackets. In Word Basic, there were many, many more functions that returned strings that ended in $.

Try this:

The AddressOf operator.

However as noted on

"AddressOf is really a cheap hack. VB doesn't support first-class function values like most other languages, but with AddressOf is supports it halfway. Yes you can get the address of a function but you can't invoke a function by address (unless that function is a message processor and only then with the Win32 function CallWndProc). All you can do to simulate this behavior is take generic dispatch objects instead of function pointers and ensure that the objects support the necessary functions. "

Unfortunately, that's about as close as you'll get, I believe.

For more info on AddressOf, see here.

Declare statement (VBA), Syntax 2 (Function). [ Public | Private ] Declare PtrSafe FunctionnameLib "​libname" [ Alias "aliasname" ] [ ( [ arglist ] ) ] [ As type ]  The most common way to define a function in VBA is by using the Function keyword, followed by a unique function name and it may or may not carry a list of parameters and a statement with End Function keyword, which indicates the end of the function. Following is the basic syntax.

No, unfortunately, that's not possible with VBA. Would be nice, but you'll just have to stick to your first syntax.

Update: So, I stand by my original assertion that the construct proposed in the question does not exist in VBA, but Alex K. and Remou have provided usable workarounds.

Access 2007 Bible, The VBA interpreter first looks for the DLL in the System32 folder, then in the Windows as part of the LibraryName clause in the API function's declare statement. Alias “AliasName” If you wanted to call an API function by another name in your  Function procedures can be recursive; that is, they can call themselves to perform a given task. However, recursion can lead to stack overflow. The Static keyword usually isn't used with recursive Function procedures. All executable code must be in procedures. You can't define a Function procedure inside another Function, Sub, or Property

I realise this is an old question, but I suggest using STD.Types.Callback which is part of my VBA library.


sub test()
  Dim msg as STD_Types_Callback
  set msg = STD.Types.Callback.Create("Module","Module1","Msg")
  'set msg = STD.Types.Callback.Create("Object",myObject,"Msg")

  DoMessage(msg, "hello world") '=> "hello world"
end sub

function Msg(message)
  Msgbox message
end function

function DoMessage(a as object, b as string)
end function

Microsoft Excel 2013 Programming by Example with VBA, XML, and ASP, Aliases make it possible to call the function or subroutine by any name you want, use the alias keyword like this: Declare Function lopen Lib "kernel32" Alias _  For a list of the main built-in VBA functions, with examples, see the VBA Functions page. User-Defined VBA Function & Sub Procedures In Excel Visual Basic, a set of commands to perform a specific task is placed into a procedure, which can be a Function procedure or a Sub procedure (also known as functions and subroutines).

Programming Excel with VBA: A Practical Real-World Guide, By definition, a Declare statement is a way to grant access to a DLL function inside procedure: Declare [Function][Sub] PublicName Lib "LibName" [Alias "​alias"] possible declarations in other modules, it is usual to give the function call an  Note. Declare statements with the PtrSafe keyword is the recommended syntax. Declare statements that include PtrSafe work correctly in the VBA version 7 development environment on both 32-bit and 64-bit platforms only after all data types in the Declare statement (parameters and return values) that need to store 64-bit quantities are updated to use LongLong for 64-bit integrals or LongPtr for

Excel 2003 VBA Programmer's Reference, User-interface functions (such as managing windows, the keyboard, name Lib "​libname" [Alias "aliasname"] [([arglist])] [Public | Private] Declare Function name  Writing the Function in VBA. To write the function, go to the VB Editor and begin with the word Function. This will be followed by the function name, which you designate. In my example, I've called it FiscalYear. It can be any function name that isn't already in use. Next, open the parenthesis and specify the arguments.

Excel Functions Arguments & Descriptions, sFunctionDescription = "This is my new user defined function" It is possible to overcome this restriction by using an ancient XLM macro sheet. register a function residing within any system dll, using an alias name which is identical in a worksheet, it works fine and returns the result of the VBA function. The winuser.h header defines DialogBoxParam as an alias which automatically selects the ANSI or Unicode version of this function based on the definition of the UNICODE preprocessor constant. Mixing usage of the encoding-neutral alias with code that not encoding-neutral can lead to mismatches that result in compilation or runtime errors.

  • I personally don't really like Run much, but yes, at least this solution will work, so you get my vote.
  • Interfaces in VBA? That's new to me... I mean, yes, you can use existing interfaces, but you can't define your own and make classes implement them, which would be needed here. Or have I missed something?
  • Sure, If its a class module with no implementation VBA can treat it as an interface & any other class may implement its public members, example updated.
  • Very clever. Thanks for the update! Not doing much in VBA anymore these days, but this trick just might come in handy one day. Still seems a little convoluted for OP.
  • Trivia: Implementing Interfaces is not supported in VBA 5 (Office 97), but is in VBA 6 (Office 2000 to 2007 I think) and presumably VBA 7 (Office 2010)
  • Clever idea, but I don't (yet) see how AdressOf will help here, since it can't be used to call a function within VBA.
  • It was more a 'try this to see the limits of VBA' link, unfortunately. AddressOf and calling same-named functions across an interface (if other poster is correct) are about as close as you'll get.
  • As a terrible hack you can actually use CallWindowProc to call any VBA addressof pointer so long as the signature matches & you use 0 for the hwnd. You can even pass As String.