Free Newsletters:
DatabaseJournal  
DBANews
Database Journal
Search Database Journal:
 
HOME News MS SQL Oracle DB2 Access MySQL PostgreSQL PHP SQL Etc Scripts Links Discussion
internet.com

» HOME
» NEWS
» FEATURES
» SERIES
MS SQL
Oracle
MS Access
MySQL
DB2
» RESOURCES
Products
Scripts
Links
» DISCUSSION
» TECH JOBS

Marketplace Partners
Be a Marketplace Partner




internet.commerce
Be a Commerce Partner
Corporate Awards
Disney World Tickets
Data Center Solutions
Server Racks
Web Hosting Directory
Dental Insurance
Auto Insurance Quote
Condos For Sale
Desktop Computers
Shop
Remote Online Backup
Home Improvement
Online Universities
Shop Online




MySpace Joins eBay, Yahoo in Open Profile Push

News Corp. Unit Under Fire for Ties to Hacker

Are Non-PC Devices Hurting 'Net Innovation?

internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Linked Data Planet Conference & Expo

CA ERwin® Data Modeler Proven database design and modeling. Efficiently analyze, design and deploy effective database solutions. Whitepaper: Manage SQL Server Deployments
Try it free: CA ERwin® Data Modeler


Solaris 8 Migration Assistant
Rapidly move your Solaris 8 application environments to new systems running Solaris 10 with the Solaris 8 Migration Assistant. Reduce migration risk while taking advantage of increased performance, reliability and security of the latest SPARC hardware platforms and Solaris 10 OS. »

 
Sun Eco Innovation: Good for Business, Good for the Environment
A complete solution to help you optimize and refresh your datacenter while properly recycling equipment and eliminating eWaste, including money-saving promotions to lower hardware acquisition costs. »

 
Sun Eco Innovation: Power Calculators
Power consumption has increasingly become a priority in customer's minds when purchasing new systems or storage. Sun's Power Calculators provide data on power consumption of Sun products allowing IT managers to better plan the power requirements in the datacenter to achieve better energy and cost savings. »

 
Optimize the Web Tier: Consolidate to Get More Performance in Less Space and Lower Power Consumption
Expansion in the Web tier is generally accomplished by adding more servers whenever extra capacity is needed. As the pool of servers grows larger, however, the complexity of the environment can grow exponentially. »
Related Articles
Automatic Table Relinking

Production Manager (hands on)
Aquent
US-MA-Cambridge

Justtechjobs.com Post A Job | Post A Resume
MS Access
March 17, 2006
Manual Table Relinking
By Danny Lesandrini

Last month's article, Automatic Table relinking, demonstrated how to implement table linking behind the scenes, with no user interaction required.  However, there are times when we have to trust the users with the awesome responsibility of pointing to the correct database.  Perhaps it is only at the initial setup of the client application, or perhaps your program allows users to connect to different databases.  Whatever the reason, at times you need to allow for manual table relinking.

Once again, I am compelled to point out that I have seen many and various solutions to this issue, and any number of them may be used with success.  The point of this article is not to show the best way to provide a table linking paradigm but rather to consolidate some of the tools and tricks that I have used over the years to serve as a starting point for your solution. 

The download for this article includes a module named API_Functions , which includes published code from some of the all-time great Access programmers, like Dev Ashish and Terry Kreft.  I have been using this code for years (nearly a decade, now that I think about it) and these code snippets always include an attribution to give credit where credit is due.  The rest of the demo, its forms and module code, is my own.  Feel free to use it in part or in whole, but please retain references to the generous programmers who contributed these valuable VBA functions.

The objective for this article is to demonstrate the following:

1.  Relink ODBC tables to Pubs database on a SQL Server 

2.  Demonstrate how to display a Progress meter with cancel button  

3.  Show the same process for dynamic linking to an MDB file

4.  How to open the Linked Table Manager wizard. 

