Docmd.openreport Where Clause syntax - sql

I am trying to save individual records in a report to PDF files. using Access 2010. I got this to work, but I need to put a start date and end date in, and I can't seem to figure out the syntax. Here is what I have so far:
DoCmd.OpenReport "Rpt Form Responses", acViewReport, , "[Facility Number]=" & temp & _ And [Service Date] Between & begindate And enddate;
The top line works fine but when I add the second I can't get it to work. I have tried all the qualifiers', ", and # and many different variations of syntax with no luck.
begindate and enddate are strings that I capture from an input box. Should I change these to a date? I have done a lot of reading on here and based on suggestions I thought about putting a text box on the form for begin and end but I think I would rather do the inputbox.

Are you sure the syntax you posted above is accurate, because it is not correct.
I am assuming 'temp' is numeric, else you need to enclose in single-quotes;
Tthe continuation line is missing the leading " followed by a space;
Missing quotes, spaces around your BETWEEN and AND
You can get away with strings for the dates as long as you did something like:
begindate = #1/1/2005#
enddate = #1/1/2012#
Here is what I believe your syntax should look like (I tested and it works):
DoCmd.OpenReport "Rpt Form Responses", acViewReport, , "[Facility Number]=" & temp & _
" And [Service Date] Between #" & begindate & "# And #" & enddate & "#"

Related

Access VBA SQL query searching between dates

