Results 1 to 4 of 4
  1. #1
    Markb384 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    44

    Referencing a textbox using a string in VBA

    Hi guys



    To simplify my problem, imagine I have a bunch of textboxes on a form, all named textbox_A, textbox_B etc. To save having to type 26 lines of code, I decided to create a for loop instead, using the code beneath to set the value in each textbox to the corresponding letter. The current code is my latest attempt, but I have tried many variations including with and without delimiters, dots instead of bangs etc.

    Code:
    Dim loopLetter As String 
    Dim loopInteger As Integer
    
    
        For loopInteger = 65 To 90
            loopLetter = "'textbox_" & Chr(loopInteger) & "'"
                [Forms]![Results]![loopLetter] = loopLetter
        Next loopInteger
    How do I go about referencing the textbox using the variable from the loop?

    Thank you

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Hmmm.... here is an example I worked up to help you enumerate your controls in a form. You could incorporate the Select Statement to assign a value to a control once you found the one you need.


    here is a list of control types. You could also assign a tag to controls of your choice if you only want to loop a specific group of controls on your form. Not sure how you want to choose the controls on your form.
    http://msdn.microsoft.com/en-us/libr...ffice.12).aspx

    Here is some sample code. Use the immediate window in the VBA editor to view the results.

    Code:
    Dim ctl As Control
    Dim intCount As Integer
    intCount = 0
    For Each ctl In Me.Controls
    intCount = intCount + 1
    Debug.Print "Item number " & intCount & " is a " & ctl.ControlType & vbCrLf
    Select Case ctl.ControlType
    Case acTextBox
    Debug.Print ctl.Name & " is a textbox!"
    End Select
    Next

  3. #3
    Markb384 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    44
    Hi ItsMe

    I solved it using
    Code:
    [Forms]![results].Controls(loopLetter).Value = = loopLetter

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Nice use...

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

Similar Threads

  1. Replies: 2
    Last Post: 01-18-2014, 11:51 AM
  2. Concat textbox name with string
    By dwif in forum Access
    Replies: 2
    Last Post: 06-08-2013, 02:31 PM
  3. Replies: 3
    Last Post: 05-28-2013, 12:53 PM
  4. Referencing a textbox using variable values from a function?
    By omahadivision in forum Programming
    Replies: 3
    Last Post: 12-20-2012, 09:23 AM
  5. Replies: 7
    Last Post: 02-23-2011, 06:26 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