Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Intermediate
Nov 5, 1999



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

Share Variables Between Multiple Apps

Sometimes it's necessary to share variables between multiple instances of an application. For example, you might want to share a database connection. Here's how: Create an ActiveX EXE with an exposed MultiUse class named Class1. An ActiveX DLL won't work because it's running in-process within each client. Include a module in the project and declare the required variable you want to share in this module as a public variable.

The code in an ActiveX DLL looks like this:

'**** Module code *****
Global gCon As Database
Global glngNrOfObjects As Long

'**** Code in Class ****
Public Property Set DBConn(Con As Database)
	Set gCon = Con
End Property
Public Property Get DBConn() As Database
	Set DBConn = gCon
End Property

Private Sub Class_Initialize()
	' this variable keeps count of objects created
	glngNrOfObjects = glngNrOfObjects + 1
End Sub
Private Sub Class_Terminate()
	' decrease object count
	glngNrOfObjects = glngNrOfObjects - 1

	' if no objects exist, close the connection
	If glngNrOfObjects = 0 Then
		If Not gCon Is Nothing Then
			Set gCon = Nothing
		End If
	End If
End Sub
Now reference this ActiveX EXE in your project and use it. Here's how I did it:
'** Code in application using the ActiveX EXE **

Dim clsDBConn As Class1

Private Sub Form_Load()
	Set clsDBConn = New Class1
	' set the connection, when first object is 
	' created
	If clsDBConn.DBConn Is Nothing Then
		Set clsDBConn.DBConn = _
			Workspaces(0).OpenDatabase( _
		End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
	Set clsDBConn = Nothing
End Sub
You don't need to include the code to count the object instances and close the database if your variable is a simple one-that is, if it's not an object reference. Also, in that case, change the Property Set to Property Let.

Both the projects have a reference to DAO because they're using the database object.

Ravindra Okade
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