Passing from Form to Query in Access/SQL/VB

本秂侑毒 提交于 2019-12-12 01:58:53

问题


Access 2007 / SQL / VB I have a query:

SELECT Count(*) AS CountOfCR1
FROM PData
WHERE (((PData.DestID)='CR1') And (((PData.AnswerTime)>=Starting)<Ending+1));

I am trying to pass the variables Starting and Ending to the above query from the below form:

Starting = StartDate & " " & StartTime
Ending = EndDate & " " & EndTime
On Error GoTo Err_Command5_Click
Dim stDocName As String
stDocName = "CountOfCR1 : Query"
DoCmd.OpenQuery stDocName, acNormal, acEdit
Exit_Command5_Click:
Exit Sub
Err_Command5_Click:
MsgBox Err.Description
Resume Exit_Command5_Click

How can I pass Starting and Ending from my form to my query?


回答1:


The usual way to do this is to refer to the form in the query:

SELECT Count(*) AS CountOfCR1
FROM PData
WHERE PData.DestID='CR1'
And PData.AnswerTime Between Forms!MyForm!StartDate + Forms!MyForm!StartTime
                     And Forms!MyForm!EndDate + Forms!MyForm!SEndTime

It is nearly always best to use a form with the recordsource set to the query or an sql string rather than opening a query. With a form, you can use the Where argument of the OpenForm method.



来源:https://stackoverflow.com/questions/3753346/passing-from-form-to-query-in-access-sql-vb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!