Variable in Form Control Names

JWalcott

New Member
Joined
Jan 25, 2003
Messages
34
I have a form that has seven columns of information. There are 40 rows of information. Each Text Box is named as follows:

Task1ID Task1 Task1DueDate Task1NA Task1Date Task1Assign Task1Comments
Task2ID Task2 Task2DueDate Task2NA Task2Date Task2Assign Task2Comments

This continues on for 40 different tasks. The text boxes are not connected to any fields in a table, though they end up being filled with fields from various tables. I wrote the original code filling the 40 groups of text boxes, but now I need to add sorting capabilities. I would like to avoid writing the 40 bits of code again if at all possible. All I need the capability of doing to make this as simple as it should be is the ability to use a variable in the name of the text box when referring to the text boxes in VBA. I have tried a few different methods that do not work shown below. If anyone knows how to do this, it would save hours of monotonous code and would be greatly appreciated!!!!!!!!!!

Code:
Dim TempPTArray(40, 7)
    For j = 1 To 40
        For k = 1 To 7
            TaskID = "[Forms]![ProjectsTracker].[Task" & j & "ID]"
            TempPTArray(j, 1) = TaskID
            TempPTArray(j, 2) = [Forms]![ProjectsTracker].[Task & j & ]
            TempPTArray(j, 3) = [Forms]![ProjectsTracker].[Task & j &   DueDate]
        Next k
    Next j
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Hi JWalcott,

Try the following, it might help:

Code:
Dim TempPTArray(40, 7)
Dim arrNames()
Dim j As Integer, k As Integer
    arrNames = Array("ID", "", "DueDate", "NA", "Date", "Assign", "Comments")
    For j = 1 To 40
        For k = 1 To 7
            TempPTArray(j, k) = [Forms]![ProjectsTracker].Controls("Task" & j & arrNames(k - 1))
        Next k
    Next j

Suat
 
Upvote 0

Forum statistics

Threads
1,221,837
Messages
6,162,282
Members
451,759
Latest member
damav78

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top