我正在使用以下内容从VBA在我的表单中设置12个TextBox ControlSource:
... Me.Oct.ControlSource = "=DSum('GBPValue', 'MF YTD Actual Income & Adret', 'Month=10 AND Org_Type=[Key]')" Me.Nov.ControlSource = "=DSum('GBPValue', 'MF YTD Actual Income & Adret', 'Month=11 AND Org_Type=[Key]')" ...
[Key]是表单
中文本框的名称当表单加载时,我会遇到一些奇怪的行为 -
在最终版本中,查询将在用户点击VBA后重新编写,因此......这是一种让表单重新查询数据库的合理方法,如果是这样,我该如何制作DLookups会自动运行/显示,以便在表单加载时立即显示所有内容吗?
答案 0 :(得分:4)
您可能正在寻找Recalc(Me.Recalc)。但是,我建议您使用记录集而不是DlookUp,并使用表单的当前事件:
Dim rs As DAO.Recordset 'Needs MS DAO 3.x library
Dim db As Database
Dim strSQL As String
Set db = CurrentDb()
'Guessing that key is a form value
'Note that Month is a reserved word
strSQL = "SELECT [Month], Sum(GBPValue) As SumVal " _
& "FROM [MF YTD Actual Income & Adret] " _
& "WHERE Org_Type= " & Me.[Key]
& " GROUP BY [Month]"
Set rs=db.OpenRecordset(strSQL)
'You can probably use a Do While Loop, consider
'naming the controls, eg, Month10
rs.FindFirst "[Month]=10"
Me.Oct = rs!SumVal
'and so on