MS Access vba查询格式日期

时间:2019-03-18 09:57:52

标签: vba ms-access

我尝试创建一个对Count项具有三个WHERE条件的查询,但是运行代码时没有结果,甚至没有错误。我在做什么错了?

Private Sub Command5_Click()
Dim db As DAO.Database
Set db = CurrentDb
Dim qdf As DAO.QueryDef
Dim qryMajorDesignReview As String
Dim tblMainReportLOI As String


qryMajorDesignReview = "SELECT Count(tblLOI.loiActivities) As MajorDesignReview, INTO tblMainReportLOI FROM tblLOI " & _
                   "WHERE tblLOI.loiActivities='PSG Major design review for new or existing facilities' " & _
                   "AND Format([loiDate], ""yyyy"")=[Forms]![frmMonthlyDivisionReports]![txtYear] " & _
                   "AND Format([loiDate], ""mmmm"")=[Forms]![frmMonthlyDivisionReports]![txtMonth]; "

On Error Resume Next
DoCmd.DeleteObject acTable, "tblMainReportLOI"

Err.Clear

CurrentDb.Execute qryMajorDesignReview
If Err.Number <> 0 Then
strError = Err.Description
End If

On Error GoTo 0
End Sub

1 个答案:

答案 0 :(得分:0)

在INTO之前删除逗号。另外,连接变量。表单控件的引用是变量。可以使用撇号代替Format()中的双引号。可以使用Year()函数代替Format。

qryMajorDesignReview = "SELECT Count(tblLOI.loiActivities) As MajorDesignReview INTO tblMainReportLOI FROM tblLOI " & _
                   "WHERE tblLOI.loiActivities='PSG Major design review for new or existing facilities' " & _
                   "AND Year([loiDate])=" & [Forms]![frmMonthlyDivisionReports]![txtYear] & _
                   " AND Format([loiDate], 'mmmm')='" & [Forms]![frmMonthlyDivisionReports]![txtMonth] & "'"