Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Relink tables in database with a database password, using ADO

Posted on 2006-11-13
12
Medium Priority
?
1,163 Views
Last Modified: 2008-01-09
I am converting my DAO code to ADO for a function that relinks tables in a backend database. I have come across one glitch. The backend database has a database password. The code is below. When I try to run this code, it doesn't like the database password line. That is, when Access tries to execute:
     tdf.Properties("Jet OLEDB:Database Password") = strPassword
I get the following error:
     Run-time error '3265';
     Item cannot be found in the collection corresponding to the requested name or ordinal.

The code words fine if I omit the offending line and the database has no password. If I omit the line and the database DOES have a password, I get an "Invalid password" error.

Can I use ADO to relink tables in password-protected databases? If I can, then how do I fix my code?


Function ReLink(strDir As String) As Boolean

' Relink a broken linked Access table.

On Error Resume Next

    Dim cnn As New ADODB.Connection
    Dim cat As ADOX.Catalog
    Dim tdf As ADOX.Table
    Dim intCounter As Integer
    Dim varReturn As Variant
    Dim intNumBadLinks As Integer
    Dim strMissingTables As String
    Dim strPassword as String
   
    'First, make sure all of the linked tables are in the selected database.
    'If one table is missing, then do not try to link the database. If all
    'the linked tables exist, then go ahead and relink them.
   
    Set cnn = CurrentProject.Connection
   
    Set cat = New ADOX.Catalog
    Set cat.ActiveConnection = cnn
   
    With cat
        .ActiveConnection = CurrentProject.Connection
       
        strPassword = InputBox("Enter the password for the items database: " _
            & vbCr & strDir & vbCr _
            & "If there is no password, then just hit Enter.", "Enter Password")
       
        'Create progress meter
        Call SysCmd(acSysCmdInitMeter, "Linking Data Tables", .Tables.Count)
   
        'Loop through each table, attempting to update the link
        intNumBadLinks = 0
        strMissingTables = ""
        For Each tdf In .Tables
            intCounter = intCounter + 1
            Call SysCmd(acSysCmdUpdateMeter, intCounter)
            If .Tables(tdf.Name).Type = "LINK" And _
                Left(tdf.Name, 3) = "tbl" Then
                if strPassword <> "" then
                   tdf.Properties("Jet OLEDB:Database Password") = strPassword 'THIS LINE CAUSES THE ERROR
                end if
                tdf.Properties("Jet OLEDB:Link Datasource") = strDir
            End If
            If Err.Number Then
                If intNumBadLinks = 0 Then
                    MsgBox "The physical table '" & tdf.Name & "' " _
                        & "was not found in database '" & strDir & "'. " _
                        & "You either selected the wrong items database file or " _
                        & "the structure of this file must be updated to be " _
                        & "compatible with this version of gItemBank. I will continue to check for missing tables.",  _
                            vbExclamation, "Incompatible Items Database File"
                End If
                intNumBadLinks = intNumBadLinks + 1
                strMissingTables = strMissingTables & ", " & tdf.Name
                Err.Clear
            End If
        Next tdf
    End With
   
    'Reset the progress meter
    Call SysCmd(acSysCmdRemoveMeter)
   
    If intNumBadLinks > 0 Then
        MsgBox "The following " & intNumBadLinks & " tables were missing from your data file: " _
            & vbCrLf & vbCrLf & strMissingTables
    End If
   
    varReturn = SysCmd(acSysCmdClearStatus)
   
    Set tdf = Nothing
    Set cat = Nothing
    Set cnn = Nothing
   
    ReLink = (intNumBadLinks = 0)
   
End Function
0
Comment
Question by:gordonwwaugh
  • 7
  • 3
12 Comments
 
LVL 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 2000 total points
ID: 17935123
Why are you changing? You can have ADO and DAO live peacefully side-by-side ...

ADOX tables have different properties. Try something like this instead:

if strPassword <> "" then
  tdf.Name = YourTableName
  Set tdf.ParentCatalog = cat
  tdf.Properties("Jet OLEDB:Link Provider String") = "MS Access;DATABASE=C:\SomeFolder\SomeFile.mdb;Pwd=" & strPassword
  tdf.Properties("Jet OLEDB:Remote Table Name") = tdf.Name
  tdf.Properties("Jet OLDEB:Create Link") = True
  cat.Tables.Append tdf <<< you may not need this
End If
0
 
LVL 1

Author Comment

by:gordonwwaugh
ID: 17935311
It doesn't like "Link Provider String". Error message was something like "Invalid ISAM ..."

By the way, I don't need to create links. I just need to reassign them to a different data source.

Perhaps my code is fine, except that I need to replace "Jet OLEDB: Database Password" with the correct phrase. "Jet OLEDB: Link Database Password" does not work either.
0
 
LVL 1

Author Comment

