带有复杂Where子句的DoCmd.OpenReport问题(语法问题)

时间:2014-12-16 16:16:18

标签: sql access-vba

首先,我已经找了很多其他来源试图寻找解决方案或解释我的错误,但我还没有找到任何可以帮助我的东西。

我一直在学习所有这些东西,所以我肯定缺乏一些基础知识,而且它已经赶上了我。基本上我遇到问题的表单允许用户根据他们关心的标准类型生成三种不同的报告。有三个不同的组合框cboCompany,cboProject,cboEmployee。我使用Nz命令将空值更改为零,然后使用If语句确定要使用哪个报告,具体取决于用户选择的内容。我希望自那以后有所作为。它更容易展示而不是解释。那部分一切都很好。

我想要解决的问题是我想为用户生成的报告进行打印预览。我需要where子句来获取用户在打印预览中选择使用的值。我无法使用where子句的语法来实际执行此操作。

以下是我调用变量的方法。我理解的Nz函数要求变量是变量,因此我不太清楚如何编写where子句的语法。我知道整数,字符串和日期都有不同的语法,但我不知道变体是什么。

Dim Company As Variant
Dim Project As Variant
Dim Employee As Variant
Dim EndDate As String
Dim StartDate As String

Company = Nz(cboCompany, 0)
Project = Nz(cboProject, 0)
Employee = Nz(cboEmployee, 0)
StartDate = txtStartDate.Value
EndDate = txtEndDate.Value

所以接下来的部分是我如何编写其中一个报告选项,我最好猜测where子句的语法。它没有用,所以如果你能帮我指出正确的方向,我会非常感激。

 ElseIf Company <> 0 And Project <> 0 And Employee <> 0 Then
    DoCmd.OpenReport "ProjectReport_Employee_R", acViewPreview, "", "[cboCompany]= " & Company & " And [cboEmployee]= " & Employee & " And [cboProject]= " & Project & " And [txtStartDate]= #" & StartDate & "# And [txtEndDate]= #" & EndDate & "#", acWindowNormal

如果我不能很好地解释事情,或者您需要更多代码来帮助我,请告诉我,并提前感谢您。

***编辑:只是为了给出更完整的图片,这是整个表单背后的代码。

Private Sub cboCompany_AfterUpdate()     Me.cboProject.Requery 结束子

Private Sub cmdSubmit_Click()

'Open Report With Filter Criteria
Dim Company As Variant
Dim Project As Variant
Dim Employee As Variant

Company = Nz(cboCompany, 0)
Project = Nz(cboProject, 0)
Employee = Nz(cboEmployee, 0)

If Company = 0 Then
    MsgBox ("Please Select At Least A Company To Generate A Report")
    cboCompany.Value = Null
    cboProject.Value = Null
    cboEmployee.Value = Null

ElseIf Company <> 0 And Project = 0 And Employee = 0 Then
    DoCmd.BrowseTo acBrowseToReport, "ProjectReport_Company_R", "Main_F.NavigationSubform>ProjectReport_F.Child31"

ElseIf Company <> 0 And Project <> 0 And Employee = 0 Then
    DoCmd.BrowseTo acBrowseToReport, "ProjectReport_Project_R", "Main_F.NavigationSubform>ProjectReport_F.Child31"

ElseIf Company <> 0 And Project <> 0 And Employee <> 0 Then
    DoCmd.BrowseTo acBrowseToReport, "ProjectReport_Employee_R", "Main_F.NavigationSubform>ProjectReport_F.Child31"

ElseIf Company <> 0 And Project = 0 And Employee <> 0 Then
    MsgBox ("Please Select A Project")

End If

End Sub

Private Sub Form_Current()  Me.cboCompany.SetFocus 结束子

Private Sub Form_Load()

&#39;设置日期值    Dim EndDate As String    Dim StartDate As String         StartDate =格式(DateSerial(年(日),月(DateAdd(&#34; m&#34;, - 1,日期)),16),&#34;短日期&#34;)         EndDate =格式(DateSerial(年(日期),月(日期),15),&#34;短日期&#34;)

txtStartDate.Value = StartDate
txtEndDate.Value = EndDate

&#39;在打开的Child31上设置表单消息     DoCmd.BrowseTo acBrowseToForm,&#34; ProjectReport_Message_F&#34;,&#34; Main_F.NavigationSubform&gt; ProjectReport_F.Child31&#34;

End Sub &#39; ---------------------------------------------- -------------- &#39; Print_Click &#39; &#39; ---------------------------------------------- -------------- Private Sub Command39_Click() 错误GoTo Command39_Click_Err

Dim Company As Variant
Dim Project As Variant
Dim Employee As Variant
Dim EndDate As String
Dim StartDate As String

Company = Nz(cboCompany, 0)
Project = Nz(cboProject, 0)
Employee = Nz(cboEmployee, 0)
StartDate = txtStartDate.Value
EndDate = txtEndDate.Value


If Company <> 0 And Project = 0 And Employee = 0 Then

    DoCmd.OpenReport "ProjectReport_Company_R", acViewPreview, "", "", acNormal

ElseIf Company <> 0 And Project <> 0 And Employee = 0 Then

    DoCmd.OpenReport "ProjectReport_Project_R", acViewPreview, "", "", acNormal

ElseIf Company <> 0 And Project <> 0 And Employee <> 0 Then

    DoCmd.OpenReport "ProjectReport_Employee_R", acViewPreview, "", "[cboCompany]= " & Company & " And [cboEmployee]= " & Employee & " And [cboProject]= " & Project & " And [txtStartDate]= '" & StartDate & "' And [txtEndDate]= '" & EndDate & "'", acWindowNormal

End If

Command39_Click_Exit:     退出子

Command39_Click_Err:     MsgBox错误$     恢复Command39_Click_Exit

End Sub

2 个答案:

答案 0 :(得分:1)

我认为您可能在过滤器中使用了错误的字段名称。

假设在报表设计中,您的报表的RecordSource是tblProject。

执行此操作时:

  DoCmd.OpenReport "ProjectReport_Employee_R", , , "[cboCompany]= " & Company ...

您实际上是使用以下记录源打开报告:

  SELECT * FROM tblProject WHERE [cboCompany] = 12345 AND ...

如果tblProject没有名为&#34; cboCompany&#34;的字段。然后它不会起作用。

答案 1 :(得分:0)

我会将报告基于查询,而不是使用DoCmd的“WhereCondition”参数。

使“ProjectReport_Employee_R”记录一个名为“qryProjectReport_Employee_R”的查询。

...然后在每次要运行报告时修改查询定义。

例如:

sub print_report()

'添加这些    昏暗的qdf作为Dao.Querydef    dim strSQL as String

如果这样呢

ElseIf Company&lt;&gt; 0和项目&lt;&gt; 0和员工&lt;&gt; 0然后

'SQL may need modifying...
strSQL= "SELECT * FROM SomeTable WHERE [cboCompany]= " & Company & " And [cboEmployee]= " & Employee & " And [cboProject]= " & Project & " And [txtStartDate]= #" & StartDate & "# And [txtEndDate]= #" & EndDate & "#"

Set qdf="qryProjectReport_Employee_R"
qdf.SQL=strSQL

DoCmd.OpenReport "ProjectReport_Employee_R", acViewPreview

'clean up
set qdf=nothing

结束如果 结束子

这就是全部。如果您有任何问题,请告诉我。