473,421 Members | 1,616 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,421 software developers and data experts.

Query by date from form

80
Hi, I've been trying to make a report that will show the deliverys to be made between 2 dates, or the deliverys to be made after a certian date, or the deliveries that were made before a certian date, based on criteria in a form.

At this point I have the report based on the query, and the dates bieng pulled from the form.

I can't seem to get the syntax right. In the query's WHERE statement I have <[forms]![Deliverys]![DelDate]. In the form, the field DelDate's format is set to short Date. That works, but if I leave it this way then i have to make 2 more queries and 2 more reports to be able to have the options im after.

I tried making an unbound textbox and if I hit before in the combo box and typed 08/01/07 in the DelDate field, then the textbox would would display <#08/01/07#. From there I called the field into the criteria of the query, but got an error of "This expression is typed incorrectly, or its too complex to be evaluated. etc...". But If i just type <#08/01/07# directly into the criteria of the query everything works great...

So, basically how can I get that information into the query?
Aug 23 '07 #1
3 2064
Scotter, can you supply the SQL code for your query? This sounds familiar - but I need more specific details.

Thanks,
Sophie
Aug 23 '07 #2
Scotter
80
Hi Sophie, I've been messing around with the query and the vb in the form, and just now I've gotten to the point where I have some hope. I went and deleted all the sql from the query so its basically just a blank saved query now. This is the VB code

Expand|Select|Wrap|Line Numbers
  1. Private Sub btnRun_Click()
  2.  
  3. Dim queryis As DAO.QueryDef
  4. Dim db As DAO.Database
  5. Dim sign As String
  6. Dim del As Date
  7. Dim pickup As Date
  8. Dim strCriteria As String
  9. Dim strSQL As String
  10.  
  11.  
  12. Set db = CurrentDb()
  13. Set queryis = db.QueryDefs("Delivery")
  14.  
  15. del = Me.DelDate
  16.  
  17. If Me.Combo8.Value = "before" Then
  18.       sign = "< "
  19.       strCriteria = strCriteria & sign & del
  20.       strSQL = "SELECT * FROM Orders " & _
  21.                     "WHERE [Delivery Date] = " & strCriteria & ";"
  22. End If
  23.  
  24. queryis.SQL = strSQL
  25. DoCmd.OpenQuery "Delivery"
  26.  
  27. End Sub
  28.  
I only have the one if statement, so its only for orders before myDate. But the problem I'm having with this is when I run this I get an error that says

Syntax error(missing operator) in query expression '[Delivery Date] = <8/1/2007'

Yet If I type <8/1/07 in the criteria of the query under delivery date everything works fine. What am I doing wrong?

Thanks for the help.
Aug 23 '07 #3
Scotter
80
Ok, I figured it out. I got it to work by changing the variable type of the date to a string and adding #'s, and i had to change
Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT * FROM Orders " & _
  2.                     "WHERE [Delivery Date] = " & strCriteria & ";"
  3.  
to
Expand|Select|Wrap|Line Numbers
  1. srtsql = "SELECT * FROM Orders " & _
  2.              "WHERE [Delivery Date] " & strCriteria & ";"
  3.  
the = sign was messing things up. Here is the code that is working for me now.
Expand|Select|Wrap|Line Numbers
  1. Private Sub btnRun_Click()
  2.  
  3. Dim sign As String
  4. Dim Date1 As String
  5. Dim date2 As String
  6. Dim strCriteria As String
  7. Dim strSQL As String
  8. Dim queryis As DAO.QueryDef
  9. Dim db As DAO.Database
  10.  
  11. Set db = CurrentDb()
  12. Set queryis = db.QueryDefs("Delivery")
  13.  
  14.  
  15. If Me.Combo8.Value = "before" Then
  16.     Date1 = Me.txt1
  17.     Date1 = "#" & Date1 & "#"
  18.     sign = "< "
  19.     strCriteria = strCriteria & sign & Date1
  20.     strSQL = "SELECT * FROM Orders " & _
  21.              "WHERE [Delivery Date] " & strCriteria & ";"
  22. End If
  23. If Me.Combo8.Value = "after" Then
  24.     Date1 = Me.txt1
  25.     Date1 = "#" & Date1 & "#"
  26.     sign = ">"
  27.     strCriteria = strCriteria & sign & Date1
  28.     strSQL = "SELECT * FROM Orders " & _
  29.              "WHERE [Delivery Date] " & strCriteria & ";"
  30. End If
  31. If Me.Combo8.Value = "between" Then
  32.     Date1 = Me.txt1
  33.     Date1 = "#" & Date1 & "#"
  34.     sign = "Between "
  35.     date2 = Me.txt2
  36.     date2 = "#" & date2 & "#"
  37.     strCriteria = strCriteria & sign & Date1 & " AND " & date2
  38.     strSQL = "SELECT * FROM Orders " & _
  39.              "WHERE [Delivery Date] " & strCriteria & ";"
  40. End If
  41.  
  42.  
  43. queryis.SQL = strSQL
  44. DoCmd.OpenQuery "Delivery"
  45. End Sub
  46.  
Also I made the second textbox disabled so you couldnt use it unless the combobox was set to between.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Combo8_AfterUpdate()
  2.  
  3. If Me.Combo8.Value = "before" Then
  4. Me.txt2.Enabled = False
  5. End If
  6.  
  7. If Me.Combo8.Value = "after" Then
  8. Me.txt2.Enabled = False
  9. End If
  10.  
  11. If Me.Combo8.Value = "between" Then
  12. Me.txt2.Enabled = "true"
  13. End If
  14.  
  15. If IsNull(Combo8) = True Then
  16. Me.txt2.Enabled = "false"
  17. End If
  18.  
  19. End Sub
  20.  
Aug 23 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Harvey | last post by:
Hi, I try to write an asp query form that lets client search any text-string and display all pages in my web server that contain the text. I have IIS 6.0 on a server 2003. The MSDN site says...
1
by: Michael DeLawter | last post by:
Using Access 2002. I have a chart in a report that is currently based on a query in which the user enters the start and end date for the chart to display. Both the start and end dates have been...
3
by: Steve | last post by:
Form FrmRestock's recordsource is QryFrmRestock. The TransactionDate field's criteria is set ats: Forms!FrmRestock!LastXDays. LastXDays on the form is a combobox where the selections are 30, 60...
2
by: John | last post by:
Hi - I am trying to perform a simple append query, with no luck. I have a table (MktPrices) that has the following fields: BondID, PriceDate, Price. The objective is to allow the user to input a...
3
by: JC Mugs | last post by:
Help needed for project-Access 2002(office xp) PROBLEM: Figuring out how to lookup a record and DDate field in Table1 - Take that DDate-field data from record looked up and assign it to Date...
1
by: longtim | last post by:
I have been having endless difficulty creating reports/queries that set any relevent parameters from controls in forms. I am creating an application under access 2003 but will target access...
2
by: Mark Roughton | last post by:
I have a form where the users need to view records for various criteria, one of which is a date field on which they may wish to view all related data for the selected date, for all dates upto and...
2
by: Bill | last post by:
I have a 200 record database that includes a date/time field, AnnivDate, for a wedding anniversary. AnnivDate has nulls and some incorrect year data. I have been creating the Access database...
1
by: mbatestblrock | last post by:
I think I have a rather advanced question that I was hoping to find some good help with. I am still pretty new to VBA and I know that doesn't help my situation here. But here is what I am trying to...
19
by: phill86 | last post by:
Hi I am re-posting this thread because it has become very confusing and I have got some way to solving the problem so it is a slightly different question from the initial thread. here is the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.