I have created an addin that among other things, has the ability to open *.sql files, read the contents of those files into a string variable; then that variable used in MS Query to populate a spreadsheet. It works swimmingly for any select query I have fed to it but action queries return an error. The question is are action queries off limits to MS Query? I will show first the VB I am using. The typical script is too long to post
so have attached a text file of a typical script for my purposes.
Public Sub ReadAsciiFile()
Dim szFileName As String
Dim iFileNum As Integer
Dim sBuf As String
Dim SQL As String
Dim szCatalog As String
szCatalog = InputBox("SELECT [ ASCEND = 'A' :: PROVAL = 'P' ]", "SELECT DATABASE", "P")
Select Case UCase(szCatalog)
Case "A"
szCatalog = "Ascend"
Case Else
szCatalog = "Proval"
End Select
' open common dialog form and get filename
UserForm1.Show
szFileName = g_ScriptFile
' does the file exist? simpleminded test:
If Len(Dir$(szFileName)) = 0 Or Len(szFileName) = 0 Then
Exit Sub
End If
iFileNum = FreeFile()
Open szFileName For Input As iFileNum
Do While Not EOF(iFileNum)
Line Input #iFileNum, sBuf
' we want to be able to have notes in the script
If InStr(sBuf, "--") = 0 Then
SQL = SQL & sBuf & vbCrLf
End If
Loop
Debug.Print SQL
' close the file
Close iFileNum
If Not IsOpen(SQL, szCatalog, True) Then
Call BadDataError("ReadAsciiFile")
End If
End Sub
' the 'SQL' is then used in the following
Public Function IsOpen(ByVal szSql As String, _
Optional ByVal szDatabase As String = "Proval", _
Optional ByVal fUseHeader As Boolean = False) As Boolean
Dim fOpen As Boolean
Dim szDSN As String
Dim szCatalog As String
Dim szUser As String
Dim szPswd As String
On Error GoTo EH
' read connection parameters from READER sheet range names
fOpen = True ' assume the best
szDSN = GetDSN ' dsn name
Call ClearNames ' clear previous query names
szDatabase = UCase(szDatabase)
Select Case szDatabase
Case "PROVAL"
szCatalog = GetProvalDB
szUser = GetProvalUser
szPswd = GetProvalPass
Case Else
szCatalog = GetAscendDb
szUser = GetAscendUser
szPswd = GetAscendPass
End Select
With ActiveSheet.QueryTables.Add(Connection:= _
"ODBC;DSN=" & szDSN & ";" & "UID=" & szUser & ";" & _
"PWD=" & szPswd & ";DATABASE=" & szCatalog, Destination:=Range(ActiveCell.Address))
.CommandText = szSql
.Name = "QRY_" & Format(Now(), "mmddyy_hhmm") ' create query name
.FieldNames = fUseHeader
.RowNumbers = False
.FillAdjacentFormulas = True
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlOverwriteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
TheExit:
On Error GoTo 0
IsOpen = fOpen
Exit Function
EH:
fOpen = False
Debug.Print Err.Description
GoTo TheExit
End Function
Like I said, this works well for select queries. Typically I am going out to a SQL Server database ( I have all permissions necessary for action queries ).
TIA
Bookmarks