The following method exports all available queries in an Access database into individual text files, which are called like the corresponding query.
Source Code
DAO does not provide the support ADODB gives you, therefore this method will only work with queries created and stored in a current MS Access database.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
' @Author - Alexander Bolte' @ChangeDate - 2016-03-15' @Description - exports all queries available in current database query definitions.' @Param trgDir - a String representing a target directory to export' all queries in current query definition.' @Returns true, if all queries have been successfully exported into provided target directory.FunctionexportQueries(ByValtrgDirAsString)AsBooleanDimsqlAsStringDimnameAsStringDimqAsObjectDimtrgFileAsString' Reference a query.ForEachqInCurrentDb.QueryDefs' Get the SQL from a referenced query as text.sql=q.sql' Get a queries name.name=q.name' Replace special characters in file name.trgFile=trgDir&"\"&VBATools.replaceSpecialCharacters(name)&".sql"' Delete the target file, if already existing.CallVBATools.deleteFileOnHD(trgFile)' Write the query text into a separate text file.CallVBATools.writeLineToTextFile(trgFile,sql,False)NextEndFunction
Resources
The method writeLineToTextFile is not a standard VBA method, but can be found at following URL.
The target encoding should not be UTF-16LE but ASCII, if you intend to use a versioning tool like GIT to keep track of changes in MS Access queries.