MS Access运行时错误' 3061'预期参数太少2

时间:2018-01-30 10:04:05

标签: sql vba ms-access count

我一直在玩这个vba / sql代码已经有一段时间了,我无法弄清楚为什么我一直收到错误' MS Access运行时错误' 3061'预期的参数太少2',有什么我想念的吗?我正在尝试计算在review_programme表中有多少记录,其中包含' B' (这是一个连接表的一部分,它是BFMA_Tasklist并在任务字段上加入),它位于从表单输入的两个日期之间。然后,结果将显示在表单的文本框中。

Private Sub cmdstats_Click()
Dim sSQL As String
Dim db As Database
Dim rs As DAO.Recordset

sSQL = "SELECT Count(*) AS [CountOfScheduleB] " & _
"FROM [BFMA_TaskList] INNER JOIN [Reviews_Programme] ON [BFMA_TaskList].[Task] = [Reviews_Programme].[Task] " & _
"WHERE [BFMA_TaskList].[Schedule]=""B"" AND [Reviews_Programme].[Planned_Date] Between Me![txtstatsfrom] And Me![txtstatsto];"


Set db = CurrentDb
Set rs = db.OpenRecordset(sSQL)


If rs.RecordCount > 0 Then
   Me.Text2 = rs![CountOfScheduleB]
Else
   Me.Text2 = "N/A"
End If

Set rs = Nothing
Set db = Nothing


End Sub

任何帮助将不胜感激。谢谢。

2 个答案:

答案 0 :(得分:1)

试试这个:

sSQL = "SELECT Count(*) AS [CountOfScheduleB] " & _
"FROM [BFMA_TaskList] INNER JOIN [Reviews_Programme] ON [BFMA_TaskList].[Task] = [Reviews_Programme].[Task] " & _
"WHERE [BFMA_TaskList].[Schedule]=""B"" AND [Reviews_Programme].[Planned_Date] Between #" & Format(Me![txtstatsfrom],"mm\/dd\/yyy") & "# And #" & Format(Me![txtstatsto],"mm\/dd\/yyy") & "#;"

答案 1 :(得分:0)

首先,currentDB在哪里?在调用它的表之前,你需要为此建立数据库连接。

第二,我认为'B'就够了,不需要“”B“”

你需要使用'#'作为日期参数。并格式化文本cos我们都知道所有文本都是字符串。你需要转换它。就像格式(数据,“格式为”)一样,如果这种格式与数据库相同,请记住它会更好。

所以代码应该是:

Private Sub cmdstats_Click()
Dim sSQL As String
Dim db As Database
Dim rs As DAO.Recordset

sSQL = "SELECT Count(*) AS [CountOfScheduleB] " & _
"FROM [BFMA_TaskList] INNER JOIN [Reviews_Programme] ON [BFMA_TaskList].[Task] = [Reviews_Programme].[Task] " & _
"WHERE [BFMA_TaskList].[Schedule]='B' AND [Reviews_Programme].[Planned_Date] Between #' & format(Me![txtstatsfrom],"dd/mm/yyyy") & '# And  #' & format(Me![txtstatsto],"dd/mm/yyyy") & #';"


'Set db = CurrentDb
 Set db = OpenDatabase("Your database)
 Set rs = db.OpenRecordset(sSQL)


If rs.RecordCount > 0 Then
   Me.Text2 = rs![CountOfScheduleB]
Else
   Me.Text2 = "N/A"
End If

Set rs = Nothing
Set db = Nothing


End Sub

Private Sub cmdstats_Click()
Dim sSQL As String
Dim db As Database
Dim rs As DAO.Recordset

sSQL = "SELECT Count(*) AS [CountOfScheduleB] " & _
"FROM [BFMA_TaskList] INNER JOIN [Reviews_Programme] ON [BFMA_TaskList].[Task] = [Reviews_Programme].[Task] " & _
"WHERE [BFMA_TaskList].[Schedule]='B' AND [Reviews_Programme].[Planned_Date] Between '" & format(Me![txtstatsfrom],"dd/mm/yyyy") & "' And  '" & format(Me![txtstatsto],"dd/mm/yyyy") & "';"


'Set db = CurrentDb
 Set db = OpenDatabase("Your database)
 Set rs = db.OpenRecordset(sSQL)


If rs.RecordCount > 0 Then
   Me.Text2 = rs![CountOfScheduleB]
Else
   Me.Text2 = "N/A"
End If

Set rs = Nothing
Set db = Nothing


End Sub

如果你确定你做了什么,你可以删除“我”。我是形式的。有时我们需要“我”,有时候不需要。

快乐编码。