madelina
madelina

Reputation: 55

Loop through controls with certain naming structure to set visible property

I have built a form that displays the "status" of projects for each month. I want to update the column/control box properties once at the start of each month (unhide the current month as the year progresses). Since future months won't have any data yet I wrote the following code.

Public Function updateProgressForm()

Dim currentMonth As String
Dim currentYear As Long
Dim txtDate As String
Dim todaysmonth As Long
Dim txtboxMonth As Integer
Dim txtboxName As String
Dim txtboxLabel As String

todaysmonth = Month(Now())
currentMonth = MonthName(todaysmonth, 3)
currentYear = Year(Now())
txtDate = currentMonth & " " & currentYear

DoCmd.OpenForm "IndProgressTracking", acDesign, , , , acWindowNormal
Forms!IndProgressTracking!Month12.ControlSource = txtDate
Forms!IndProgressTracking!Month12Label.Caption = txtDate

For txtboxMonth = 1 To 12 Step 1
    txtboxName = "Month" & txtboxMonth
    txtboxLabel = txtboxName & "Label"
  If IsNull(txtboxName) Then
    txtboxName.Visible = False
    txtboxLabel.Visible = False
  Else
    txtboxName.Visible = True
    txtboxLabel.Visible = True
End If
Next

txtboxName = "Month1"
Forms!IndProgressTracking.Controls!txtboxName.Visible = True

End Function

The part that is giving me an error is the txtboxName.Visible it states invalid qualifier. Therefore I tried the last 2 lines instead however, the last line (before end function) states that it cant find the control I referred to (txtboxName). I have a text box and label for each month so its not a huge deal if I have to call each individually using the control name I was just hoping to simplify by using the For loop. Any suggestions are welcome!

Upvotes: 0

Views: 393

Answers (1)

Darren Bartrup-Cook
Darren Bartrup-Cook

Reputation: 19722

You've defined txtboxName as a string rather than a control object.
To refer to the control by it's name use:

Forms!IndProgressTracking.Controls(txtboxname).Visible = True

Upvotes: 2

Related Questions