4

My program has 3 fields namely title, start date and end date for the user to enter. I wish to populate my listbox which is called "filteredResults" with results that contains records with tiles equal to the one entered by the user OR that falls into the range of the date.

May I know:

1) How can I populate the listbox with the recordset obtained from the query?

2) How can I compare the date inside the query?

Thanks a lot!

Private Sub FilterProj_Click()
Dim title As String, startDate As Date, endDate As Date
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT * FROM Project WHERE  ORDER BY [ProjectId] DESC")
filteredResults.Recordset = rs
Laughy
  • 1,949
  • 5
  • 20
  • 22

2 Answers2

11

For Question 1:

Populate directly from query:

Me.mylistbox.rowsource = _
  db.OpenRecordset("SELECT titles FROM Project _
           WHERE title = '" & parameter & "' ORDER BY [ProjectId] DESC")

Populate using Recordset object:

If (rs.RecordCount <> 0) Then 
   Do While Not rs.EOF
      mylistbox.Items.Add(rs.Fields(0).Value)
      rs.MoveNext()
   Loop
End IF

Another method:

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim qdf As DAO.QueryDef
Dim strSQL As String

strSQL = "SELECT titles from Projects _
       WHERE title = '" & parameter & "' ORDER BY [ProjectId] DESC"

Set db = CurrentDb
Set qdf = CurrentDb.CreateQueryDef("", strSQL)
qdf.Connect = strConn

Set rs = qdf.OpenRecordset()

Set Me.mylistbox.Recordset = rs

Question 2:

Not very clear. I assume you require a parameterized query. By setting a user's value into where, and clauses.

  db.OpenRecordset("SELECT titles FROM Project _ 
      WHERE title = '" & parameter & "' ORDER BY [ProjectId] DESC")

Or you may use the following article to set the parameter in query design view itself.

bonCodigo
  • 14,268
  • 1
  • 48
  • 91
  • Hi, may I know if the following expression is correct for an SQL statement?SELECT p.ProjectId, p.Title, p.Description, p.Client, p.StartDate, p.EndDate, p.DateCreated, p.Status, p.ProjectLead FROM Project AS p WHERE p.Title = '" & pjtName & "' OR ( p.StartDate <= '" & startDate & "' AND p.EndDate >= '" & endDate & "' ) ORDER BY p.ProjectId DESC;" Without the date comparison , it works fine, but with it, it throws an "Run time error '3464': Data type mismatch in criteria expression" message. startDate is a microsoft date/time picker control and p.StartDate is a date column in my database. – Laughy Jan 06 '13 at 09:16
  • 1
    You need to wrap dates with `#`. Try this for both date parameters please. `p.StartDate <= # '" & startDate & "' # AND ...` I am on mobile so unable to give an example. – bonCodigo Jan 06 '13 at 10:06
  • @Laughy Check this out please http://www.dbforums.com/microsoft-access/1617757-compare-date-ms-access-db.html – bonCodigo Jan 06 '13 at 10:17
  • Ah, I am comparing Microsoft Date/Time picker control value with a date in a table. I have done: startDate = Format(Me.ProjStartDate.Value, "Short Date") to ensure that their formats are the same already. I have changed my query to be: strSQL = "SELECT p.ProjectId, p.Title, p.Description, p.Client, p.StartDate, p.EndDate, p.DateCreated, p.Status, p.ProjectLead FROM Project AS p WHERE p.Title = '" & pjtName & "' OR ( p.StartDate <= #'" & startDate & "'# AND p.EndDate >= #'" & endDate & "'# ) ORDER BY p.ProjectId DESC;" but it gives me a syntax error in date in query expression. What's wrong? – Laughy Jan 06 '13 at 10:23
  • @Laughy if you are using a datepicker then you really nothing much to worry. Check my post here. http://stackoverflow.com/questions/13934705/support-needed-with-vba-programming-for-a-pop-up-calendar/13935299#13935299 BTW `'` is for string parameter and `#` for dates. I'll give it a shot when I get front of a machine. – bonCodigo Jan 06 '13 at 10:33
  • Ahhh it works now after removing ' and adding #! Thanks a million! – Laughy Jan 06 '13 at 13:48
  • @Laughy Anytime :) Yay you got it worked :) BTW do check on that last post to make your life easier though. When you use `parameterized queries`, the wizard is a *real help*. Once you use the wizard, you can simply look into the `sql view` to get the query out as well. – bonCodigo Jan 06 '13 at 13:52
3

Set the row source property for your filteredResults list box to a query which uses a strategy similar to this.

SELECT p.ProjectId, p.title, p.date_field
FROM [Project] AS p
WHERE
    p.title = Forms![YourForm]![txtTitle]
    OR
        (
                p.date_field >= Forms![YourForm]![txtStartDate]
            AND p.date_field <= Forms![YourForm]![txtEndDate]
        )
ORDER BY p.ProjectId DESC

Whenever you want to update the contents of filteredResults to reflect changes to the user-submitted title, start date, and/or end date, you can requery the list box:

Me!filteredResults.Requery

You could trigger the requery from a command button, or from the after update events of the text boxes.

Note I assumed your Project table includes a text field named title and a Date/Time field named date_field. I made up my own names (txtTitle, txtStartDate, txtEndDate) for the text boxes. Set the Format property to "General Date" for txtStartDate and txtEndDate. Substitute the name of your form for YourForm.

HansUp
  • 95,961
  • 11
  • 77
  • 135
  • 1
    Hi thanks for the detailed explanations! In my table there is a StartDate and EndDate column and a Title column. In my form, I am using ActiveX control time and Date picker(SP6) to get the dates input from the user. I have come up with the below query but my form gives me a "Missing Operator Warning" whenever I open it. Is it wrong? Thanks! SELECT p.StartDate, p, p.Title, p.endDate FROM [Project] AS p WHERE p.Title = Forms!Search by project!ProjTitle OR ( p.StartDate <= Forms!Search by project!ProjStartDate AND p.EndDate >= Forms!Search by project!ProjStartDate) ORDER BY p.ProjectId DESC – Laughy Jan 01 '13 at 08:55
  • 1
    Hi, thanks for your clear instructions and guidance! I have included [Search by project] in it. However now the form prompts me for the value of p,Forms!Search by project!ProjTitle,Forms!Search by project!ProjStartDate,Forms!Search by project!ProjEndDate in a pop up box each time i open it! – Laughy Jan 02 '13 at 15:45
  • 1
    With the query SQL using `Forms![Search by project]!ProjTitle`, I can't find any way to make Access ask for `Forms!Search by project!ProjTitle` as a parameter. – HansUp Jan 02 '13 at 18:21
  • 1
    Hi, thanks for your help anyways. I shall post another question regarding the parameter pop up box. Your answer has helped me greatly. thanks! – Laughy Jan 04 '13 at 09:29