Error 3709 connecting via ADO to Mysql from Excel

griselda

New Member
Joined
Feb 13, 2009
Messages
21
Hi there

I have an excel sheet which queries a MySQL database.
It all runs well (if connected first, which it does when the Workbook is first opened)
However I am now trying to detect when there is no connection (for whatever reason) so it reconnects again before running any queries.

The problem is that (if not connected), the VBA code doesn't go to the error_handling line

I would asume the On Error GoTo error_handling line would make the code jump to error_handling line when an error happens.
But it doesn't. The program just breaks on the next line:
.Open sql_query
and a big message comes up saying err 3709 bla bla bla


Here is some of the code:

Public oConn As ADODB.Connection

' Create a recordset object.
Dim rsPubs As ADODB.Recordset
Set rsPubs = New ADODB.Recordset
Dim rangearray As Variant
Set rangearray = Nothing

With rsPubs
' Assign the Connection object.
.ActiveConnection = oConn
sql_query = "SELECT DISTINCT etc etc etc";

On Error GoTo error_handling ' <<<<<<<<<< DOESN'T WORK ???

.Open sql_query
' Copy the records into cell A5
Range("A5").CopyFromRecordset rsPubs
lastrow = Range("B65536").End(xlUp).Row
rangearray = Range("A1:Z" & lastrow)
Range("M2") = rangearray(lastrow, 1)
.Close
End With

EXIT SUB

error_handling:
if err.number=3709 then
connect_to_MySQL ' << THIS ROUTINE CONNECTS TO MYSQL DB
.Open sql_query
resume next
else
end if



I hope someone can help
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

Forum statistics

Threads
1,214,925
Messages
6,122,301
Members
449,078
Latest member
nonnakkong

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top