How to use conditional compiling in VBA

chuckbo

New Member
Joined
Feb 10, 2010
Messages
22
I'm trying to use conditional compiling in a project.

Bottom Line Up Front
Can I / How do I use conditional compiling in the Declarations section?

Details
I have a Windows app that I'm upgrading so that my Mac users can also use it. The trick is that it accesses an SQL Server backend. On Windows, I can use the ADODB library, but I knew that ADO wouldn't work on the Mac. I'm testing an ODBC driver from ActualTech. I'm using conditional compiling to separate the two methods to submit the SQL statement. But I'm seeing a message for this code in my Declarations section when I run this on the Mac.

#If (Win32 Or Win64) Then
Global rst As ADODB.Recordset
Global cnn As ADODB.Connection
#Else
#End If

The error I'm getting is Can't find project or library.
The line flagged as the troublemaker is the Global rst line.

I know that the library isn't on the Mac, which is why I tried to put it into a conditional compiling block. Am I doing something wrong, or does conditional compiling not work in the Declarations section? The reason I want to make it a global variable is I'm concerned the performance will be bad if I had to reinitialize every time I call a routine. And passing it as a parameter to my routines I don't think is an option because I won't be able to pass an object type that the Mac doesn't recognize.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
And here's an additional problem that I thought I'd already worked around -- but it's giving me an error today.
I had been getting an error when I passed a recordset to a routine, even if the Mac code would never call that routine. I thought I'd fixed it by putting that entire routine within a compiler condition.

#If (Win32 Or Win64) Then
'************************************
Sub Add_costs_win(rst As Recordset, RiskID As String, Version_number As Integer)
'************************************



but running the app on the Mac gives me a compile error and flags the sub line.
So for now, I can't set up a recordset as a global variable, and I can't pass it to a routine. What suggestions does anyone have?

Chuck
 
Upvote 0
I usually use this to distinguish between Mac and Windows code:

VBA Code:
#If Mac Then
  ' Mac only code here
#Else
  ' Windows only code here
#End If
 
Upvote 0
Solution
This is perfect.
I discovered that if Win64 still runs the Mac code (because Win64 isn't testing for Windows 64-bit, it's testing for any 64-bit).
And my tests for NOT Mac were failing because the NOT has documented bugs and shouldn't be used.

Chuck
 
Upvote 0
This is perfect.
I discovered that if Win64 still runs the Mac code (because Win64 isn't testing for Windows 64-bit, it's testing for any 64-bit).
And my tests for NOT Mac were failing because the NOT has documented bugs and shouldn't be used.

Chuck
I must have discovered that too, leading to the syntax I now use.
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,226
Members
452,620
Latest member
dsubash

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top