VBA variable name that increases with each loop and can be used to populate a textbox

141 Views Asked by At

I hope you can assist. For some reason I can not think of what I am doing wrong. Maybe a fresh pair of eyes would help.

  • pulling GL's and their currency value from a query.
  • each GL needs to populate text boxes in a report with have sequential names. Ex: GL1, GL2, GL3, etc.
  • Each GL Value (currency) needs to populate other text boxes named uniquely. Ex: GLV1, GLV2, GLV3, GLV4, etc.

Here is my script - any help would be appreciated.

Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT * FROM qry_GL_totals")
Dim GLField As Variant

If Not (rs.EOF And rs.BOF) Then
    rs.MoveFirst 'Unnecessary in this case, but still a good habit
    RecordCount = 0
    Do Until rs.EOF = True
        RecordCount = RecordCount + 1
        MsgBox (rs!GL & " " & rs!Expr1) 'MsgBox is just for testing
        "[" & GLField & "]" =rs!GL
        "[" & GLField & "T]" =rs!Expr1
        rs.MoveNext
    Loop
Else
End If
rs.Close
Set rs = Nothing
GLField = Null
End Sub

Update: This is what I have now and its failing still.

Private Sub Report_Load() 

    Dim rs As DAO.Recordset 
    Set rs = CurrentDb.OpenRecordset("SELECT * FROM qry_GL_totals") 
    If Not (rs.EOF And rs.BOF) Then 
        rs.MoveFirst 
        i = 0 
        Do Until rs.EOF = True 
            i = i + 1 
            Me.Controls(GLField) = rs!GL 
            Me.Controls(GLField & "T") = rs!Expr1 
            rs.MoveNext 
        Loop 
    Else 

    End If 
    rs.Close 
    Set rs = Nothing 

End Sub 

Run-time error "438" Object doesn't support this property or method.

1

There are 1 best solutions below

5
On

Maybe try:

Me.Controls(GLField) = rs!GL
Me.Controls(GLField & "T") = rs!Expr1

For your updated code:

Me.Controls("GL" & i) = rs!GL
Me.Controls("GLV" & i) = rs!Expr1

assuming your controls are named "GL1", "GL2", "GLV1", "GLV2" etc