Page 1 sur 1

[R] COM erreur 80020009 sur $cat.Tables.Append($Tb)

Posté : ven. 22 mai 2009 06:58
par pw93
Bonjour,
Débutant en AutoIt, je cherche à migrer mes précédents codes VB / VBA en AutoIt. J'ai un pb (générique) pour lier une table d'une base mdb (Access) dans une autre base mdb.
Je plante systématiquement sur la ligne $adox_catalog.Tables.Append($adox_table) .

J'ai déjà cherché une solution dans les forums Français / Américain / Allemand et Google sans succès. Les UDF trouvés (Access.au3 et AccessCom.au3) ne contiennent pas de code pour effectuer des LinkTables.

Code : Tout sélectionner

; ----------------------------------------------------------------------------  
;   AutoIt Version : 3.3.0.0
;   Auteur:          pw93 at live point fr
;   Date  :          2009-05-22 
;   Fonction du Script : 
;      Lier la table Access ZIPTPW01.ZIPTPW01 en tant que CUSTOMER.ZIPTPW01
;   --> Se plante sur la ligne : $adox_catalog.Tables.Append($adox_table)
; 
;   err.description    : COM Erreur 80020009 Multiple-step OLE DB operation generated errors.
;   err.source         : Microsoft JET Database Engine
;   err.windescription : ....  des caractères en chinois....
; ----------------------------------------------------------------------------
Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
Local $CustomerMdbPath = "c:\$user\$user"
Local $CustomerMdbName = "CUSTOMER"
Local $CustomerTbName = "CLITPW00"

Local $ZipfooMdbPath = "c:\$user\$user"
Local $ZipfooMdbName = "ZIPTPW01"
Local $ZipfooTbName = "ZIPTPW01"

_TestAddLinkedTable($ZipfooMdbPath, $ZipfooMdbName, $ZipfooTbName, $CustomerMdbPath, $CustomerMdbName, "ZIPTPW01")
Exit

;--------------------------------------- Functions ---------------------------
Func _TestAddLinkedTable($LinkedMdbPath, $LinkedMdbName, $LinkedTbName, $MdbPath, $MdbName, $TbName)
    Dim $conn
    $conn = ObjCreate("ADODB.Connection")
    $conn.Open( _
        "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & $MdbPath & "\" & $MdbName & ".mdb" & ";" & _
        "Jet OLEDB:Engine Type=5;")

    Dim $adox_catalog 
    $adox_catalog = ObjCreate("ADOX.Catalog")   
    $adox_catalog.ActiveConnection() = $conn
    
    Dim $adox_table
    $adox_table = ObjCreate("ADOX.Table")
    With $adox_table
        .Name = $TbName
        .ParentCatalog = $adox_catalog
        .Properties("Jet OLEDB:Link Datasource").Value = $LinkedMdbPath & "\" &  $LinkedMdbName & ".mdb"
        .Properties("Jet OLEDB:Remote Table Name").Value = $LinkedTbName
        .Properties("Jet OLEDB:Create Link").Value = True
    EndWith
    [color=#FF0000]$adox_catalog.Tables.Append($adox_table)[/color]
    
    $adox_table = ''
    $adox_catalog = ''
    $conn.Close
    $conn = ''
EndFunc   ;==>_TestAddLinkedTable
 
Pour info le code en VB suivant fonctionne parfaitement dans un vbs / wsf:

Code : Tout sélectionner

Sub AddLinkedTable(ByVal LinkedMdbPath, ByVal LinkedMdbName, ByVal LinkedTbName, ByVal MdbPath, ByVal MdbName, ByVal TbName)    
    Dim cnn
    Set cnn = CreateObject("ADODB.Connection")
    cnn.Open ("Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & MdbPath & "\" & MdbName & ".mdb" & ";" & _
        "Jet OLEDB:Engine Type=5;")
    Dim adox_catalog
    Set adox_catalog=CreateObject("ADOX.Catalog")
    Dim adox_table
    Set adox_table=CreateObject("ADOX.Table")
    adox_catalog.ActiveConnection() = cnn   
    With adox_table
        .Name = TbName
        .ParentCatalog = adox_catalog
        .Properties("Jet OLEDB:Link Datasource").Value = LinkedMdbPath & "\" & LinkedMdbName & ".mdb"
        .Properties("Jet OLEDB:Remote Table Name").Value = LinkedTbName
        .Properties("Jet OLEDB:Create Link").Value = True
    End With
    adox_catalog.Tables.Append adox_table
    Set adox_table = Nothing
    Set adox_catalog = Nothing
    cnn.Close
    Set cnn = Nothing
End Sub
 
J'ai alors fait les tests suivants:
1er test) en utilisant le DAO ---> Celà fonctionne avec le code ci-dessous !

