Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2003
    Posts
    104

    Unanswered: Function Overload??? [MS Access 2002 VBA]

    Hi All
    I want to write a function called UpdateStatusBar and I want it to have two different parameter lists (i.e an overloaded function). Here are the two definitions I would like

    Private Sub UpdateStatusBar(message As String)

    and

    Private Sub UpdateStatusBar(User As Boolean, found As Boolean, num As Integer, name As String)


    However, When i do this, microsoft Access complains at run time saying the macro has an ambiguous function "UpdateStatusBar".

    I am just curious, Is ther a way to do function overloading in Vb/VBA?

    thanx beforehand


    PS: I don't know if this thread should be here or in the Vb forum. I am sorry if it is in the wrong forum

  2. #2
    Join Date
    Nov 2003
    Posts
    1,487
    You can not have two of the same named procedures within the same module. In your case, looking at what the procedure might do, I would say that it would be best to have it as a single Public Function within a module rather than a local Sub procedure. Of course, this all depends upon what type of status bar you are using and how information is formated within that particular bar. For example, a typilcal text box bar of your own design, you may want to use something like:

    Code:
    Public Function UpdateStatusBar(Msg As String) As String
       '.... Function Code......
       '...............................
    End Function
    Usage:
    myStatusBar = UpdateStatusBar(Msg)

    At the moment this appears to be rather useless since you could of have accomplished the very same thing by simply doing:

    myStatusBar = Msg

    Where a function can becomes more beneficial (for the formating aspect of things) is when more 'optional' parameters are supplied to the function and then formated accordingly to suit your status bar. For example, a typical function may appear as such:

    Code:
    Public Function UpdateStatusBar(Optional Msg As String, Optional User As Boolean, _
             Optional found As Boolean, Optional num As Integer, Optional name As String) As String
       Dim ReturnMsg As String
       If Msg = "" Then
          'Default Message if no Msg parameter is passed to Function
           ReturnMsg = "Welcome to my little program"
       Else
           ReturnMsg = Msg
       End If
       '..........Process other Parameters here - ie:.........
       If name <> "" Then
          ReturnMsg = ReturnMsg & " " & name
       End If
       '.............etc...etc....etc...........
       '.............
        UpdateStatusBar = ReturnMsg
    End Function
    Useage:
    myStatusBar = UpdateStatusBar(, , , ,"Bill Gates' Barber")

    In the function example above, all the parameters are 'Optional'. You decide which ones you want to fill at any given time within your program code. How you format the any one or all the parameter information passed to the function is entirely up to you. And of course, placing your function into a module (located in the Modules section of your database window) as public, makes the function globaly available anywhere within your database program.

    FF

  3. #3
    Join Date
    Nov 2003
    Posts
    104
    I don't understand the difference between Sub and Function. I am used to "functions" from C/C++/Java.

    Also, The status bar I am using is actually just a label box, and i update the Caption property of it as necessary. I have found this to be the easiest way to do it so I did it that way!

  4. #4
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740
    main difference between VB sub() and VB function() is that function() returns a value and sub() doesn't.

    ...which implies the need to call them differently
    blah
    blah
    callMySub
    blah
    returnValue = callMyFunction


    CyberLynx' example uses a function to return a string which the caller then needs to place in myStatusBar as in:
    myStatusBar.Caption = UpdateStatusBar etc etc

    You could equally use a sub()
    Code:
    Public Sub UpdateStatusBar(Optional Msg As String, Optional User As Boolean, _
             Optional found As Boolean, Optional num As Integer, Optional name As String)  'no return type
       Dim ReturnMsg As String
       If Msg = "" Then
          'Default Message if no Msg parameter is passed to Function
           ReturnMsg = "Welcome to my little program"
       Else
           ReturnMsg = Msg
       End If
       '..........Process other Parameters here - ie:.........
       If name <> "" Then
          ReturnMsg = ReturnMsg & " " & name
       End If
       '.............etc...etc....etc...........
       '.............
        myStatusBar.Caption= ReturnMsg
    End sub
    izy
    currently using SS 2008R2

  5. #5
    Join Date
    Nov 2003
    Posts
    104
    Oh, thanx for the clarification izy.

    Now is there an easy way to overload a function in VB (note: I am using VBA not VB 6 or .net) ?

  6. #6
    Join Date
    Nov 2003
    Posts
    1,487
    The code above is VBA.

  7. #7
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740
    ...and the only way to fake "overload" (if i have understood your definition of this term) is with Optional parameters as suggested by CyberLynx.


    izy
    currently using SS 2008R2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •