+ Reply to Thread
Results 1 to 3 of 3

Referencing a public constant using a variable

  1. #1
    Forum Contributor
    Join Date
    07-07-2005
    Location
    England
    MS-Off Ver
    Office 2019 Enterprise
    Posts
    464

    Referencing a public constant using a variable

    Hi all,

    I very rarely use public constants and I'm struggling with this bit: I am trying to start a connection to a database. We have 2 different databases and I am declaring a Public Constant for each data Location. On my spreadsheet I have a cell which the user can enter the database name. So for example, cell A1 would contain 'Location1'

    In the code below you will see that I want to use this cell A1 variable and add the word "Data" on the end to reference the correct Public Const but this way I get an error "Data Source Not Found". I know the data source is correct though as if I don't use the variable and simply put Location1Data in the code then it works. Would someone be able to help please?

    Please Login or Register  to view this content.

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Referencing a public constant using a variable

    You can't build up a variable name like that.

    You've got a few options, one of the easiest might be to use a named range.

    Put the connection strings in cells on a worksheet and give the cells names (Location1Data etc), you could then use something like:

    Please Login or Register  to view this content.

  3. #3
    Forum Contributor
    Join Date
    07-07-2005
    Location
    England
    MS-Off Ver
    Office 2019 Enterprise
    Posts
    464

    Re: Referencing a public constant using a variable

    Hi, sorry I replied to this but then checked back and it obviously didn't work. Thanks for the reply and that works perfect. Thank you

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Public Variable and Static Variable Difference
    By zanshin777 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-18-2016, 01:58 AM
  2. [SOLVED] set public constant to current folder path
    By mcdermott2 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-10-2015, 05:24 PM
  3. Value of Public Constant From Concatenate strings
    By exceere in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-13-2014, 04:45 PM
  4. creating a public constant by concatenating string......gives compile error
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-26-2011, 07:23 AM
  5. Using a cell value as a public constant
    By larawill in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-30-2009, 05:59 AM
  6. Create a public constant that defines a specific RGB color
    By batman in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-18-2007, 11:39 PM
  7. [SOLVED] How do you update a public constant
    By Greg in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-11-2005, 07:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1