Share Variables Between Multiple Apps

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 DatabaseGlobal glngNrOfObjects As Long'**** Code in Class ****Public Property Set DBConn(Con As Database)	Set gCon = ConEnd PropertyPublic Property Get DBConn() As Database	Set DBConn = gConEnd PropertyPrivate Sub Class_Initialize()	' this variable keeps count of objects created	glngNrOfObjects = glngNrOfObjects + 1End SubPrivate 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			gCon.Close			Set gCon = Nothing		End If	End IfEnd 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 Class1Private 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( _			"	empdb1.mdb")		End IfEnd SubPrivate Sub Form_Unload(Cancel As Integer)	Set clsDBConn = NothingEnd 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.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several