?
Solved

Setting the value of a constant depending on environment

Posted on 2006-11-14
7
Medium Priority
?
152 Views
Last Modified: 2010-04-30
I have a VB application. I have both a production and test database set up.
In a BASIC module, I have four constants that define the parameters for a database connection (DATASOURCE, INITIAL_CATALOG, UID, PWD)

I want the values of these to be set to different values if I am running within the VB IDE than when I compile the application. Is there some way I can accomplish this?

Doug
0
Comment
Question by:dbbishop
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 17944880
Use this function:

Public Function IsDebugMode() As Boolean

' This function returns True if code is running in VB debugger, False otherwise.

   Dim DebugMode As Boolean

   ' Following line is not included in compiled code
   Debug.Assert SetDebugModeTrue(DebugMode)
   IsDebugMode = DebugMode

End Function

Kevin
0
 
LVL 81

Assisted Solution

by:zorvek (Kevin Jones)
zorvek (Kevin Jones) earned 200 total points
ID: 17944886
Here is the other function:

Private Function SetDebugModeTrue( _
      ByRef DebugMode As Boolean _
   ) As Boolean

   ' Return True for function result to prevent assert from halting execution
   DebugMode = True
   SetDebugModeTrue = True

End Function

Kevin
0
 
LVL 29

Expert Comment

by:Nightman
ID: 17944890
You could use a compiler directive. Only risk here is that you forget to change it before the production compile. Not that I've ever done that before ;)

Otherwise you could make them variables, and set their values like this:

if InIDE then
  set variables to x
else
  set variables to y
endif


Use this function for InIDE:

Public Function InIDE(Optional SetMode As Boolean = False) As Boolean
'---------------------------------------------------------------
'Call this with no arguments so the optional argument is false.
'this will cause a recursive call via Debug.Assert that will set
'the static return value True.  If not running in the IDE then
'Debug.Assert will not execute and the static return value will
'remain false.
'---------------------------------------------------------------
  Static bInIDE As Boolean
 
  bInIDE = SetMode
  If Not bInIDE Then Debug.Assert InIDE(True)
  InIDE = bInIDE
End Function
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 35

Accepted Solution

by:
Raynard7 earned 800 total points
ID: 17944895
Hi,

The fact these are constants means that you would not be able to dynamically change them because of their very nature.

You could have global variables that are accessible from your application - and in your main module change their value dpeending if in the IDE or not.  The problem with this approach is that it means that anything else in your application could change them .

Alternativley - you could call a function when initilising your program to set a global value from true to false depending if in an ide or not - then whenever you want your value create some functions to return the value and if the global value (in ide) is true then return one value otherwise return the other.

If you want to see if you are in ide or not you can use the function located here: http://vbnet.mvps.org/index.html?code/helpers/isinide.htm
0
 
LVL 29

Expert Comment

by:Nightman
ID: 17947817
Hmm - dbbishop, I am glad that you have a working solution, but I am also wondering why you only split the points between Raynard7 and zorvek.
0
 
LVL 15

Author Comment

by:dbbishop
ID: 17949457
Nightman: Sorry. I gave points to zorvek because he had the first answer. I liked (and used) the example in the link Raynard7 provided, and also that it provided a way to accomplish the same thing using an API call instead of debug.assert. Functionally, there was not a whole lot of difference between what zorvek provided and what you suggested. You can give ten programmers the same problem and get ten solutions, none of them necessarily wrong. You can tear each on apart and show how solution six would take 3.5 nanoseconds longer to run through 2,000 iterations then solution eight, but if you are only running it once through a user interface, who really cares?

I'll make it up with my renaming a SQL Server database file question :-)

Doug
0
 
LVL 29

Expert Comment

by:Nightman
ID: 17949930
No problem Doug - just interested to know why.

Enjoy your vacation and we'll see you near the end of the month.

Cheers
Night
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month10 days, 22 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question