views:

38

answers:

1

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?

A: 

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.

Remou
PARDON THE FORMATING, I DONT KNOW WHY ITS NOT WORKING. My SQL Statement reads: SELECT Count(*) AS CountOfCR1 FROM PData WHERE PData.DestID='CR1' AND PData.AnswerTime Between Forms!MainForm!Starting And Forms!MainForm!Ending; Starting and Ending are date/time that is set in MainForm It still doesnt seem to work.
Seth
In what way is it not working? No data? Incorrect data? Error message? You have : PhoneData WHERE PData.DestID this cannot be correct, either the table is called PData or it is called PhoneData.
Remou
Its called Pdata, thats not the error. i've changed that. The query reports the wrong answer= 0. It should respond with a few a number above records as there are a few records that fit the criteria of CR1 and between the two dates entered in the form.
Seth
Seth
Try adding the forms reference to the select to see if the data is as expected, like so: SELECT Count(*) AS CountOfCR1, [Forms]![MainForm]![Starting] AS SDate, [Forms]![MainForm]![Ending] AS EDateFROM PData
Remou
That didnt help. When i msgbox my "starting" variable, it shows that it is formated correctly (i think). 9/2/2010 11:10:17 AM is an example.....and here is my current SQL statement(ive changed the tables, so PhoneData is now correct): SELECT Count(*) AS CountOfCRC1, [Forms]![MainForm]![Starting] AS SDate, [Forms]![MainForm]![Ending] AS EDate FROM PhoneData WHERE PhoneData.DestID='CRC1' And PhoneData.AnswerTime Between Forms!MainForm!Starting And Forms!MainForm!Ending;
Seth
Figured it out. I created a new text box and called it "Starting". I just made in not visible. I dont know why this needed to exist instead of just using the dim name Starting alone.
Seth
I assumed that starting and ending were textboxes. You cannot refer to variables in Jet SQL, other than through a function, which is not a good idea. Think of Access as two parts - the Jet/ACE database with queries, and the deployment tool with forms, code and so forth.
Remou
Remou
Thanks for all your help. I could not have done it without you.
Seth