在窗体中单击textBox之前,Access中的DLookup未运行

时间:2022-02-22 15:38:23

I'm setting 12 TextBox ControlSources in my Form from VBA using the following :

我使用以下内容从VBA在我的表单中设置12个TextBox ControlSources:

...
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] is the name of a textbox in the form

[Key]是表单中文本框的名称

When the form loads up i get some odd behavior -

当表单加载时我得到一些奇怪的行为 -

  • all of the summary form text boxes are blank as are all the dlookup text boxes
  • 所有的dlookup文本框都是空白的所有摘要表单文本框
  • if i then click on one of the text boxes that has a dlookup control source assigned the summary text boxes for the other columns start to populate with 0's and #Num etc. and the dlookup runs and displays the expected numbers
  • 如果我然后点击其中一个分配了dlookup控制源的文本框,其他列的摘要文本框开始填充0和#Num等,并且dlookup运行并显示预期的数字
  • once i've clicked on all the dlookup fields the summary numbers calc properly.
  • 一旦我点击了所有dlookup字段,摘要数字就会正确计算。

In the final version of this the query will be re-written after user clicks from the VBA so ... is this a sensible way to get the form to re-query the DB and, if so, how can i make the DLookups run/display automatically so that everything displays immediately on form load?

在最终版本中,查询将在用户点击VBA后重写...所以...这是一种让表单重新查询数据库的合理方法,如果是这样,我怎样才能运行DLookups /自动显示,以便在表单加载时立即显示所有内容?

1 个解决方案

#1


4  

You are probably looking for Recalc (Me.Recalc). However, I suggest you use a recordset, rather than DlookUp, and the Current event for the form:

你可能正在寻找Recalc(Me.Recalc)。但是,我建议您使用记录集而不是DlookUp,以及表单的Current事件:

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

#1


4  

You are probably looking for Recalc (Me.Recalc). However, I suggest you use a recordset, rather than DlookUp, and the Current event for the form:

你可能正在寻找Recalc(Me.Recalc)。但是,我建议您使用记录集而不是DlookUp,以及表单的Current事件:

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