No problem. You don't have to set up your Access query to accept a variable. You can pass variables after the WHERE statement in the SQL when you open the query from VB. In DAO it would be something like this:
It doesn't really matter whether you are using DAO, ADO, etc. Just put the criteria after the WHERE statement in an SQL to pull up the records that match that criteria.VB Code:
iRequestedWeekNo = 5 sSQL = "SELECT * FROM MyQueryName WHERE ((WeekNo)='" & iRequestedWeekNo & "');" Set rs = db.OpenRecordset(sSQL)
Then you can open a file in Excel and use the Excel CopyFromRecordset method to put your recordset data into Excel.
Here's a hint: You don't even have to have a query in Access. You can do the whole query from VB by setting the SQL when you open the recordset. If you aren't sure what the SQL should be, create a query in Access then switch to SQL view. It will show you the SQL used to create the query. Sort of like recording a macro in Excel or Word.![]()





Reply With Quote