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:
Share on facebook
Share on twitter
Share on linkedin


The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.