Results 1 to 6 of 6
  1. #1
    fredz is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2014
    Posts
    19

    Referenced Values

    With repetitive data, sometimes I "build" my references via VBA.



    For referencing table field names, I can use rst.Fields("name")
    Example: A table with fields for PhoneHome, PhoneWork, PhoneFax
    I can reference them by rst.Field("Phone" & strType)
    Where strType is a variable set to "Home" or "Work" or "Fax"

    For referencing controls on a form, I can use Me("name")
    Example: A form with text controls for PhoneHome, PhoneWork, PhoneFax
    I can reference them by Me("Phone" & strType)
    Where strType is a variable set to "Home" or "Work" or "Fax"

    But if I have dimmed variables (not fields or form controls),
    How can I reference them via VBA?
    Example:
    Dim Appt0800 as String
    Dim Appt0930 as String
    Dim Appt1300 as String
    Dim Appt1430 as String

    If I have a variable strTime = "0930"
    How can I reference the variable named Appt0930?

    I Hope This Makes Sense
    Thanks,
    Fred

  2. #2
    amrut is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2012
    Location
    India
    Posts
    616
    Dim Appt as String
    Appt="Appt" & strTime
    If the value of strTime changes or those values belong to recordset, you can loop through concatenating them with Appt to perform further calculations
    Appt=""
    Appt="Appt" & rst.Fields("strTime")
    Hope this helps.

  3. #3
    fredz is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2014
    Posts
    19
    Thank you Amrut, but I don't think your suggestion works.

    Assume I have a variable named Appt0930 with a value of "Smith"
    and a variable named strTime with a value of "0930".

    If I build a statement like, "Appt" & strTime,
    that gives me a string with the value of "Appt0930", but what I want is to retrieve the value of "Smith" ?

    The following statement is FALSE
    "Appt" & strTime = "Smith"

    Fred

  4. #4
    hapm is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    197
    Variable names shouldn't be used to differntiate between values. It is possible with Eval, but I don't suggest you to go this way!!! Use collections instead. They are for this purpose and allow you to specify a String as the key for the values you store in it.

  5. #5
    amrut is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2012
    Location
    India
    Posts
    616
    What I have mentioned is only a concept. You have to change/select the variables to get the concept working or may be I have not understood your exact problem. What are your fields ? What do you want achieve in above code ? Can you post your code for this part or event ? Further, to get more precise help from experts , I would suggest you to mention your requirement with reference to the table/fields in your database.

  6. #6
    fredz is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2014
    Posts
    19
    I didn't explain my problem very well, I apologize.
    But, I think I found my solution.
    There already is a built-in collection of defined variables, it is called TempVars()

    So, If I have a variable named Appt0930 (with a value of "Smith") and another variable named strTime (with a value of "0930"), then
    I can refer to TempVars("Appt" & strTime) and retrieve the value of "Smith".

    Thank You.
    Fred

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

Similar Threads

  1. Report Referenced Query
    By lugnutmonkey in forum Queries
    Replies: 2
    Last Post: 02-19-2013, 10:58 AM
  2. Replies: 1
    Last Post: 08-19-2012, 09:55 PM
  3. Replies: 11
    Last Post: 06-13-2012, 08:14 AM
  4. Replies: 8
    Last Post: 06-05-2012, 11:49 AM
  5. Replies: 2
    Last Post: 04-15-2011, 01:13 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