Question : Export a query to a CSV file with a Query Filter/Where Condition

I have an access Query that I want to output to a CSV file. However, I want to filter the query first via code so the CSV file only has certain record in it.

I tried the below code and was able to filter the open Query but the output file has all OrderID's. My overall aim is to be able to select certain orders and then output them to a CSV file named the OrderID (e.g. 48670.csv in the below code example).

Can someone please help me with the way to filter or put a where condition on the query named 'SybizOutputQry'.

Thanks,

Andrew
1:
2:
3:
4:
DoCmd.OpenQuery ("SybizOutputQry")
    DoCmd.ApplyFilter , "[OrderID] = " & 48670
    
    DoCmd.TransferText acExportDelim, , "SybizOutputQry", "C:\Export\test.csv", False

Answer : Export a query to a CSV file with a Query Filter/Where Condition

you can do it by altering the querydef to include the filter

dim qd as dao.querydef, db as dao.database
dim oSql as string, nSql as string
set db=currentdb
set qd=db.querydefs("SybizOutputQry")
oSql=qd.sql
nsql=replace(qd.sql,";","")
nsql=nsql & " Where [OrderID] = 48670"
qd.sql=nSql

DoCmd.TransferText acExportDelim, , "SybizOutputQry", "C:\Export\test.csv", False
 
'to restore back the querydef
qd.sql=oSql

Random Solutions  
 
programming4us programming4us