在 MS Access 中创建报告的 VBA 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3129752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA code to create a report in MS Access
提问by Edmond
Can anyone help me create a code satatement that will allow me to create a report with the sQ statement below? The problem I'm having is that my form allows you to input a Cost center, but when I click on the command button to execute the code it asks me to input the cost center again before it shows me the report. I want to eliminate having to enter the cost center again and just take it from when it is enters on the form.
任何人都可以帮助我创建一个允许我使用下面的 sQ 语句创建报告的代码声明吗?我遇到的问题是我的表单允许您输入成本中心,但是当我单击命令按钮执行代码时,它要求我在向我显示报告之前再次输入成本中心。我想消除必须再次输入成本中心,并从它在表单上输入时开始。
Private Sub CmdCC_Click()
私有子 CmdCC_Click()
Set mydb = CurrentDb
myCC = txtCC.Value
If IsNull(myCC) Or myCC = "" Then
MsgBox "Please enter a Cost Center!", vbCritical + vbOKOnly, pTitle
End If
sQ = "SELECT ZBASED.ACCT_UNIT, CenterName, ZBASED.ACCOUNT, ZBASED.ACCOUNT_DESC " & _
"FROM ZBASED, CCtable " & _
"WHERE (ZBASED.ACCT_UNIT = " & myCC & ") And (CenterNo = " & myCC & ") " & _
"ORDER BY ZBASED.ACCOUNT;"
采纳答案by Kevin Ross
All you have to do is reference the form you are calling the report from in the SQL, for example
您所要做的就是在 SQL 中引用您正在调用报告的表单,例如
SELECT foo FROM bar WHERE foo=[Forms]![frmReporting]![txtFoo]
SELECT foo FROM bar WHERE foo=[Forms]![frmReporting]![txtFoo]
You then have a button on frmFoo that opens the report, you can include some logic in before the docmd.OpenReport call to validate the input i.e. make sure they have entered a cost centre
然后您在 frmFoo 上有一个打开报告的按钮,您可以在 docmd.OpenReport 调用之前包含一些逻辑以验证输入,即确保它们已进入成本中心
回答by Fionnuala
If the report is already based on say,
如果报告已经基于 say,
SELECT ZBASED.ACCT_UNIT, CenterName,
ZBASED.ACCOUNT, ZBASED.ACCOUNT_DESC
FROM ZBASED, CCtable
(There is no point in using ORDER BY with a report, you must use the report's own Oder & Grouping properties)
(在报表中使用 ORDER BY 毫无意义,您必须使用报表自己的 Oder & Grouping 属性)
You can use the Where argument of OpenReport:
您可以使用 OpenReport 的 Where 参数:
DoCmd.OpenReport "ReportName", acViewPreview, , "ZBASED.ACCT_UNIT = " & myCC _
& " And CenterNo = " & myCC