To give you an idea of what this demo does, consider the screen shot below.  The main form reads the list of linked objects from the MsysObjects table and gives you three options for relinking:  link only the ODBC to SQL Server tables, Link only the MDB tables or open the Linked Table Manager wizard. The first two options prompt you for a source (DSN for the ODBC request and File Path for the MDB option).  Once selected, the relinking process begins and the progress meter displays the process metadata.  Clicking the Cancel button aborts the process.  Cool, huh?  Now let's see how it is done.

Attach ODBC Method

The process for relinking to an ODBC data source, such as SQL Server Pubs database, requires the following steps:

  • Get the connection string
  • Loop through the correct set of tables
  • Set the tabledef's connect property
  • Refresh the link

Now, the most difficult part of this is finding the ODBC connect string. One could hard code a list of Servers and Databases and allow the user to select from the list, but a more flexible solution is to allow the user to link by means of new or preexisting ODBC DSNs.  However, how does one open the ODBC Data Source Administrator dialog box?  Well, here's a little trick for that:

   ' This is a relatively simple way to capture a connect string.
   ' Create a database objects and open an ODBC connection.
   ' Then simply read the connect string and discard the object.
   Set wks = DBEngine.Workspaces(0)
   Set dbs = wks.OpenDatabase("", False, False, "ODBC;")
   m_strConnect = dbs.Connect
   GetDSN = True
   
   ' The results of this call is to set the module level variable, m_strConnect,
   ' to the selected connect string.  It will look something like this ...
   ODBC;DSN= My_Pubs_dsn;Trusted_Connection=Yes;DATABASE=pubs

Now, looping through tables to reset their connect string and refresh the link is a trivial matter. This is the same code we used in the last article, with the one distinction that the connect property is now being set to the user-selected value that was stored in the global variable named  m_strConnect.

   Dim dbs As DAO.Database
   Dim tdf As DAO.TableDef
   Set dbs = CurrentDb()
   ' Loop through TableDefs collection, only processing
   ' the table if it already has a Connection property.
   For Each tdf In dbs.TableDefs
      If tdf.Connect <> "" Then
         tdf.Connect = m_strConnect
         tdf.RefreshLink
      End If
   Next

That's all there is to it. We have exposed the ODBC Data Source Administrator dialog box and allowed the user to select a database. This is great for SQL Server tables, but it will not work if you need to connect to another Access database. Let's see how that might be accomplished.

Attach MDB Method

Warning: The following code works ONLY if you import the API_Functions module into your MDB file.

It is beyond the scope of this article to explain the inner-workings of the find-file common dialog box, but I will demonstrate how to use the methods that are included in the API_Functions module included in the download. You will have to play around with it a little, but if you haven't already discovered this little gem, you may find this to be the most valuable piece of code in the download.

  Dim strFileMDB As String
   Dim myOpenFile As gtypCw_OPENFILE
   Dim strExt As String
   Dim strFilter As String
   
   ' Specify the default extension
   With myOpenFile
      .strDefaultExtension = ".mdb"
      .strDialogTitle = "Locate MDB data File"
      .strFilter = CreateFilterString("PubsData.mdb", "*.mdb")
      .strInitialDir = CurrentProject.Path
      .strInitialFile = "PubsData.mdb"
      strFileMDB = GetOpenFileEX(myOpenFile)
      strFileMDB = .strFullPathReturned
   End With
   
   FindMDBFile = myOpenFile.strFullPathReturned

The above snippet is taken from a public function named   FindMDBFile()  which returns the path to the data file selected by the user. Once you have that, you are ready to relink to the MDB tables. The code for refreshing links is almost identical to that above, with one exception: You must prefix the file path with the text  ;DATABASE=   to indicate that you're linking to an Access MDB database.

   strDataFile = FindMDBFile()
   Set dbs = CurrentDb
   ' CONNECT STRING FOR MDB MUST BE IN THIS FORMAT:
   '   ;DATABASE=C:\Development\Crestone\DataCTG.mdb
   strConnect = ";DATABASE=" & strDataFile
   
   For Each tdf In dbs.TableDefs
      If tdf.Connect <> "" Then
         tdf.Connect = strConnect
         tdf.RefreshLink
      End If
   Next

