Results 1 to 4 of 4
  1. #1
    kpilsbury is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jan 2014
    Location
    St. Petersburg, FL
    Posts
    5

    Trying to dynamically change control names on Access Form using VBA

    I am trying to reference different controls on an access form:


    txtCompleteStartDate13 txtCompleteEndDate13
    txtCompleteStartDate26 txtCompleteEndDate26
    txtCompleteStartDate52 txtCompleteEndDate52
    txtCompleteStartDate65 txtCompleteEndDate65

    And run the same queries for each set of controls, they are basically different date ranges.

    I have everything working fine except the following: I am wanting to use retper (The number in the array) to point me to a different text box on the form. But the variable keeps returning a string instead of the value on the form.

    Any ideas? Below is part of the code I am having issues with.

    Dim retstart As Date
    Dim retend As Date



    DoCmd.RunSQL "DELETE FROM TMP_REGION_REPORT_DATA" (Clears Table for input)

    retlist = Array("13", "26", "52", "65")


    For Each retper In retlist

    retstart = "[Forms]![ANNUAL_RETENTION_MAIN_new]![txtCompleteStartDate" & '" & retper & "' & "]"
    retend = "[Forms]![ANNUAL_RETENTION_MAIN_new]![txtCompleteEndDate" & '" & retper & "' & "]"

    Kevin

  2. #2
    kpilsbury is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jan 2014
    Location
    St. Petersburg, FL
    Posts
    5
    I actually found another post from earlier that helped me resolve this: (Referencing a textbox with a VBA String)

    Here is my solution:
    retlist = Array("13", "26", "52", "65")


    For Each retper In retlist


    ctlstart = "txtCompleteStartDate" & retper
    ctlend = "txtCompleteEndDate" & retper


    retstart = [Forms]![ANNUAL_RETENTION_MAIN_new].Controls(ctlstart).Value
    retend = [Forms]![ANNUAL_RETENTION_MAIN_new].Controls(ctlend).Value

    This allows me to loop through my array and change to the different date ranges in the multiple text boxes on the form.

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Try

    retstart = [Forms]![ANNUAL_RETENTION_MAIN_new]("txtCompleteStartDate" & retper)
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Kevin,

    Please show us all the code. Where do you Dim retper? Array?

    You can name controls on a form with sequential numbers, then use a loop and index to identify the controls.

    But first how about telling us WHAT you're trying to do. Simple, clear English with no jargon would be a big help for the readers.

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

Similar Threads

  1. Replies: 6
    Last Post: 02-28-2015, 01:23 PM
  2. Replies: 3
    Last Post: 05-30-2013, 08:03 AM
  3. Replies: 3
    Last Post: 04-21-2013, 06:48 AM
  4. Use String to change form control values
    By DerekAwesome in forum Programming
    Replies: 14
    Last Post: 12-02-2012, 07:19 PM
  5. writing control names on a form to a table
    By focosi in forum Access
    Replies: 1
    Last Post: 09-21-2011, 08:47 AM

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