在窗体中单击文本框之前,Access中的DLookup不会运行 [英] DLookup in Access not running until textBox clicked on in Form

查看:176
本文介绍了在窗体中单击文本框之前,Access中的DLookup不会运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在VBA中使用以下命令在窗体中设置12个TextBox ControlSource:

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


...
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]是格式的文本框的名称

[Key] is the name of a textbox in the form

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

When the form loads up i get some odd behavior -

  • 所有摘要表单文本框都为空白,所有dlookup文本框都为空白
  • 如果我然后单击具有dlookup控件源的文本框之一,则为其他列分配了摘要文本框,开始用0和#Num等填充,并且dlookup运行并显示期望的数字
  • 一旦我单击了所有dlookup字段,汇总数字就会正确计算.
  • all of the summary form text boxes are blank as are all the dlookup text boxes
  • 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
  • once i've clicked on all the dlookup fields the summary numbers calc properly.

在此最终版本中,用户从VBA单击后将重新编写查询,所以...这是获取表格以重新查询数据库的明智方法,如果是的话,我该如何制作DLookups自动运行/显示,以便在加载表单时立即显示所有内容?

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?

推荐答案

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

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:

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

这篇关于在窗体中单击文本框之前,Access中的DLookup不会运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