To simplify the article I have posted only the code that does the actual work. Of course, you will need to declare all your variables and you will want to add error handling, especially in the case of table linking. If and when it fails, you will need to know because it is possible that such an error could preclude opening the application at all. Even in the best scenario, where a single table fails to relink, it is important for debugging to know and log such errors.

Display the Progress

The sample code includes a form named frmProgress, shown in the image below.  This form is manipulated by a public sub named UpdateProgressMeter, which updates the display by extending the length of a blue label to give a graphic representation of how far along the process is.  The lower label also accepts a new caption and the form is repainted.

   Public Sub UpdateProgressMeter(ByVal strMsg As String, _
                                  ByVal lngCurr As Long, _
                                  ByVal lngTotal As Long)
      On Error Resume Next
      Dim intPercent As Integer
      intPercent = (lngCurr / lngTotal) * 100
    
      With Forms!frmProgress
         !lblBlue.Caption = "Processing  " & intPercent & "%  Completed"
         !lblTransparent.Caption = "Processing  " & intPercent & "%  Completed"
         !lblBlue.Width = intPercent / 100 * !lblTransparent.Width
         !lblTable.Caption = strMsg
         .Repaint
      End With
      DoEvents
   End Sub

The process for implementing this progress meter is simple and the following steps are illustrated in the code snippet below:

  • Open form and initialize values
  • Call the Update method with each table processed
  • Watch for the Cancel flag and respond accordingly
  • Close the form when finished
   ' Count the total number of ODBC linked tables (Type=4)
   ' This will be used to determine how far along we are
   ' with the linking of each new table.
   lngTotal = DCount("*", "MsysObjects", "[Type]=4")
   ' Open the progress form and set its properties
   DoCmd.OpenForm "frmProgress"
   strMsg = "Ready to link to ODBC tables"
   UpdateProgressMeter strMsg, 0, lngTotal
   
   Set dbs = CurrentDb
   For Each tdf In dbs.TableDefs
      If tdf.Connect <> "" Then
         ' Update progress meter with info about current table
         lngCurr = lngCurr + 1
         strMsg = "Linking table " & tdf.Name
         UpdateProgressMeter strMsg, lngCurr, lngTotal
         ForcePause 1
         
         tdf.Connect = m_strConnect: tdf.RefreshLink
         
         ' If user pressed the Cancel button, then the global
         ' variable will be true.  In that case, exit the loop.
         If m_fCancel = True Then Exit For
      End If
   Next
   ' Clean up by closing the form.
   DoCmd.Close acForm, "frmProgress"

Not Exactly A Cut-And-Paste Solution

As with most of my articles, the code is supplied as a framework, not a cut-and-paste solution.  Your table linking schema is probably going to be more complicated than what is shown above.  For that matter, the sample code in the download file is more complicated than what is shown above, since I have to manage two sets of tables:  ODBC and MDB linked.  The test for tdf.Connect <> "" isn't sufficient to distinguish which are linked to SQL Server and which to Access.  However, these matters are usually trivial and business rules easy to implement, once you have the basic idea.

I hope you found this discussion useful.  Download the code and play with it.  The Progress Meter and the Find File code are worth it, even if you aren't currently looking for a table linking solution.  As usual, I welcome your feedback, comments and questions.

» See All Articles by Columnist Danny J. Lesandrini

Tools:
Add databasejournal.com to your favorites
Add databasejournal.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed

MS Access Archives

Webcast: Five Virtualization Trends to Watch. Produced for HP, Citrix, and Intel.
What's The Future Of IT? Find Out By Reading "IT in 2018" Now. Free Registration Required.
Learn Tools & Techniques to Justify and Fund Your IT Investments. Download Complimentary Report Now!
HP eBook: Using Business Service Management (BSM) to Manage Your Business Applications
Learn about expanding business opportunities for the reseller channel. Visit IT Channel Planet.


Latest Forum Threads
MS Access Forum
Topic By Replies Updated
Export a report into excel Irina_5220 4 May 9th, 01:50 PM
Table Property Question barlowr70 0 May 6th, 10:51 AM
Compile MS Access Database samson 1 May 1st, 03:28 AM
How to connect MS-Access with c++ rockys111 0 April 30th, 01:36 AM







JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES