Hi (again).
Can anyone help me with a little problem with RecordSets?
I have a table called "Issues", from which I wish to pull the fields "NI" (trainee National Insurance number), "Survey" (the survey type, 1-4) and a completion status (yes/no field). I am querying on these fields to return a record count, showing the number of issues each trainee has raised on each survey.
If I do the query in the Query Builder, I get the following SQL:
SELECT Issues.NI, Issues.Survey, Issues.Completed
FROM Issues
WHERE (((Issues.NI) Like "XY123456Z" AND ((Issues.Survey)=1) AND ((Issues.Completed)=No));
and this works fine.
I am using the following code to run the query in an AfterUpdate event of a listbox, so that when the user selects a trainee from the list, the number of issues raised and the number of unresolved (i.e. Completed=No) issues are displayed in a second listbox:
---
'' Display the total number of issues raised.
For i = 1 To 4
qry = "SELECT [NI], [Survey] FROM Issues " & _
"WHERE (([NI] Like '" & lstTrainees & "') AND " & _
"([Survey] = " & CStr(i) & ");"
Set rst = dbs.OpenRecordset(qry, dbOpenDynaset)
AddListItem lstIssues, CStr(rst.RecordCount)
Next i
'' Display the number of unresolved issues.
For i = 1 To 4
qry = "SELECT [NI], [Survey], [Completed] FROM Issues " & _
"WHERE (([NI] Like '" & lstTrainees & "') AND " & _
"([Survey] = " & CStr(i) & " AND ([Completed] = No));"
Set rst = dbs.OpenRecordset(qry, dbOpenDynaset)
AddListItem lstIssues, CStr(rst.RecordCount)
Next i
---
The queries are both more-or-less exactly the same, but for some reason, the second loop (unresolved) shows exactly the same result as the first, as if it's ignoring the ([Completed] = No) part of the WHERE SQL section.
As they say, "two heads are better than one". If anyone has any ideas why it might be doing this, I would be grateful if you'd let me know!
Thanks.
FYI,
* lstTrainees is the listbox displaying the trainee names, and has bound column of NI, the trainee national insurance number.
* lstIssues is the list to which the recordcount is being output. It is a four-column, two row listbox.
* The "AddListItem" is a self-defined function to add string data to a listbox (it's in the FAQ!).
Can anyone help me with a little problem with RecordSets?
I have a table called "Issues", from which I wish to pull the fields "NI" (trainee National Insurance number), "Survey" (the survey type, 1-4) and a completion status (yes/no field). I am querying on these fields to return a record count, showing the number of issues each trainee has raised on each survey.
If I do the query in the Query Builder, I get the following SQL:
SELECT Issues.NI, Issues.Survey, Issues.Completed
FROM Issues
WHERE (((Issues.NI) Like "XY123456Z" AND ((Issues.Survey)=1) AND ((Issues.Completed)=No));
and this works fine.
I am using the following code to run the query in an AfterUpdate event of a listbox, so that when the user selects a trainee from the list, the number of issues raised and the number of unresolved (i.e. Completed=No) issues are displayed in a second listbox:
---
'' Display the total number of issues raised.
For i = 1 To 4
qry = "SELECT [NI], [Survey] FROM Issues " & _
"WHERE (([NI] Like '" & lstTrainees & "') AND " & _
"([Survey] = " & CStr(i) & ");"
Set rst = dbs.OpenRecordset(qry, dbOpenDynaset)
AddListItem lstIssues, CStr(rst.RecordCount)
Next i
'' Display the number of unresolved issues.
For i = 1 To 4
qry = "SELECT [NI], [Survey], [Completed] FROM Issues " & _
"WHERE (([NI] Like '" & lstTrainees & "') AND " & _
"([Survey] = " & CStr(i) & " AND ([Completed] = No));"
Set rst = dbs.OpenRecordset(qry, dbOpenDynaset)
AddListItem lstIssues, CStr(rst.RecordCount)
Next i
---
The queries are both more-or-less exactly the same, but for some reason, the second loop (unresolved) shows exactly the same result as the first, as if it's ignoring the ([Completed] = No) part of the WHERE SQL section.
As they say, "two heads are better than one". If anyone has any ideas why it might be doing this, I would be grateful if you'd let me know!
Thanks.
FYI,
* lstTrainees is the listbox displaying the trainee names, and has bound column of NI, the trainee national insurance number.
* lstIssues is the list to which the recordcount is being output. It is a four-column, two row listbox.
* The "AddListItem" is a self-defined function to add string data to a listbox (it's in the FAQ!).