Code : Tout sélectionner

Func _DAOTestAddLinkedTable($LinkedMdbPath, $LinkedMdbName, $LinkedTbName, $MdbPath, $MdbName, $TbName)
    Local $engine = ObjCreate("DAO.DBEngine.36")    
    Local $dbs = $engine.OpenDatabase($MdbPath & "\" & $MdbName & ".mdb", False, False)     
    Local $tdfLnked = ObjCreate("DAO.TableDef.36")
    $tdfLinked = $dbs.CreateTableDef($TbName)
    $tdfLinked.Connect = ";DATABASE=" & $LinkedMdbPath & "\" & $LinkedMdbName & ".mdb"
    $tdfLinked.SourceTableName = $LinkedTbName
    $dbs.TableDefs.Append($tdfLinked)
    $dbs.Close
    $tdfLinked = ""
    $dbs = ""
    $engine = ""
EndFunc   ;==>_DAOTestAddLinkedTable 
2eme test) En créant une table locale (pour tester la commande $adox_catalog.Tables.Append($adox_table) ---> Celà fonctionne avec le code ci-dessous !

Code : Tout sélectionner

Func _TestCreateLocalTable($MdbPath, $MdbName)
    Local $adox_table = ObjCreate("ADOX.Table")
    With $adox_table
        .Name = "TESTNAME"
        .Columns.Append("id", 3)
        .Columns.Append("surname", 202, 30)
        .Columns.Append("firstname", 202, 30)
        .Keys.Append("pk_name_id", 1, "id")
    EndWith
    Local $adox_catalog = ObjCreate("ADOX.Catalog")
    With $adox_catalog
        .ActiveConnection() = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                "Data Source=" & $MdbPath & "\" & $MdbName & ".mdb" & ";" & _
                "Jet OLEDB:Engine Type=5;"
        .Tables.Append($adox_table)
    EndWith
    $adox_catalog = ""
    $adox_table = ""
EndFunc   ;==>_TestCreateLocalTable
Je ne vois vraiment pas ce qui cloche dans mon premier code.....
Quelqu'un aurait il LA solution ?

Re: [..] COM erreur 80020009 sur $cat.Tables.Append($Tb)

Posté : ven. 22 mai 2009 07:59
par Tlem
Je pense que c'est une question pour GaRydelaMer (le pro de la base de donnée). :lol:

Re: [..] COM erreur 80020009 sur $cat.Tables.Append($Tb)

Posté : dim. 24 mai 2009 23:22
par ani
l'erreur soumise doit signifié n'a pu etre excécuté (créer).
1.Soit çà vient de la conversion
2.Soit çà vient des variables soumises pour la fonction testaddlinkedtable (erreur dans les parametres)
vu l'image il parle de Microsoft.Jet.OLEDB.4.0 voyant ton exemple il manque le caract§re &

1.Conversion VBS to AU3

Code : Tout sélectionner

Func AddLinkedTable($LinkedMdbPath, $LinkedMdbName, $LinkedTbName, $MdbPath, $MdbName, $TbName)    
    Dim $cnn, $Nothing = 0
    Local $cnn = ObjCreate("ADODB.Connection")
    $cnn.Open ("Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & $MdbPath & "\" & $MdbName & ".mdb" & ";" & _
        "Jet OLEDB:Engine Type=5;")
    Dim $adox_catalog
    Local $adox_catalog=ObjCreate("ADOX.Catalog")
    Dim $adox_table
    Local $adox_table=ObjCreate("ADOX.Table")
    $adox_catalog.ActiveConnection() = $cnn   
    With $adox_table
        .Name = $TbName
        .ParentCatalog = $adox_catalog
        .Properties("Jet OLEDB:Link Datasource").Value = $LinkedMdbPath & "\" & $LinkedMdbName & ".mdb"
        .Properties("Jet OLEDB:Remote Table Name").Value = $LinkedTbName
        .Properties("Jet OLEDB:Create Link").Value = 1
    EndWith
    $adox_catalog.Tables.Append($adox_table)
    Local $adox_table = $Nothing
    Local $adox_catalog = $Nothing
    $cnn.Close
    Local $cnn = $Nothing
EndFunc

Modications :
  • Ajout de la variable $Nothing = 0
  • Changement du mot set en Local
  • Changement de la valeur true en 1 a cette ligne

    Code : Tout sélectionner

    .Properties("Jet OLEDB:Create Link").Value = [color=#FF0000]true[/color]
2.Les paramètrages soumis à la fonction
Ouverture du fichier $MdbPath & "\" & $MdbName & ".mdb"
!! :arrow: $CustomerMdbPath & "\" & $CustomerMdbName & ".mdb"
Ce fichier va lire une table sous cette variable $TbName
!! Dans l'exemple vous indiqué la valeur de la variable $ZipfooTbName ("ZIPTPW01") et non celui de $CustomerTbName. :?: le soucis ne viendrai t'il pas de là ?


bonne continuation ;)

Re: [..] COM erreur 80020009 sur $cat.Tables.Append($Tb)

Posté : lun. 25 mai 2009 08:10
par pw93
Bravo ani, You got it ! :D
Le pb initial était dû à l'affectation de la valeurTrue à la Propriété ("Jet OLEDB:Create Link"). Il fallait lui assigner la valeur 1.
Et j'ai bien noté que pour supprimer un Objet, il fallait lui assigner la valeur 0 et non "".
Voici le code final qui fonctionne :

Code : Tout sélectionner

;===============================================================================
; Function Name:    _AddLinkedTable()
; Description:      Link an Access Table to another Access Base.
; Syntax:           _AddLinkedTable($LinkedMdbPath, $LinkedMdbName, $LinkedTbName, $MdbPath, $MdbName, $TbName)
; Parameter(s):     $LinkedMdbPath  - The path of the source database to be linked (without the final \)
;                   $LinkedMdbName  - The name of the source database to be linked (filename without extension .mdb)
;                   $LinkedTbName   - The name of the source table to be linked
;                   $MdbPath        - The path of the destination database to append the linked table to (without the final \)
;                   $MdbName        - The name of the destination database to append the linked table to (filename without extension .mdb)
;                   $TbName         - The name of the destination table
; Requirements:     ADODB.Connection, ADOX.Catalog, ADOX.Table
; Author(s):        pw93 with help from ani  (http://www.autoitscript.fr/forum)
; Notes:            WARNING: The True value isn't equivalent to 1 to assign the "Jet OLEDB:Create Link" Property
; Modifications:
;===============================================================================
Func _AddLinkedTable($LinkedMdbPath, $LinkedMdbName, $LinkedTbName, $MdbPath, $MdbName, $TbName)
    Local $conn = ObjCreate("ADODB.Connection")
    Local $adox_catalog = ObjCreate("ADOX.Catalog")
    Local $adox_table = ObjCreate("ADOX.Table")
    Local $Nothing = 0
    $conn.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=" & $MdbPath & "\" & $MdbName & ".mdb" & ";" & _
            "Jet OLEDB:Engine Type=5;")
    $adox_catalog.ActiveConnection = $conn

    With $adox_table
        .Name = $TbName
        .ParentCatalog = $adox_catalog
        .Properties("Jet OLEDB:Link Datasource") = $LinkedMdbPath & "\" & $LinkedMdbName & ".mdb"
        .Properties("Jet OLEDB:Remote Table Name") = $LinkedTbName
        [color=#40BF00].Properties("Jet OLEDB:Create Link") = 1 ; WARNING: Error if the True value is used ![/color]
    EndWith
    $adox_catalog.Tables.Append($adox_table)

    Local $adox_table = $Nothing
    Local $adox_catalog = $Nothing
    $conn.Close
    Local $conn = $Nothing
EndFunc   ;==>_AddLinkedTable
Encore Merci pour ton aide.