DB Access

J

John

Hi,
is it possible to access an mysql database from mac axcel.
I need a direct access to the tables in the vba code.

Thanks John
 
J

Jonathan Monroe

John said:
is it possible to access an mysql database from mac axcel.
I need a direct access to the tables in the vba code.

It is possible to connect to a MySQL database from Excel using ODBC.
You can either use the Data->Get External Data->New Database Query
menu, or use the QueryTable VBA class.

Here is an example of executing SQL against an ODBC database using VBA:

Sub ExecuteQuery ()
Dim sql
sql = "SELECT my_column FROM my_table WHERE id = the_id"

Dim connString
connString = "ODBC;DSN=mysqlDSN;UID=myUserID;PWD=myPassword"

Dim thisQT As QueryTable
Set thisQT = ActiveSheet.QueryTables.Add(Connection:=connString,
Destination:=Range("a1"))

thisQT.BackgroundQuery = False
thisQT.sql = sql

On Error GoTo XERR
thisQT.Refresh

Exit Sub
XERR:
Dim errErrs As ODBCErrors
Dim errErr As ODBCError
Set errErrs = Application.ODBCErrors
If errErrs.Count > 0 Then
For Each errErr In errErrs
strMsg = strMsg & "#" & errErr.SqlState & " = " & errErr.ErrorString
Next errErr
MsgBox strMsg, vbCritical, "ODBC ERROR"
End If
If Err.Number <> 0 Then
MsgBox Err.Number & vbCr & Err.Source & vbCr & vbCr &
Err.Description, vbCritical
Err.Clear
End If
End Sub


Instead of a SELECT, you could also execute an UPDATE or INSERT
statement to update your MySQL database.

Jonathan Monroe
Actual Technologies - ODBC for Mac OS X
http://www.actualtechnologies.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top