Question : Relink tables in database with a database password, using ADO

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

Answer : Relink tables in database with a database password, using ADO

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
Random Solutions  
 
programming4us programming4us