VBA变量名称随每个循环增加,可用于填充文本框

时间:2014-11-12 21:43:22

标签: vba loops variables report sequential

我希望你能提供帮助。出于某种原因,我无法想到我做错了什么。也许一副新鲜的眼睛会有所帮助。

  • 从查询中提取GL及其货币值。
  • 每个GL需要在具有顺序名称的报表中填充文本框。例如:GL1,GL2,GL3等
  • 每个GL值(货币)需要填充其他名为unique的文本框。例如:GLV1,GLV2,GLV3,GLV4等

这是我的剧本 - 任何帮助都将不胜感激。

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

更新:这就是我现在所拥有的,它仍然失败了。

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 

运行时错误" 438"对象不支持此属性或方法。

1 个答案:

答案 0 :(得分:0)

也许试试:

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

您的更新代码:

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

假设您的控件名为“GL1”,“GL2”,“GLV1”,“GLV2”等