2017-02-10 8 views
0

私はすでにいくつかのチュートリアルを終えていますが、接続が失敗し続けています。私はさまざまな接続方法を試みました。EXCELからSQLデータベースに接続

私はmySQLワークベンチを通じてmySQLに接続しています。私は、IPアドレスとポート番号を使用してログインしています。これはうまく動作し、私は必要なクエリを実行することができます。

私は今Excelから、好ましくはVBAを通じてこのデータベースにアクセスしようとしています。私は新しい接続を作成しようとしましたが、私は何も動作していないようです。私はstrConn文字列に何を入れるべきか分かりません。

私は現在、使用しています:あなたの助けを

Options Explicit 
Private Sub CommandButton2_Click() 
    Dim cn As ADODB.Connection 
    Dim rs As ADODB.Recordset 
    Dim strConn As String 

    Set cn = New ADODB.Connection 
    strConn = "DRIVER={MySQL ODBC 5.3.7 Driver};" & _ 
       "SERVER=XXX.XXX.X.X;" & _ 
       "PORT=3306" & _ 
       "DATABASE=cahier_de_lab;" & _ 
       "UID=xxx;" & _ 
       "PWD=xxx;" & _ 
       "Option=3" 

    cn.Open strConn 

    ' Find out if the attempt to connect worked. 
    If cn.State = adStateOpen Then 
      MsgBox "Welcome to Pubs!" 
    Else 
      MsgBox "Sorry. No Pubs today." 
    End If 

    ' Close the connection. 
    cn.Close 

End Sub 

感謝を!

+1

ドライバは私に間違って見えます。 –

+0

これは... https://www.google.com.au/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=vba+mysql+driver+connection+string –

+0

odbc接続も設定してください。 –

答えて

0

ExcelからSQL Serverにエクスポートします。

Sub InsertInto() 

'Declare some variables 
Dim cnn As adodb.Connection 
Dim cmd As adodb.Command 
Dim strSQL As String 

'Create a new Connection object 
Set cnn = New adodb.Connection 

'Set the connection string 
cnn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=Northwind;Data Source=Excel-PC\SQLEXPRESS" 
'cnn.ConnectionString = "DRIVER=SQL Server;SERVER=Excel-PC\SQLEXPRESS;DATABASE=Northwind;Trusted_Connection=Yes" 


'Create a new Command object 
Set cmd = New adodb.Command 

'Open the Connection to the database 
cnn.Open 

'Associate the command with the connection 
cmd.ActiveConnection = cnn 

'Tell the Command we are giving it a bit of SQL to run, not a stored procedure 
cmd.CommandType = adCmdText 

'Create the SQL 
strSQL = "UPDATE TBL SET JOIN_DT = '2013-01-22' WHERE EMPID = 2" 

'Pass the SQL to the Command object 
cmd.CommandText = strSQL 


'Execute the bit of SQL to update the database 
cmd.Execute 

'Close the connection again 
cnn.Close 

'Remove the objects 
Set cmd = Nothing 
Set cnn = Nothing 

End Sub 

OR。 。 。 。

Import from SQL Server into Excel . . . . . 


Sub Create_Connectionstring() 

Dim objDL As MSDASC.DataLinks 
Dim cnt As ADODB.Connection 
Dim stConnect As String 'Instantiate the objects. 

Set objDL = New MSDASC.DataLinks 
Set cnt = New ADODB.Connection 

On Error GoTo Error_Handling 'Show the Data-link wizard 
stConnect = objDL.PromptNew 'Test the connection. 
cnt.Open stConnect 'Print the string to the VBE Immediate Window. 
Debug.Print stConnect 'Release the objects from memory. 
exitHere: 
    cnt.Close 
    Set cnt = Nothing 
    Set objDL = Nothing 
Exit Sub 

Error_Handling: 'If the user cancel the operation. 
If Err.Number = 91 Then 
    Resume exitHere 
End If 
End Sub 
+0

元のポスターはMySQLを指定しました。 –

+0

はい、実際に接続しようとしているMySQLデータベースです。 – JahKnows

関連する問題