简体   繁体   中英

SQL Query in Access not applying WHERE clause

I am an amateur vba programmer currently struggling with the following:

I wrote a simple SQL Query for my VBA program to extract data from an existing Query (GroupedData) in Access:

Dim strSQL as string
Dim db as dao.database
dim rs as dao.recordset

strSQL = "SELECT GroupedData.EmployeeID, GroupedData.End, GroupedData.LOB, GroupedData.Position, GroupedData.WorkStatus " & _
    "FROM GroupedData WHERE (((GroupedData.End) Is Null) AND ((GroupedData.Position)=0) AND ((GroupedData.WorkStatus)=0));"

Set db = OpenDatabase(MY_DB, dbOpenDynaset)
Set rs = db.openrecordset(strSQL, dbOpenSnapshot)

Do While Not rs.EOF
    ListBox1.AddItem rs!EmployeeID
    rs.MoveNext
Loop

So my problem is that SQL is running smoothly but is not applying any filters (that is where clause - it produces records where workstatus and position contain values above 0)

I have done such queries in the past without having any issues. This time however I am stuck

Any help would be much appreciated!

Thank you

Ok i have fixed the issue. Here is what happened: I have made a separate function that was retrieving Employee Name based on EmployeeID. But in that function i had recordset and database declared the same way, that is: rs and db. What happened when i ran the query was it was taking the first record filtered but then the rest of them were unfiltered because they were based on the recordset within the function (which is of course all of the employees) Such a silly mistake :) Thanks anyways

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM