Problema con connessione SQL excel
Hai un problema con Excel? 
Problema con connessione SQL excel
di loop (utente non iscritto) data: 08/08/2014 16:11:50
Ciao a tutti, ho un problemino con una cosa abbastanza semplice, ma ho letto di tutto e non riesco a trovare una soluzione al mio problema. Praticamente vorrei collegare il mio foglio excel ad una tabella di sqlserver per effettuare una query e successivamente trattare i dati con la macro stessa, il problema sorge nel collegare il db al foglio excel. Ho registrato la macro importando l'origine dati e funziona, ma se provo a farlo io direttamente scrivendo una macro di connessione il problema persiste, posto sotto il codice. Ho letto un sacco di documentazione ma proprio non riesco a capire dove sta il problema
L'errore restituito è: [Microsoft][Driver Manager ODBC] Nome origine dati non trovato e driver predefinito non specificato
Qualcuno sa e vuole aiutarmi?
Grazie anticipatamente a tutti
public sub importadati()
Dim oCon As ADODB.Connection
Dim oRS As ADODB.Recordset
Set oCon = New ADODB.Connection
oCon.ConnectionString = "OLEDB;Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Password=aaa;Data Source=192.168.30.2;Use Procedure for Prepare=1;" & _
"Packet Size=4096;Use Encryption for Data=False;Tag with column collation when possible=False;Initial Catalog=Databasetest"
oCon.Open
Set oRS = New ADODB.Recordset
oRS.ActiveConnection = oCon
oRS.Source = "Select * From Fogli"
oRS.Open
ThisWorkbook.Sheets("db").Range("A1").CopyFromRecordset oRS
oRS.Close
oCon.Close
If Not oRS Is Nothing Then Set oRS = Nothing
If Not oCon Is Nothing Then Set oCon = Nothing
End Sub |
di Grograman data: 08/08/2014 16:26:04
Ciao!
Io purtroppo non ti posso aiutare, ma direi che dal codice proposto manca un informazione importante... il nome della libreria aggiuntiva da utilizzare!
Microsoft ActiveX Data Objects 6.1 Library (nel mio caso 6.1)
Inoltre per la distruzione degli oggetti direi che è inutile usare il ciclo if-end if, basta distruggerlie basta ;)
Set oRS = Nothing
Set oCon = Nothing |
di loop (utente non iscritto) data: 08/08/2014 16:30:10
Ciao, intanto grazie per la tua risposta. Io ho abilitato Microsoft ActiveX Data Objects 2.0, devo aggiungere una riga per richiamarla? La cosa strana è che facendo la connessione con il registatore di macro funziona e non importa alcuna libreria...
di Grograman (utente non iscritto) data: 08/08/2014 16:41:21
come dicevo non posso aiutarti perchè non ho mai usato la connessione ADODB con excel!
Prova a postare la macro registrata e vediamo se cavo qualche ragno dal buco.
di loop (utente non iscritto) data: 08/08/2014 16:58:08
Ecchila
Sub Macro2()
'
' Macro2 Macro
'
'
Range("A1").Select
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array( _
"OLEDB;Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Data Source=192.168.30.2;Use Procedure for Prepare=1;Auto Translat" _
, _
"e=True;Packet Size=4096;Workstation ID=Portable;Use Encryption for Data=False;Tag with column collation when possible=False;Initial " _
, "Catalog=Databasetest"), Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdTable
.CommandText = Array("""Databasetest"".""dbo"".""Fogli""")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.SourceConnectionFile = _
"C:UsersloopDocumentsOrigini dati utente192.168.30.2 Databasetest Fogli.odc"
.ListObject.DisplayName = _
"Tabella__192.168.30.2_Databasetest_Fogli_1"
.Refresh BackgroundQuery:=False
End With
Range("B10").Select
End Sub
|
di loop (utente non iscritto) data: 11/08/2014 09:22:55
Nessuno sa aiutarmi?
di Grograman data: 11/08/2014 10:01:23
Nutro forti dubbi che possa funzionare, visto che il mio capo ha paura di tutti e quindi guai a chiedere un accesso al server per poter provare una query... quindi sono andato proprio a livello teorico:
Option Explicit
Sub Tentativo()
Dim oLst As ListObject
Dim ws As Worksheet
Dim sList As String, sQuery As String, sSource As String
Set ws = thisworksbook.Worksheets(1)
sList = "OLEDB;Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Password=aaa;Data Source=192.168.30.2;Use Procedure for Prepare=1;" & _
"Packet Size=4096;Use Encryption for Data=False;Tag with column collation when possible=False;Initial Catalog=Databasetest"
sQuery = "Select * From Fogli"
sSource = "C:UsersloopDocumentsOrigini dati utente192.168.30.2 Databasetest Fogli.odc"
Set oLst = ws.ListObjects.Add(SourceType:=0, Source:=sList, Destination:=ws.Range("$A$1")).QueryTable
With oLst
.CommandType = xlCmdTable
.CommandText = sQuery 'Array("""Databasetest"".""dbo"".""Fogli""")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.SourceConnectionFile = sSource
.ListObject.DisplayName = "OTentativo"
.Refresh BackgroundQuery:=False
End With
Set ws = Nothing
Set oLst = Nothing
End Sub |
di Grograman data: 11/08/2014 10:02:17
p.s. probabilmente oLst andrebbe dichiarato come "Querytable"
Vuoi Approfondire?