首页 > 解决方案 > 参数太少,预期为 1

问题描述

我有一个小问题......

DDA3 = "SELECT DDAs.[QR Code], DDAs.[Drug Name and Strength],DDAs.[Dispensing Date], DDAs.[Amount Prescribed], DDAs.[Prescription Date], DDAs.[Record Number], DDAs.[NOT DISPENSED] " _
& "FROM DDAs " _
& "WHERE ([DDAs].[QR Code] = [QR Code] AND [DDAs].[Dispensing Date] = [Date 2] AND [DDAs].[NOT DISPENSED] = No) " _

以上给了我错误

当我在 WHERE 部分 [Date 2] 中替换为 Date() (使用今天的日期而不是控件中的日期)时,它可以正常工作....

我需要使用 [Date 2] 我尝试重命名控件不断出现相同的错误,尝试删除 [QR Code] 以声明只有一个控件仍然不起作用...

(完整代码如下:

'On Error Resume Next

[Date 2] = Date

Dim rs As DAO.Recordset
Dim strOutput As String
Dim i As Integer
'Dim Date2 As Date

DDA3 = "SELECT DDAs.[QR Code], DDAs.[Drug Name and Strength],DDAs.[Dispensing Date], DDAs.[Amount Prescribed], DDAs.[Prescription Date], DDAs.[Record Number], DDAs.[NOT DISPENSED] " _
& "FROM DDAs " _
& "WHERE ([DDAs].[QR Code] = [QR Code] AND [DDAs].[Dispensing Date] = [Date 2] AND [DDAs].[NOT DISPENSED] = No) " _

'OR ([DDAs].[QR Code]=[QR Code] AND [DDAs].[Dispensing Date]= [DateMod] AND [DDAs].[NOT DISPENSED]= No) " _

Set rs = CurrentDb.OpenRecordset(DDA3)

With rs
'test for empty recordset
If Not .EOF And Not .BOF Then
    .MoveLast
    .MoveFirst
Dim cdg As String
cdg = UCase([Forms]![POYC].[txtEnterState6])

    For i = 0 To (.RecordCount - 1)
        'test for last record
        If i = (.RecordCount - 1) Then
            'last record
            strOutput = strOutput + ![Amount Prescribed] + " " + ![Drug Name and Strength]
        Else
            'all other records
            strOutput = strOutput + ![Amount Prescribed] + " " + ![Drug Name and Strength] + vbCrLf
        End If

        .MoveNext
    Next
End If
.Close
End With
'execute messagebox
MsgBox "The following DDA prescriptions for " & [cdg] & " have been dispensed:" & vbCrLf & vbCrLf & UCase([strOutput]), vbInformation + vbOKOnly, "DDA records!"
Set rs = Nothing

我有一些代码,我故意告诉访问暂时忽略,直到我找到解决方案....我计划将 or 部分添加到 where 语句中,以便它通过 Date() 或 [Date 2] 运行查询

有任何想法吗?

标签: vbams-access

解决方案


尝试在日期之前和之后使用 #

DDA3 = "SELECT DDAs.[QR Code], DDAs.[Drug Name and Strength],DDAs.[Dispensing Date], DDAs.[Amount Prescribed], DDAs.[Prescription Date], DDAs.[Record Number], DDAs.[NOT DISPENSED] " _
& "FROM DDAs " _
& "WHERE ([DDAs].[QR Code] = [QR Code] AND [DDAs].[Dispensing Date] = #" & [Date 2] & "# AND [DDAs].[NOT DISPENSED] = No) " _

推荐阅读