2012-11-26 11 views
10
Dim oConn As ADODB.Connection 
Private Sub ConnectDB() 
Set oConn = New ADODB.Connection 
Dim str As String 
str = "DRIVER={MySQL ODBC 5.2.2 Driver};" & _ 
              "SERVER=sql100.xtreemhost.com;" & _ 
              "PORT=3306" & _ 
              "DATABASE=xth_9595110_MyNotes;" & _ 
              "UID=xth_9595110;" & _ 
              "PWD=myPassword;" & _ 
              "Option=3" 
''' error ''' 
oConn.Open str 
End Sub 

Private Sub InsertData() 
Dim rs As ADODB.Recordset 
Set rs = New ADODB.Recordset 
ConnectDB 
sql = "SELECT * FROM ComputingNotesTable" 
rs.Open sql, oConn, adOpenDynamic, adLockOptimistic 
Do Until rs.EOF 
    Range("A1").Select 
    ActiveCell = rs.Fields("Headings") 
    rs.MoveNext 
Loop 
rs.Close 
oConn.Close 
Set oConn = Nothing 
Set rs = Nothing 
End Sub 

Facendo le cose simili in PHP, ho potuto accedere correttamente al server MySQL. Ho installato il connettore ODBC. Ma nei codici VBA di cui sopra, ho fallito. Si verifica un errore. (Vedi i codici in cui è presente l'errore)In che modo VBA può connettersi al database MySQL in Excel?

$connect = mysql_connect("sql100.xtreemhost.com","xth_9595110","myPassword") or die(mysql_error()); 

mysql_select_db("myTable",$connect); 

risposta

4

Il codice di Ranjit ha causato lo stesso messaggio di errore riportato da Tin , ma ha funzionato dopo aver aggiornato Cn.open con il driver ODBC in esecuzione. Controllare la scheda Driver nell'Amministratore origine dati ODBC. Il mio ha detto "MySQL ODBC 5.3 Unicode Driver" quindi l'ho aggiornato di conseguenza.

+1

Grazie mille davvero per testare i codici postati tanto tempo fa !!! –

8

Questo pezzo di VBA ha lavorato per me:

Sub connect() 
    Dim Password As String 
    Dim SQLStr As String 
    'OMIT Dim Cn statement 
    Dim Server_Name As String 
    Dim User_ID As String 
    Dim Database_Name As String 
    'OMIT Dim rs statement 

    Set rs = CreateObject("ADODB.Recordset") 'EBGen-Daily 
    Server_Name = Range("b2").Value 
    Database_name = Range("b3").Value ' Name of database 
    User_ID = Range("b4").Value 'id user or username 
    Password = Range("b5").Value 'Password 

    SQLStr = "SELECT * FROM ComputingNotesTable" 

    Set Cn = CreateObject("ADODB.Connection") 'NEW STATEMENT 
    Cn.Open "Driver={MySQL ODBC 5.2.2 Driver};Server=" & _ 
      Server_Name & ";Database=" & Database_Name & _ 
      ";Uid=" & User_ID & ";Pwd=" & Password & ";" 

    rs.Open SQLStr, Cn, adOpenStatic 

    Dim myArray() 

    myArray = rs.GetRows() 

    kolumner = UBound(myArray, 1) 
    rader = UBound(myArray, 2) 

    For K = 0 To kolumner ' Using For loop data are displayed 
     Range("a5").Offset(0, K).Value = rs.Fields(K).Name 
     For R = 0 To rader 
      Range("A5").Offset(R + 1, K).Value = myArray(K, R) 
     Next 
    Next 

    rs.Close 
    Set rs = Nothing 
    Cn.Close 
    Set Cn = Nothing 
End Sub 
+0

Grazie per i vostri codici. Ma sono rimasto bloccato in cui non so quale dovrebbe essere il server. Sto usando MySQL di Xtreemhost e sto usando "sql100.xtreemhost.com". Xtreemhost Support afferma che questo è il nome del server. Cosa dovrei fare allora? –

+0

hai provato a inserire il nome del tuo server nella cella b2 del tuo foglio Excel? –

+0

Sì, l'ho fatto, presumibilmente il nome del server è "sql100.xtreemhost.com" ma per il driver, ho scritto i codici in questo modo: "Driver = {Driver MySQL ODBC 5.1.11}", secondo la versione sul mio macchina. Sono stato bloccato in "Cn.Open" e l'errore è lo stesso: "Errore di run-time -2147467259 (80004005) Errore di automazione Errore non specificato" –

1

Abilita Microsoft ActiveX Data Objects 2.8 libreria

Dim oConn As ADODB.Connection 
Private Sub ConnectDB()  
Set oConn = New ADODB.Connection  
oConn.Open "DRIVER={MySQL ODBC 5.1 Driver};" & _   
"SERVER=localhost;" & _   
"DATABASE=yourdatabase;" & _   
"USER=yourdbusername;" & _  
"PASSWORD=yourdbpassword;" & _  
"Option=3" 
End Sub 

Ci resto è qui: http://www.heritage-tech.net/908/inserting-data-into-mysql-from-excel-using-vba/

+1

Hanno abilitato la libreria di oggetti dati. E mi chiedo se SERVER = localhost, come può sapere che il mio server SQL è su xtreemhost SQL? Ancora lo stesso errore: Errore di runtime -2147467259 (80004005) Errore di automazione Errore non specificato " –

+0

Ho provato questo e ho ricevuto l'errore:" Nome origine dati non trovato e nessun driver predefinito specificato " – hammythepig

4

Solo una nota a margine per chiunque si imbatta in questa stessa domanda ... Il mio sistema operativo è a 64 bit, quindi ovviamente ho scaricato il driver MySQL a 64 bit ... tuttavia, le mie applicazioni Office sono a 32 bit ... Una volta scaricata la versione a 32 bit, l'errore è andato via e ho potuto andare avanti.

+1

Quale driver? E da dove lo hai scaricato?Mi dispiace, ma sono nuovo nel cercare di ottenere Excel per parlare con MySQL. Ho la stessa configurazione, con una macchina a 64 bit e Office a 32 bit, quindi ho il sospetto che il mio problema potrebbe essere simile al tuo. – lukehawk

Problemi correlati