vba - Access Query with Variable and Export to Excel -
when pressing button execute sql statement uses variable.
for example:
select a, b, data not null , b = '&<variable>&';
then result of query shall exported worksheet of excel file.
you might have create sql on fly , save docmd.outputto method export you. like.
option compare database option explicit sub exportquerytoexcel() dim dbobj dao.database, qdobj dao.querydef dim filepath string, sqltext string, yourvariable string set dbobj = currentdb yourvariable = "apple" sqltext = "select a, b, c not null , b = '" & yourvariable & "'" filepath = "c:\users\p\desktop\filename.xlsx" on error resume next dbobj .querydefs.delete "tmpdataqry" set qdfnew = .createquerydef("tmpdataqry", sqltext) .close end on error goto 0 docmd.outputto acoutputquery, "tmpdataqry", acformatxlsx, filepath set qdobj = nothing set dbobj = nothing end sub