【问题标题】:ms access form criteria help errorms访问表单条件帮助错误
【发布时间】:2017-10-18 21:45:41
【问题描述】:

我正在尝试打开带有条件的表单。

我得到错误的标准。

strCriteria = "WorkID = 3 And 
               OptOut= -1 And 
               AppointmentDate = (Last(AppointmentDate))>DateSerial(Year(Date()),Month(Date())-3,1) And 
                                 (Last(AppointmentDate))<DateSerial(Year(Date()),Month(Date())-2,0)"

我这样放置是为了更容易阅读。

我的错误号是 3096。

谢谢。

====更新我放弃======= 到目前为止我的代码。

strSQL = "SELECT tblAppointment.WorkID," & _
        "tblCustomer.OptOut," & _
        "Last(tblAppointment.AppointmentDate) AS LastAppointmentDate," & _
        "tblAppointment.CustomerID," & _
        "tblCustomer.Surname," & _
        "tblCustomer.Name," & _
        "tblCustomer.FatherName," & _
        "Last(tblAppointment.AppointmentMemo) AS LastAppointmentMemo" & _
"FROM tblCustomer INNER JOIN tblAppointment ON tblCustomer.CustomerID = tblAppointment.CustomerID " & _
"GROUP BY tblAppointment.WorkID," & _
    "tblCustomer.OptOut," & _
    "tblAppointment.CustomerID," & _
    "tblCustomer.Surname," & _
    "tblCustomer.Name," & _
    "tblCustomer.FatherName " & _
"HAVING (((tblAppointment.WorkID) = 3) And ((tblCustomer.OptOut) = -1) And " & _
       "(LastAppointmentDate > DateSerial(Year(Date()), Month(Date()) - 3, 1) And " & _
       "LastAppointmentDate < DateSerial(Year(Date()), Month(Date()) - 2, 0)))" & _
"ORDER BY LastAppointmentDate, " & _
    "tblCustomer.Surname," & _
    "tblCustomer.Name," & _
    "tblCustomer.FatherName;"

【问题讨论】:

  • 为什么不使用查询生成器?

标签: string forms ms-access where criteria


【解决方案1】:

我看你还是有问题。让我们看看你给我们的代码。

您的 having 语句正在查找表中不存在的字段。您目前拥有:

Having (((tblAppointment.WorkID) = 3) And ((tblCustomer.OptOut) = -1) And " & _
"(LastAppointmentDate > DateSerial(Year(Date()), Month(Date()) - 3, 1) And " & _
"LastAppointmentDate < DateSerial(Year(Date()), Month(Date()) - 2, 0)))" & _

你的having子句正在你的桌子上寻找LastAppointmentDate。此字段不存在,因为字段名称为 AppointmentDate。在您的声明中更改您的字段名称以匹配字段名称,它应该可以工作。您还缺少括号。

Having (((tblAppointment.WorkID) = 3) AND ((tblCustomer.optout) = -1) AND " & _
"((tblAppointment.AppointmentDate) > DateSerial(Year(Date()), Month(Date()),-3,1)) AND " & _
"((tblAppointment.AppointmentDate) < DateSerial(Year(Date()), Month(Date()),-2,0))) " & _

试试这个解决方案来处理你的声明。如果它不起作用,请告诉我,我会做更多的挖掘工作。

【讨论】:

    【解决方案2】:

    您的问题没有太多信息可以解释,但我会尝试一下。

    我假设您正在尝试使用以下代码打开一个表单:

    docmd.openform "FormName",acnormal,,strcriteria
    

    如果是这种情况,您的变量正在寻找尚未确定或尚未发现的最后约会日期。基本上,您为尚未加载的表单上的字段设置条件,因此无法使用任何信息。

    您可以尝试多次对我公正的不同方法,我今天继续使用这种方法。

    private sub Eventtrigger()
    dim frm as form
    dim strSQL as string
    
    strsql = "SELECT * " & _
             "FROM TableName " & _
             "WHERE (((TableName.workID) = 3 AND (TableName.OptOut) = -1 AND (TableName.AppointmentDate) > DateSerial(year(date()),Month(Date())-3,1) AND (TableName.AppointmentDate) < DateSerial(year(date()),Month(date())-2,0)));"
    'Edited since I missed 2 closing parenthesis
    
    Docmd.openform "FormName",acnormal
    set frm = [forms]![FormName] 'New form opened
    frm.recordsource = strsql
    
    EndCode:
    if not frm is nothing then
    set frm = nothing
    end if
    end sub
    

    上面的代码将允许您将表单的记录源设置为新创建的查询。查询将为您过滤结果。

    或者,要修复变量,只需将变量设置如下:

    strcriteria = "WorkID = 3 AND OptOut = -1 AND " & _
                  "AppointmentDate > DateSerial(Year(date()),Month(date())-3,1) AND " & _
                  "AppointmentDate < DateSerial(Year(date()),Month(date())-2,0)"
    

    如果您需要最后一个,请在 AppointmentDate 上使用 last:

    Last(AppointmentDate) > DateSerial(Year(date()),Month(Date())-3,1) AND " & _
    Last(AppointmentDate) < DateSerial(Year(date()),Month(Date())-2,0)
    

    如果这些方法/修复中的任何一个都不起作用,请告诉我,我会做更多的挖掘工作。

    【讨论】:

    • 谢谢克里斯。我正在尝试将第二种解决方案与 Last.但我仍然收到错误。
    • 最后一个是聚合函数。标准不喜欢最后一个。放下最后一个,看看你的结果是什么,因为你使用的是大于和小于,看起来你正在寻找某个日期范围内的约会日期。
    • 我需要查看最近 3 个月内特定工作的最后一次约会。
    • 如果使用“Last”聚合,您的查询需要是分组依据。但请注意,您的查询将无法更新。 group by 不能使用 *,您需要列出您希望查看的所有字段。我也会考虑将 last 更改为 max。
    • strSQL = "SELECT WorkID, OptOut, Max(AppointmentDate) " & _ "FROM tblAppointment " & _ "WHERE (((tblAppointment.WorkID) =3 AND (tblAppointment.OptOut) = -1 AND (tblAppointment.AppointmentDate) 在 DateSerial(Year(date()),Month(Date()),-3,1) AND DateSerial(Year(date()),Month(Date())-1,0)) 之间" & _ "GROUP BY WorkID, OptOut;"
    猜你喜欢
    • 2019-04-03
    • 2016-02-09
    • 2011-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-28
    • 1970-01-01
    相关资源
    最近更新 更多