Results 1 to 9 of 9
  1. #1
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919

    Dynamic "variable names" in code

    I knew the following code wouldn't compile when I wrote it, but I'm showing it here to clarify the question. Namely, can a statement be formed to accomplish what one sees in the code below where I'm trying to utilize the string constants "strDFltxx" where xx is the current value in the variable "intNewTTypeID"? (E.g., Const strDFlt10 as string = "blah blah blah")



    Code:
                Select Case intNewTTypeID
                    Case 2, 3, 4, 8, 10, 13
                        Me.tbNewDescription = strDFlt & intNewTTypeID
                    Case Else
                        Me.tbNewDescription = ""
                        bolFUPending = True
                End Select

  2. #2
    ranman256's Avatar
    ranman256 is online now VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,521
    What is tbNewDescription?
    a table?
    a tab?

    if tab,you must use tab.caption=val.
    if table, it will fail.

  3. #3
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    tbNewDesctiption is an unbound text box and strDFltxx are string constants.

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I tried many things, no success. I used the Switch() function, tried the EVAL() function, wrote a UDF.... easiest was to use "SELECT CASE".
    If the string constants were in a table, it would be much easier.
    Code:
    Option Compare Database
    Option Explicit
    
    Const strDFlt2 As String = "222222222222222"
    Const strDFlt3 As String = "333333333333333"
    Const strDFlt4 As String = "444444444444444"
    Const strDFlt8 As String = "888888888888888"
    Const strDFlt10 As String = "10101010101010"
    Const strDFlt13 As String = "13131313131313"
    
    Private Sub tbintNewTTypeID_AfterUpdate()
    
        Select Case Me.tbintNewTTypeID
            Case 2
                Me.tbNewDesctiption = strDFlt2
            Case 3
                Me.tbNewDesctiption = strDFlt3
            Case 4
                Me.tbNewDesctiption = strDFlt4
            Case 8
                Me.tbNewDesctiption = strDFlt8
            Case 10
                Me.tbNewDesctiption = strDFlt10
            Case 13
                Me.tbNewDesctiption = strDFlt13
            Case Else
                Me.tbNewDesctiption = ""
                bolFUPending = True
        End Select
    
    End Sub
    Sometimes you have to brute force it.

    Does this help??
    Hopefully, someone else will give a better solution.

  5. #5
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    I concluded the same thing and implemented the SELECT solution in order to move on with more important aspects of the app. However, I like the table thought so as to keep hard-coded strings out of the otherwise generalized front end. What are you thinking, something like DLookup("[MyDescription]","tblDFltDescriptions", "DescID = " & intNewTypeID) where the table doesn't use autonumbers but rather the description keys 2, 3, 4, 8, 10 and 13? And, where the table is in the backend.
    Bill

  6. #6
    ranman256's Avatar
    ranman256 is online now VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,521
    you should use better prefix descriptive letters, for self identification,
    ksXXX for a constant string
    txtXXX for text boxes
    cboXXX for combo boxes
    etc

  7. #7
    InsuranceGuy is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Aug 2015
    Location
    Colorado
    Posts
    126
    Quote Originally Posted by GraeagleBill View Post
    I concluded the same thing and implemented the SELECT solution in order to move on with more important aspects of the app. However, I like the table thought so as to keep hard-coded strings out of the otherwise generalized front end. What are you thinking, something like DLookup("[MyDescription]","tblDFltDescriptions", "DescID = " & intNewTypeID) where the table doesn't use autonumbers but rather the description keys 2, 3, 4, 8, 10 and 13? And, where the table is in the backend.
    Bill
    Your DLOOKUP approach is the fast-hack that I use occasionally. Like you, I try to avoid putting anything hard coded into front ends. It forces application updates that should be data updates.


    Jeff

  8. #8
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I "roll my own" look up function....

    In the table, you could change the "prefix" (from "strDFlt" to "strZZZ") to get other string messages. Of course, you will probably change the field names/table name to your standards.

    Take a look at this:
    Attached Files Attached Files

  9. #9
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    I don't have a form with those control names, so I dim'd the variable i in place of that. Seems to me that an array might be the way to go.
    Code:
    Sub testConstNames()
    Dim aryCnst(2)
    Dim i As Integer
    aryCnst(0) = "apple"
    aryCnst(1) = "pear"
    i = 1
    MsgBox aryCnst(i)
    
    End Sub
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. Can't test code - "Debug" - "Run to Cursor"
    By GraeagleBill in forum Programming
    Replies: 4
    Last Post: 05-15-2016, 05:16 PM
  2. Replies: 6
    Last Post: 02-11-2016, 02:05 PM
  3. Replies: 2
    Last Post: 12-23-2015, 09:32 PM
  4. Replies: 1
    Last Post: 09-07-2015, 08:00 AM
  5. Replies: 4
    Last Post: 07-12-2014, 02:02 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