Losing ADODB.Connection

mikeymay

Well-known Member
Joined
Jan 17, 2006
Messages
1,645
Office Version
  1. 365
Platform
  1. Windows
I am using the following to create a connection to an Access Db file via the Workbook_Open procedure in ThisWorkbook
Code:
Set cnConnection = CreateObject("ADODB.Connection")

strDbName = ThisWorkbook.Path & "\" & strDbFile

cnConnection.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strDbName
In a module I have
Code:
Public cnConnection As ADODB.Connection
to set the connection variable to span all modules, userforms, etc

I am then adding new records to the Db which executes as expected the majority of the time but sometimes the connection is lost and I'm wondering if there is a default timeout period for the connection?

Sometimes I can add a new record, but I also have an amend process that finds a record, populates a userform with the record details and then I want that already open record to be updated but at the point of amending the record
Code:
rsDriver.Fields("Surname") = strSurname
rsDriver.Fields("First_Name") = strFirstName
rsDriver.Fields("Location") = strLocation
etc
etc
The connection has been lost which I am assuming is because of a timeout and as I'm stepping through the code for development it's taking a while to get to the point of amending the record.


TIA
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Do any errors ever occur in the rest of the code?

Perhaps errors 'hidden' by On Error Resume Next, or something similar?
 
Upvote 0
It only seems to happen after a period fo time has passed, as if I run an Add and then and Amend quickly there is no issue.

I have set a timeout to a persistent connection with
Code:
cnConnection.CommandTimeout = 0
which seems to be working.

If the problem continues I will just have to make the connection again and where a query is open, re-open to enable amending. Not ideal and going round the houses a bit but should work.


Thanks
 
Last edited:
Upvote 0
So I have just checked the timeout for the connection and it's still at '15'.

Prior to adding the timeout code it was 15.......but the connection seems to be persistant now...
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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