-
I have an Excel workbook that I use for general SQL testing. It has a lot of specialized functions in it, but here is a stripped down version that will dump the results of an SQL Query directly to a new Excel Workbook. (Note:this is a "pass-through" query, not a stored query/view/whatever...)
You should be able to modify this to create what you are looking for.
[vba]'DataBase SQl tester
Option Explicit 'Delete this line if you get "Undeclared Variable" errors
Option Private Module
'Database contstants
Private Const DB_Path As String = "\\10.2.56.24\shared\"
Private Const DB_Name As String = "DB.mdb"
Private Const AccDBConnStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & DB_Path & DB_Name
Private strSQL As String
Public conAcc As Object
'
Private Sub psubTestingSQL()
Dim rst As Object
OpenAccConn
Set rst = CreateObject("ADODB.RecordSet")
rst.CursorLocation = adUseClient
strSQL = "SELECT stuff" _
& "FROM stuff" _
& "WHERE stuff" _
& "ORDER BY stuf"
rst.Open strSQL, conAcc, adOpenStatic, adLockReadOnly
Workbooks.Add
ActiveCell.CopyFromRecordset rst
rst.Close
Set rst = Nothing
conDM.Close
Set conDM = Nothing
End Sub
Private Sub OpenAccConn()
'Set up Object, open DB connection
Set conAcc = CreateObject("ADODB.Connection")
conAcc.Open AccDBConnStr
End Sub
[/vba]
If you need to open an Access DB that has user security, you'll have to change your connection string to this:
[vba]'DataBase SQl tester
Option Explicit 'Delete this line if you get "Undeclared Variable" errors
Option Private Module
'Database contstants
Private Const DB_Path As String = "\\10.2.56.24\shared\"
Private Const DB_Name As String = "DB.mdb"
Private Const MDW_Name As String = "DB.mdw"
Private Const DB_LoginID As String = "Login name"
Private Const DB_Password As String = "Password"
Private Const AccDBConnStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "User ID=" & DB_LoginID & ";Password=" & DB_Password _
& ";Data Source=" & DB_Path & DB_Name & ";" _
& "Jet OLEDB:System Database=" & DB_Path & MDW_Name _
& ";Persist Security Info=True"
Private strSQL As String
Public conAcc As Object[/vba]
Hope that helps!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules