Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

vba db connection strings

Forum home » Delegate support and help forum » Microsoft VBA Training and help » VBA DB connection strings - closing connections

VBA DB connection strings - closing connections

ResolvedVersion 2003

Lyndon has attended:
Excel VBA Advanced course

VBA DB connection strings - closing connections

I have a workbook with a OLEDB connection. A macro reloads the SQL and refreshes the query:

Selection.QueryTable.Sql = NewQuery 'string variable
Selection.QueryTable.Refresh BackgroundQuery:=False

The DB is MSAccess, when I look at the folder contain the DB.mdb I see DB.lbd. However when refreshed this query does not close the connection and the .ldb persists until the workbook is closed, when it closes the .lbd.

I do not create the connection via a macro, as the querytable is already in Excel, so I'm not sure how I should close the connection, to release the DB after refreshing. suggestions?

Edited on Mon 11 Apr 2011, 09:52

RE: VBA DB connection strings - closing connections

Hi Lyndon

Thanks for your question

There are several ways to tackle this. The simplest would be to explicitly close the database. The following code opens and then closes an Access Database. You first need to reference the Microsoft Access object model


Dim objAccess As New Access.Application

objAccess.OpenCurrentDatabase ("C:\Users\Developer\Desktop\Northwind.mdb")


objAccess.CloseCurrentDatabase

Sun 17 Apr 2011: Automatically marked as resolved.

 

Training courses

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.

VBA tip:

Use VbNullString instead of

When needing to default a String variable back to it's default of "" use vbNullString as in the following example:

Sub ClearText()

Dim strEmpName As String

strEmpName = "John Smith"
MsgBox strEmpName

strEmpName = vbNullString
MsgBox strEmpName

End Sub

View all VBA hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.1 secs.