+ Reply to Thread
Results 1 to 1 of 1

Elegantly connecting to DB with ADO, and executing commands

  1. #1
    Registered User
    Join Date
    01-22-2013
    Location
    Lowestoft, England
    MS-Off Ver
    2016
    Posts
    17

    Elegantly connecting to DB with ADO, and executing commands

    So when I started making applications that needed to connect to Access, I would search forums and would end up using something like what's in this;

    Please Login or Register  to view this content.
    And this would need to be written out every time you want to connect. This is fine when you're starting out, but as the number of connections grows, and if you move databases this will soon become frustrating, as every connection will need to be changed.

    I began to wonder how I could make this easier for myself and have come up with the following.

    In the VB project I will start by adding a reference to the latest 'Microsoft ActiveX Data Objects' library (probably 6.1 for most users), and creating a module just for declaring things which has the following;

    Please Login or Register  to view this content.
    Every database that you want to connect to should have it's own public constant. There are other properties that you can include in the connection string, but the above should work just fine (Only tested for Access, but tthe principle should work for any DB that supports ADO as long as a valid connection string is used).

    In a separate module, we set up the sub to open the connection to the database.

    Please Login or Register  to view this content.


    This now means that when we want to execute a command against our DB it is now very simple. All we need to do is to define our sql command to something like;

    Please Login or Register  to view this content.
    There are of course extra things you can add in like error trapping if you fail to connect etc, but hopefully this can help if you're starting to use DB connections with Access.
    Last edited by stimulus; 11-15-2015 at 06:23 AM.

+ 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] Executing Command Prompt commands through VBA
    By ndw123 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-25-2015, 12:17 PM
  2. [SOLVED] ActiveSheet_Change is not automatically executing commands
    By tnuis in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 08-28-2013, 02:06 PM
  3. This works but not elegantly...
    By fjdurbin in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-04-2011, 03:01 PM
  4. Excel 2007 : Difficulties executing excel commands
    By Thembinkosi in forum Excel General
    Replies: 2
    Last Post: 01-18-2011, 03:49 AM
  5. executing two different commands with same button
    By robbi100 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-18-2008, 07:06 AM
  6. [SOLVED] Executing Excel menu commands from VB
    By Ed White in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-15-2006, 02:25 AM
  7. combinations of checkboxes and executing commands
    By dreamz in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-28-2005, 10:34 AM

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