Kindly Sugesst

dongodu

Registered User.
Local time
Today, 12:56
Joined
Feb 21, 2012
Messages
13
SELECT * FROM cheques WHERE e_date like = #' & Text1.Text & '# and #' & Text2.Text & '#" -- To get the data between dates in a report.
Throughs run time error
Sugesst Plz
 
Where are Text1 and Text2 defined and what are their values when the error occurs?
You don't need the 'like' SQL keyword and the comarison should be '>=' the first date and '<=' the second date to retrieve the range.
 
Another option is BETWEEN

i.e.

Code:
SELECT table.column
FROM table
WHERE table.date BETWEEN #01/01/2011# AND #01/02/2011#
 
SELECT * FROM cheques WHERE e_date between #' & Text1.Text & '# and #' & Text2.Text & '#"
 

Attachments

  • New Picture (5).jpg
    New Picture (5).jpg
    14.4 KB · Views: 93
What is in fields Text1.Text and Text2.Text? These need to resolve to dates in the form mm/dd/yy for the query to work. Try a Debug.Print of your SQL statement just before you execute it to see what the resolved string looks like.
 
... sorry, date format should be mm/dd/yyyy (I left out the extra yy in my earlier response):rolleyes:
 
You didn't answer my question about what's in fields Text1.Text and Text2.Text? In any case, I had a closer look at the error message you posted and I think I see the problem (at least in part). The error message is
'e_date between #' & Text1.Text & '# and #' & Text2.Text & '#'
which is showing your two date field names as literals. the SQL statement should be written like this
Code:
Private Sub testSQL()
Dim strSQL As String, txtDate1 As String, txtDate2 As String
txtDate1 = Format(Now() - 10, "mm/dd/yyyy")
txtDate2 = Format(Now(), "mm/dd/yyyy")
'strSQL = "SELECT * FROM cheques WHERE e_date between #" & Text1.Text & "# and #" & Text2.Text & "#;"
strSQL = "SELECT * FROM cheques WHERE e_date between #" & txtDate1 & "# and #" & txtDate2 & "#;"
Debug.Print strSQL
End Sub
If you look at the Immediate window, you will see the string is constructed with the dates. I have used local variables for the date strings, but you would used your fields in exactly the same way.
 

Users who are viewing this thread

Back
Top Bottom