Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Beginner
Aug 6, 1997



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Successfully Close ODBC Connections

The Microsoft Access engine will maintain a persistent connection on an ODBC connection in order to be more efficient, even after using a Close method on a database opened with ODBC. The ODBC database process keeps running. To close the connection successfully, you must end the Visual Basic application. Even though this is by design, many times the connection is unwanted. One way to force the connection closed is to set ConnectionTimeout to the minimum setting of one second. A setting of zero indicates to never close the connection. It is defaulted to a value of 600 seconds, or 10 minutes. If a Visual Basic program does not reopen the ODBC connection after doing a Close method, a timeout occurs and the connection closes automatically. You can control the timeout period by placing the following line in your VB.INI or <vb_exe_app_name>.INI file, where x is the number of seconds:
To enforce the fastest possible timeout, you can set ConnectionTimeout to one. In addition, you can add this code after you close the database to make sure the connection is terminated:
 db.Close	' Close database, using 
			' database object variable (db).

Start = Timer
Do			' This loop pauses a second 
			' to allow a time-out
	FreeLocks	' Tell Microsoft Access 
			' engine that program is idle.
	DoEvents	' Tell Windows to do any 
			' pending events.
Loop While Timer <= Start + 1
This loop delays for a second after the db.Close. The FreeLocks statement tells the database engine that the user is idle. If you run the Visual Basic program with ConnectionTimeout set to one in your VB.INI or .INI file, the database engine will disconnect the one-second-old connection to the server.
Douglas Haynes
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date