I have a some VBA code that takes in a date from and date to fields from two date picker values in textboxes.
However, when searching between the two dates I receive the 'Enter Parameter Value' prompt asking for input.
Dim SQLAllReject As String
Dim strDateFrom As String
Dim strDateTo As String
strDateFrom = Format(txtDate.Value, "mm/dd/yyyy")
strDateTo = Format(txtDateTo.Value, "mm/dd/yyyy")
When running directly in the Query Design wizard, searching between dates works fine:
WHERE (((XXXXXXXXXXXXXXXXXXXX.Date) Between #10/1/2021# And #10/27/2021#))
What is wrong here? How can I force Access/VBA to take the dates I've specified and search between them and NOT display the 'Enter Parameter Value' prompt?
When I debug the query and step through, it gives exactly the same dates as running it in Query Design view.
FYI if I use one date and the LIKE operator, this works fine.
SQLAllReject = "SELECT dbo_vw_busobj_file_rejections_load_access_temp5_copy.HashKey AS [ID], dbo_vw_busobj_file_rejections_load_access_temp5_copy.Reject_Date AS [Date], " & _
"FROM dbo_vw_busobj_file_rejections_load_access_temp5_copy " & _
"WHERE (((dbo_busobj_file_rejections_load_access_temp5_copy.Reject_Date) Between #" & strDateFrom & "# And #" & strDateTo & "#)) " & _
"ORDER BY dbo_vw_busobj_file_rejections_load_access_temp5_copy.Reject_Date DESC;"
Ok I somehow fixed it... the query string was trying to select from a view rather than the table and for some odd reason it was causing the parameter prompt.
I have changed dbo_vw_busobj_file_rejections_load_access_temp5_copy to dbo_busobj_file_rejections_load_access_temp5_copy table and managed to get it to work after doing some string to date conversion when selecting dates from the textboxes.

Why is this asking me for an Expression?

I'm trying to open a report from a form, where the user fills in a start and end date. My code is:
Docmd.OpenReport (rptAllSalesByDate, acViewReport, ,
"DateOfTransaction >= #" & me.txtStartDate & "# and DateOfTransaction =< #" & me.txtEndDate & "#",,,)
The above was broken into 2 lines for easier readability, but it's actually on one line.
It keeps saying it's expecting an expressing and highlighting the closing parenthesis.
Any ideas what I'm missing? I know my Access is rusty.
Do not leaving trailing commas when no parameters are provided for those arguments. This will trigger the indicated error. So will parentheses when not executing a function.
Consider:
Docmd.OpenReport "rptAllSalesByDate", acViewReport, ,
"DateOfTransaction BETWEEN #" & Me.txtStartDate & "# AND #" & Me.txtEndDate & "#"

How can I get this code to work for mixed character/number or date fields?

I don't really know anything about coding, but found this useful code on a forum, but it's not working for any fields that have numbers or dates. I need the code to work for fields that have numbers and characters mixed and also for date fields. Any help would be appreciated. Here is the code:
Private Sub cboAmount_AfterUpdate()
Dim myAmount As String
myAmount = "Select * from Records where ([Amount] = '" & Me.cboAmount & "')"
Me.Records_subform.Form.RecordSource = myAmount
Me.Records_subform.Form.Requery
End Sub
Basically I have a search form that populates the search results in the attached subform so that searched records can be edited quickly from the search form.
Assuming [Amount] is numeric, you cannot put single quotes around it... It should look like this:
([Amount] = " & Me.cboAmount & ")"
if it's a date field, then you use the # sign:
([MyDate] = #" & Me.DateBox & "#)"

Problem using where with date in SQL Statement

I am trying to execute an SQL statement in a VBA script. I have gotten the script to run, but it ignores the where with a date filter.
I have researched and tried every option I can find, but just cant seem to get it to work.
Set rs = conn.Execute("Select [adjustment_number], [status], [tax_adjusted],[amount], [gl_date],[creation_date],[apply_date],[comments],[type],[adjustment_type],[dbo].[Code_Combinations].[segment1], [dbo].[Code_Combinations].[segment10],[dbo].[Code_Combinations].[segment11],[dbo].[Code_Combinations].[segment12],[dbo].[Code_Combinations].[code_combination] FROM [dbo].[AR_ADJUSTMENTS_ALL] " & _
"left outer join [dbo].[Code_Combinations] on [dbo].[AR_ADJUSTMENTS_ALL].[CODE_COMBINATION_ID] = [dbo].[Code_Combinations].[CODE_COMBINATION_ID] where [gl_date] >= " & gldate & ";")
(Copying this from my comment above, into an answer, since this solved your problem)
SQL Server? Put single quotes around your date in the WHERE clause.
where [gl_date] >= '" & gldate & "';"
Assumes that gldate is a valid date, and [gl_date] is of a date datatype.

Microsoft Query in Excel VBA - how to pass date filters

I am really hoping someone here could help me with the issue I have spent hours trying to fix with no result.
I am trying to establish a data connection with a csv file using MS query in Excel VBA. I need to filter the data out from the csv file into the spreadsheet by applying a date filter on a certain column. When the date is fixed (i.e. hardcoded in VBA), the connection works absolutely fine. However, I would like the date to be a user input and that's where I am facing problems. Basically, I am not sure how to pass a date variable to the connection.
When the macro works fine, the SQL statement looks like this:
.CommandText = "SELECT * FROM " & csvName & " WHERE SECTYPE='GS' AND LAST TRADED DATE={ts '2016-01-29 00:00:00'}"
When I try to pass the date via variable sValnDate, I get 'SQL syntax error':
.CommandText = "SELECT * FROM " & csvName & " WHERE SECTYPE='GS' AND LAST TRADED DATE={ts " & sValnDate & "}"
I have tried several configurations of the variable. I have tried to pass it as a date, a string exactly as in the correct command, a date formatted as required in the correct command, keeping and removing the curly brackets with each format of the variable etc, but nothing worked.
I have just presented here 1 statement to keep things simple. However, if you need to see the entire block (not more than 15-20 lines), please let me know.
Thanks in advance
PS: just looked at the preview. Somehow `` around LAST TRADED DATE have been removed here.
Assuming that sValnDate is a string that looks like 2016-01-29 00:00:00 then you are simply missing the ticks (aka single quotes or Chr(39)).
.CommandText = "SELECT * FROM " & csvName & _
" WHERE [SECTYPE]='GS' AND [LAST TRADED DATE]={ts '" & sValnDate & "'}"
If sValnDate is an actual date then format it like,
.CommandText = "SELECT * FROM " & csvName & _
" WHERE [SECTYPE]='GS' AND [LAST TRADED DATE]={ts '" & _
Format(sValnDate, "yyyy-mm-dd hh:mm:ss" & "'}"