vba db connection strings
The UK's Number 1 for Microsoft Office Training Add this page to your favourites/bookmarksBookmark page
 
View printable version of pagePrintable version
Plus One Google
Customer: Sign in
Delegate: Sign in
Trainer: Log in

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

VBA DB connection strings - closing connections

resolvedResolved · Medium Priority · Version 2003

replyReply Wed 30 Mar 2011, 14:53Delegate Lyndon said...

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?

For upcoming training course dates see: Pricing & availability

replyReply Mon 11 Apr 2011, 09:52 Edited on Mon 11 Apr 2011, 09:52Trainer Stephen said...

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.

 

Please browse our web site to find out more about
2003 access vba online classes and other Microsoft training courses.

Vba tip:

Good coding practice

It is important that you always comment the VBA code that you write. By doing this it will make the code much easier to read, as both yourself and other people will be able to see what the code is doing or meant to be doing. This is very important if there is more than one person who writes code in the company as you may have different styles of coding and may not really understand what the other person is trying to do. Also if you write some code and then do not touch it for a year or so you may actually forget what it is meant to be doing. so it is very important to include comments throughout your code and try and make sure that they are as useful as possible

View all VBA hints and tips


Microsoft Certified Partner Accredited Training Provider: Institute of IT Training Institute of Leadership and Management - Certified Courses Security Seal verified by visa, mastercard securecard