Losing ADODB.Connection

mikeymay

Well-known Member
Joined
Jan 17, 2006
Messages
1,600
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

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
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,215,493
Messages
6,125,134
Members
449,206
Latest member
burgsrus

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