I have working Excel VBA code to get data from an MS Access database:
Dim Data_Len As Integer, I As Integer, LastRow As Integer, lastRowAcurite As Integer
Dim objConn As ADODB.Connection, objRS As ADODB.Recordset, strSQL As String
Set objConn = New ADODB.Connection
If Err.Number <> 0 Then MsgBox ("ADODB.Connection error")
objConn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\server\myweb\Furnace.mdb;"
If Err.Number <> 0 Then MsgBox ("Connection string error")
Set objRS = New ADODB.Recordset
If Err.Number <> 0 Then MsgBox ("ADODB.Recordset error")
strSQL = "SELECT * FROM Furnace WHERE (Date_Reading > now()-1.) ORDER BY Date_Reading "
objRS.Open strSQL, objConn, adOpenForwardOnly, adLockReadOnly, adCmdText
If Err.Number <> 0 Then MsgBox ("Furnace open error")
Worksheets("temp2").Range("A1").CopyFromRecordset objRS
objRS.Close
Set objRS = Nothing
objConn.Close
Set objConn = Nothing
I want to convert it to retrieve data directly from a SQL Server database on a Linux computer.
I have defined the Windows ODBC entries, as is verified in the working code below. This code seems to be overly complicated:
Private Sub NewWorkbookWithODBCConnection()
Dim myWorkBook As Workbook
Dim myWorkbookConnection As WorkbookConnection
Dim myWorksheet As Worksheet
Dim myQuerytable As QueryTable
Set myWorkBook = Workbooks.Add
Set myWorkbookConnection = myWorkBook.Connections.Add2( _
Name:="French Furnace", _
Description:="Whatever", _
ConnectionString:="ODBC;DSN=SQL2Pi;", _
CommandText:="")
With myWorkbookConnection.ODBCConnection
.BackgroundQuery = True
.CommandType = xlCmdSql
.Connection = "ODBC;DSN=SQL2Pi;"
.RefreshOnFileOpen = False
.SavePassword = False
.SourceConnectionFile = "D:\MyDocs\My Data Sources\Furnace Temps.odc"
.SourceDataFile = ""
.ServerCredentialsMethod = xlCredentialsMethodIntegrated
.AlwaysUseConnectionFile = False
End With
Set myWorksheet = myWorkBook.Worksheets.Add
Set myQuerytable = myWorksheet.ListObjects.Add( _
SourceType:=0, _
Source:="ODBC;DSN=SQL2Pi;", _
Destination:=Range("$A$1")).QueryTable
With myQuerytable
.CommandText = Array("SELECT * FROM `furnace`.`temps` limit 20")
' & "WHERE (Date_Time> > #08/09/22# ORDER BY Date_Time")
' & "WHERE (Date_Time>{ts '" & firstdate & " 00:00:00'})" & " ORDER BY Date_Time")
' .CommandText = Array("SELECT Date_Time, BattV, ArrayV, OutputA, InputA, ChargerSt, OutputW, VOCV FROM `solar`.`outback` " _
' & " Where Date_Time Between '" & firstdate & " 00:00:00' AND '" & seconddate & " 00:00:00' ORDER BY Date_Time")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.SourceConnectionFile = "D:\MyDocs\My Data Sources\Furnace Temps.odc"
.Refresh BackgroundQuery:=False
End With
Columns("B:B").NumberFormat = "m/d/yy h:mm;@"
I'd like to use the ADODB objects instead of the Workbook objects, but I have not figured out how to implement them. In the example for the Access database, defining the connection and recordset objects was straightforward.
How can I extract data from a Linux SQL Server?
1条答案
按热度按时间gblwokeq1#