Does anybody know if there is a way to execute a series of sql commands from Excel? Everything that I have found requires a destination and that the query return data.
first in the project window, Tools/References menu enable
Microsoft Access 9.0 Object Library
and
Microsoft DAO 3.6 Object Library
(office 2000)
in a module call this main sub. The "apAcc.DoCmd.RunSQL strSql" row runs an sql (string) command yielded from a text file with the function (ReadSQLString) bellow. You can also use an sql string stored in a cell (worksheets(1).range("d2".value ...) or other.
You can get a recordset object with openrecordset function.
You must set db (and sql) path..
Sub main()
Dim strSql As String
Dim dbWrk As Database
Dim apAcc As New Access.Application
Dim rstWrk As DAO.Recordset
Set apAcc = CreateObject("Access.Application"
Call apAcc.OpenCurrentDatabase("D:\TEMP\dk.mdb", False)
Set dbWrk = apAcc.CurrentDb
Set rstWrk = dbWrk.OpenRecordset("SELECT * FROM TABLE1"
rstWrk.MoveLast
rstWrk.MoveFirst
MsgBox rstWrk.RecordCount
End Sub
Public Function ReadSQLString(Path As String, FileName As String) As String
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Dim fs, f
Set fs = CreateObject("Scripting.FileSystemObject"
Set f = fs.OpenTextFile(Path + FileName, ForReading, TristateFalse)
SQLSTring = ""
Do While f.AtEndOfStream <> True
SQLSTring = SQLSTring + f.ReadLine + " "
'Debug.Print SQLSTring ' Print data to the Immediate window.Loop
Loop
f.Close
ReadSQLString = SQLSTring
End Function
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.