Results 1 to 11 of 11
  1. #1
    veloopity is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    5

    a simple getter function for a constant

    I want to define a number of public constants, like this

    public const const_a = 14
    public const const_b = 29

    and I would like to use these constants in queries (independent of forms), like this

    select * from mytable where ID = const_a

    This doesn't work as easily of course, so it seems I need a getter function like this

    public function myconst (constantname)
    myconst = eval(constantname)


    end function

    I could then use the function in my query, like this

    select * from mytable where ID = myconst("const_a")

    Unfortunately, this still doesn't work because even though the value gets sent to the function and could be outputted, the expression "eval(constantname)" doesn't return the value of the constant with that name.

    I don't know what to do, but can't believe this can't be done in a simple way. Looking forward to your solution

    -Michael

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    If const_a = 14

    why not just have

    select * from mytable where ID = 14

  3. #3
    veloopity is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    5
    because it is not 2 constants, but dozens of them. Believe me, we have reasons to do it that way. We want to keep literal ID values out of our code and store them in a central place (but not in a database table but in a list of constants.) So, ideas how to do it?

  4. #4
    rankhornjp is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Jul 2011
    Posts
    46
    does "const_a" ever change?

  5. #5
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    You could place some variables in a general module as public. ie, intDeptID or lngDeptID, intUserID or lngUserID.

    Then, when the program knows what the value is, maybe 14, it can assign it to the variable. Maybe when a form opens you can do something like intDeptID = 14. Then you can use intDeptID in various functions and or routines throughout the DB.

    I really don't know the solution. That Is why I asked a question. I am still not sure I understand but.....

  6. #6
    rankhornjp is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Jul 2011
    Posts
    46
    Also, how do you select which constant you want to use? Are you using a list box or text box or .....?

  7. #7
    veloopity is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    5
    I don't want to explain why I am doing things, I need a solution for my question. I think I have stated clearly enough what my question is. Again:

    Imagine a public constant const_a, containing the value 43. How can I write a simple function that is given the string "const_a" and that would give me back the value 43?

  8. #8
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    1) Assuming that "dozens" is the right order of magnitude, you can use TempVars to store those values. They will always be available to queries and code, and can be accessed with a standard naming convention. You are limited to 255 total tempvars.

    2) Depending on how you plan to use the constants, you could create a tblConstants table with two fields, ConstName and ConstValue. This is more clumsy than TempVars, but has no upper limit for how many you could use.

    3) Use a getter function such as the following, in a standard module:
    Code:
    Option Compare Database
    Option ExplicitGlobal GBL_UserID As String
    Global GBL_UserName As String
    Global GBL_LogInTime As Date
    Global GBL_LogOutTime As Date
    
    Function set_globals()
        GBL_UserID = Environ("UserID")
        GBL_LogInTime = Now()
    End Function
       
    Public Function get_global(G_name As String) As Variant
         Select Case G_name
            Case "GBL_UserID"
                 get_global = GBL_UserID
            Case "GBL_UserName"
                 get_global = GBL_UserName
            Case "GBL_LogInTime"
                 get_global = GBL_LogInTime
            Case "GBL_LogOutTime"
                 get_global = GBL_LogOutTime
        End Select
    End Function

  9. #9
    veloopity is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    5
    thx bunches Dal, sounds good to me, will check this out on Monday!!

  10. #10
    veloopity is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    5
    great! I hadn't heard about Tempvars before. Works like a charm!

  11. #11
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Great! Please mark the thread solved. Top of page, under "thread tools".

Please reply to this thread with any new information or opinions.

Similar Threads

  1. simple syntax for putting a variable into a function
    By markjkubicki in forum Programming
    Replies: 2
    Last Post: 11-02-2012, 09:46 AM
  2. Simple VLookup function in Access
    By acharyagautam in forum Queries
    Replies: 12
    Last Post: 02-07-2012, 12:18 PM
  3. Silly error on a simple function call - help
    By allenjasonbrown@gmail.com in forum Programming
    Replies: 5
    Last Post: 06-10-2011, 01:23 PM
  4. Need Help with Simple VB Function
    By nrage21 in forum Programming
    Replies: 2
    Last Post: 09-21-2010, 09:14 AM
  5. Replies: 2
    Last Post: 06-23-2010, 06:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums