I am trying to open a database in excel vba. I started a macro and chose Get External Data from Microsoft Query. That populated a column with data but what I want is a database object I can use to create recordsets such as
db.OpenRecordset(sMySqlString, dbOpenDynaset). The macro created this code:
Sub Macro8()
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array("ODBC;DSN=Global_PLA;ServerName=someserver.1583;ServerDSN=GLOBALPLA;ArrayFetchOn=1;ArrayBufferSize=8;TransportHint=TCP;ClientVersion=10.00.1" _
), Array("51.000;CodePageConvert=1252;AutoDoubleQuote=0;")), Destination:= _
Range("$A$1")).QueryTable
.CommandText = Array( _
"SELECT ITEM_MASTER.PART FROM ITEM_MASTER ITEM_MASTER WHERE (ITEM_MASTER.PART Like 'GN%') ORDER BY ITEM_MASTER.PART")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
End Sub
I cleaned that up to this:
Dim db As ADODB.Connection
Dim rs As Recordsets
sConn = "ODBC;DSN=Global_PLA;"
sConn = sConn & "ServerName=someserver.1583;"
sConn = sConn & "ServerDSN=GLOBALPLA;"
sConn = sConn & "ArrayFetchOn=1;"
sConn = sConn & "ArrayBufferSize=8;"
sConn = sConn & "TransportHint=TCP"
sConn = sConn & "ClientVersion=10.00.1"
sSql = "SELECT ITEM_MASTER.PART FROM ITEM_MASTER "
sSql = sSql & "WHERE (ITEM_MASTER.PART LIKE 'GN%' "
sSql = sSql & "ORDER BY ITEM_MASTER.PART"
set db = New ADODB.Connection
db.Open sConn '<-----THIS ERRORS as does db.ConnectionString = sConn : db.Open
set rs = db.OpenRecordset(sSql, dbOpenSnapshot)
MsgBox rs.RecordCount
This obviously is not correct as I get "[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified". Can someone show me what needs to be changed?
Thanks!
Robb
db.OpenRecordset(sMySqlString, dbOpenDynaset). The macro created this code:
Sub Macro8()
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array("ODBC;DSN=Global_PLA;ServerName=someserver.1583;ServerDSN=GLOBALPLA;ArrayFetchOn=1;ArrayBufferSize=8;TransportHint=TCP;ClientVersion=10.00.1" _
), Array("51.000;CodePageConvert=1252;AutoDoubleQuote=0;")), Destination:= _
Range("$A$1")).QueryTable
.CommandText = Array( _
"SELECT ITEM_MASTER.PART FROM ITEM_MASTER ITEM_MASTER WHERE (ITEM_MASTER.PART Like 'GN%') ORDER BY ITEM_MASTER.PART")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
End Sub
I cleaned that up to this:
Dim db As ADODB.Connection
Dim rs As Recordsets
sConn = "ODBC;DSN=Global_PLA;"
sConn = sConn & "ServerName=someserver.1583;"
sConn = sConn & "ServerDSN=GLOBALPLA;"
sConn = sConn & "ArrayFetchOn=1;"
sConn = sConn & "ArrayBufferSize=8;"
sConn = sConn & "TransportHint=TCP"
sConn = sConn & "ClientVersion=10.00.1"
sSql = "SELECT ITEM_MASTER.PART FROM ITEM_MASTER "
sSql = sSql & "WHERE (ITEM_MASTER.PART LIKE 'GN%' "
sSql = sSql & "ORDER BY ITEM_MASTER.PART"
set db = New ADODB.Connection
db.Open sConn '<-----THIS ERRORS as does db.ConnectionString = sConn : db.Open
set rs = db.OpenRecordset(sSql, dbOpenSnapshot)
MsgBox rs.RecordCount
This obviously is not correct as I get "[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified". Can someone show me what needs to be changed?
Thanks!
Robb
Last edited: