Results 1 to 7 of 7
  1. #1
    Join Date
    May 2005
    Posts
    1,191

    Unanswered: Enumerated Argument in Procedure Declaration

    When calling an inbuilt function in VB Access will sometimes prompt you for some arguments when they are preset values (for example, when calling msgbox, the second argument is an enumerated type and it will offer a dropdown box to choose vbYesNo, vbInformation, etc.): Is there a way to set these enumerated types in your own functions? For instance, if I want to declare a Function named fxnExample that returns a boolean with one argument that has the following enumerated values:
    1 = A
    2 = B
    3 = C
    I've been experimenting with the Enum Statement, but so far have not been able to incorporate it into the procedure declaration statement. Is it possible to do this? If so, does anyone know how?
    Me.Geek = True

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    you'd need to declare the enumeration to be public or in the same module
    then define the datatype of the parameter to be that enumeration

    eg
    Code:
    Public Enum FormatStyles
            'how a co ordnate may be defined when being sent to the class
            LetClassResolve = 0                 'the developer can't be arsed to tell us so work it out
            DecimalDegrees = 1                  'dDD.DDDDDD
            DegreesDecimalMinutes = 2           'dDD MM.MMMMMM
            CompactDegreesDecimalMinutes = 3    'dDD MM.MMMMMM 'inpractice is similar to 2 with the space stripped out!
            DegreesMinutesSeconds = 4           'dDD MM SS
            Traditional = 5                     'dDDºMM'SS"  .. degree symbol is ALT + 186
        End Enum
    Code:
        Public Overloads Function SetCoOrdinate(ByVal CoOrdinate As String, ByVal strHemisphere As String, Optional ByVal intStyle As FormatStyles = FormatStyles.CompactDegreesDecimalMinutes) As Boolean
    ..at least that how it works in VB.NET, its so long ago since I did any serious (or any) VBA in Access that I cannot remmeber if that is the same.
    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    Mar 2009
    Posts
    5,441
    Provided Answers: 14
    It does not work in VBA because you cannot overload a function. One way would be to encapsulate the function into an OLE object (DLL or OCX) and to supply a .tlb file, but that would be a lot of work and it cannot be done in vba either.

  4. #4
    Join Date
    May 2005
    Posts
    1,191
    Thanks for the input guys. I was just wondering if it could easily be done as it's not uncommon for me to write a function that takes an argument into a switch statement or if/elseif statement. Usually I just make it a string argument, but then I have to remember (or look up) what the value is I have to pass it, and I thought it would be nice to have a dropdown instead. Oh well. Thanks!
    Me.Geek = True

  5. #5
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    the overloads bit comes from a class that Im developing in .net, which has numerous overloads. I stole that fragment as an example of how it should be used in a function call

    as I said Its a while since I've done much VBA stuff

    opening my copy of Access as a developer, for the first time in several months and whaddyaknow....
    take out the overloads and it works fine, as expected the enumeration is visible to code that calls the function
    I'd rather be riding on the Tiger 800 or the Norton

  6. #6
    Join Date
    May 2005
    Posts
    1,191
    I just got that to work, thanks healdem!
    Me.Geek = True

  7. #7
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    BTW - minor but it doesn't need to be in the same module as it is used. I tend to pop enums in my global module.
    Testimonial:
    pootle flump
    ur codings are working excelent.

Posting Permissions

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