I have a form with 20 buttons. I get the number of unique values for a field, make the same number of buttons visible, and set the caption to the button number. The button names are "btn1", through "btn20" I will use this to change the subform between values. I have tried many ways to get this to work but VB does not like my commandbutton variable as a string or variant. "Set bttn = varTB" is the line that gives the error "Object Required".
Below is the code:
Dim strArea As String
Dim intX As Integer
Dim strBtn As String
Dim varTB As Control
Dim bttn As New CommandButton
Dim strCnt As String
Dim rsCnt As Recordset
strArea = Me.Combo6.Value
Me.Filter = "SftCstItm = '" & strArea & "'"
Me.FilterOn = True
Me.Requery
'Set bttn.Name = Me.btn1.Name
intX = 1
Do Until intX = 21
varTB = ("Me.btn" & intX)
Set bttn = varTB
bttn.Visible = False
intX = intX + 1
Loop
'Get number of FTEs for Area
strCnt = "SELECT qryResAreaFrm.FTE " & _
"FROM qryResAreaFrm " & _
"WHERE qryResAreaFrm.SftCstItm = '" & strArea & "' " & _
"GROUP BY qryResAreaFrm.FTE;"
Set rsCnt = CurrentProject.Connection.Execute(strCnt)
If rsCnt.EOF Then
Exit Sub
Else
intX = 1
rsCnt.MoveFirst
Do Until rsCnt.EOF
varTB = "Me.btn" & intX
Set bttn = varTB
With bttn
.Visible = True
.Caption = intX
End With
intX = intX + 1
Loop
End If
Thank you in advance for your help,
Below is the code:
Dim strArea As String
Dim intX As Integer
Dim strBtn As String
Dim varTB As Control
Dim bttn As New CommandButton
Dim strCnt As String
Dim rsCnt As Recordset
strArea = Me.Combo6.Value
Me.Filter = "SftCstItm = '" & strArea & "'"
Me.FilterOn = True
Me.Requery
'Set bttn.Name = Me.btn1.Name
intX = 1
Do Until intX = 21
varTB = ("Me.btn" & intX)
Set bttn = varTB
bttn.Visible = False
intX = intX + 1
Loop
'Get number of FTEs for Area
strCnt = "SELECT qryResAreaFrm.FTE " & _
"FROM qryResAreaFrm " & _
"WHERE qryResAreaFrm.SftCstItm = '" & strArea & "' " & _
"GROUP BY qryResAreaFrm.FTE;"
Set rsCnt = CurrentProject.Connection.Execute(strCnt)
If rsCnt.EOF Then
Exit Sub
Else
intX = 1
rsCnt.MoveFirst
Do Until rsCnt.EOF
varTB = "Me.btn" & intX
Set bttn = varTB
With bttn
.Visible = True
.Caption = intX
End With
intX = intX + 1
Loop
End If
Thank you in advance for your help,