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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With