by:gordonwwaugh
ID: 17935329
I found the Microsoft web page that you were probably looking at. The top half shows how to create links. The bottom half shows how to refresh links. I'll give it a try.

http://ask.support.microsoft.com/kb/240222
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 1

Author Comment

by:gordonwwaugh
ID: 17935443
Okay. I get farther. Now it says the password is invalid. But I can open the linked database manually using the same password ("garbage"). Here's the code I'm using now (pretty much straight from the MS web site).

Any thoughts why it thinks my password is wrong?


Private Sub Command2_Click()

Dim adoCn As ADODB.Connection
Dim adoCat As New ADOX.Catalog
Dim adoTbl As New ADOX.Table
Dim strDir As String

'Refresh Link...
Set adoCn = CurrentProject.Connection
Set adoCat = New ADOX.Catalog
Set adoCat.ActiveConnection = adoCn

Set adoTbl.ParentCatalog = adoCat

strDir = "C:\My Backend Database.mdb"

For Each adoTbl In adoCat.Tables
    If adoTbl.Type = "LINK" Then
        adoTbl.Properties("Jet OLEDB:Link Provider String") = "MS Access;Pwd=garbage"
        adoTbl.Properties("Jet OLEDB:Link Datasource") = strDir
    End If
Next

Debug.Print "Link Refreshed..."

End Sub
0
 
LVL 85
ID: 17937225
I would suppose it's because you're setting your adoCn object to the current database's connection object, which is (most likely) NOT C:\My Backend Database.mdb ... try building your adoCn object in this manner, then using that connection as the ActiveConnection, something link this:

Private Sub Command2_Click()

Dim adoCn As ADODB.Connection
Dim adoCat As New ADOX.Catalog
Dim adoTbl As New ADOX.Table
Dim strCn as String
Dim strDir As String

strDir = "C:\My Backend Database.mdb"
Set adoCn = New ADODB.Connection
With adoCn
    .Provider = "Microsoft.JET.OLEDB.4.0;Jet OLEDB:Database Password=garbage"
    .Open strDir
End With

'Refresh Link...
'/Set adoCn = CurrentProject.Connection << removed this line - it was pointing to your current db connection

Set adoCat = New ADOX.Catalog
Set adoCat.ActiveConnection = adoCn

Set adoTbl.ParentCatalog = adoCat

For Each adoTbl In adoCat.Tables
    If adoTbl.Type = "LINK" Then
        adoTbl.Properties("Jet OLEDB:Link Provider String") = "MS Access;Pwd=garbage"
        adoTbl.Properties("Jet OLEDB:Link Datasource") = strDir
    End If
Next

Debug.Print "Link Refreshed..."

End Sub

Also, I noticed that you didn't set your Connection object to a New object:

Set adoCn = New ADODB.Connection

When dealing with ADO, you must set several objects to New, depending on what you wish to accomplish with them ... for example, Recordsets and Connection objects must always be initialized and then set to New:

Dim con As ADODB.Connection
<other stuff here>
Set con = new ADODB.Connection

Also, it's typically not a good idea to do this all in one line:

dim con as New ADODB.Connection

since you cannot trap for errors, should they occur. If you use the syntax as above, and there is a problem initializing your Connection object, ADO will throw the error and your users will be VERY confused <g> ... if you use the two line method, where you Dim the object and then later Set it (AFTER you fire up your error handling code), you can trap the error and deal with it.

0
 
LVL 1

Author Comment

by:gordonwwaugh
ID: 17939578
My code works fine if the database is not password-protected. Doesn't that mean that my connection objects are fine?

I also see that your code actually opens the backend database.

On the other hand, maybe adding the password property requires that things be done differently. I'll give your suggestion a shot and see what happens.
0
 
LVL 85
ID: 17940245
Access handles the two types differently. If your backend is password protected, Access will require you to provide the password in your code, and simply setting your tables to the current connection isn't sufficient. In general, you should ALWAYS build a new connection and use that to relink your tables. Don't depend on the current connection - else what's the point of relinking, if all you're doing is using the same connection?

0
 
LVL 1

Author Comment

by:gordonwwaugh
ID: 17948384
It works!

I very much appreciate you taking the time to explain things. I'll be careful to always build new ADO connections in the future.
0
 
LVL 1

Author Comment

by:gordonwwaugh
ID: 17949146
Actually, it did not work after all. The backend database does not get linked.

The problem is that the table type is never "LINK". Thus, the link datasource line is never executed.

I think that's because you are opening the adodb connection and directly referencing its tables. In fact, I think we need to reference the tables in the current database. Only then will the linked tables have a table type of "LINK."

0
 
LVL 1

Author Comment

by:gordonwwaugh
ID: 18025569
I have received no further replies, so I'm going to close this question now.

I have gone ahead and rewritten my code in DAO. Although it's not the answer I wanted most, using DAO works fine. This is what LSMConsulting suggested in the first place, so I'll consider the question answered.
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

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Suggested Courses

885 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