' VBS Script to convert Access Query to SQL
Dim accessApp, db, query, sqlQuery
' Create Access Application object
Set accessApp = CreateObject("Access.Application")
' Open the Access database (指定するデータベースのパスに変更してください)
accessApp.OpenCurrentDatabase "C:\path\to\your\database.accdb"
' Get the query name (クエリの名前を指定)
query = "YourQueryName"
' Get the SQL of the query
Set db = accessApp.CurrentDb()
sqlQuery = db.QueryDefs(query).SQL
' Output the SQL query
WScript.Echo sqlQuery
' Clean up
db.Close
accessApp.Quit
Set db = Nothing
Set accessApp = Nothing
' VBS Script to save Access Query SQL to a text file on Desktop
Dim accessApp, db, query, sqlQuery, fso, textFile, desktopPath
' Create Access Application object
Set accessApp = CreateObject("Access.Application")
' Open the Access database (指定するデータベースのパスに変更してください)
accessApp.OpenCurrentDatabase "C:\path\to\your\database.accdb"
' Get the query name (クエリの名前を指定)
query = "YourQueryName"
' Get the SQL of the query
Set db = accessApp.CurrentDb()
sqlQuery = db.QueryDefs(query).SQL
' Define the path to save the text file on Desktop
desktopPath = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\QuerySQL.txt"
' Create FileSystemObject to write to the text file
Set fso = CreateObject("Scripting.FileSystemObject")
Set textFile = fso.CreateTextFile(desktopPath, True)
' Write the SQL query to the text file
textFile.WriteLine sqlQuery
' Close the text file
textFile.Close
' Output a message
WScript.Echo "SQL has been saved to " & desktopPath
' Clean up
db.Close
accessApp.Quit
Set textFile = Nothing
Set fso = Nothing
Set db = Nothing
Set accessApp = Nothing