Link Table via DAO

后端 未结 2 1463
后悔当初
后悔当初 2021-01-21 15:14

So I am essentially trying to link a table via DAO from an ACCDB that is password-encrypted into the DB I am working in. The premise of what I am doing is that the data is sort

相关标签:
2条回答
  • 2021-01-21 15:40

    Take a closer look at how you're examining the table names in CurrentDb. This line throws error #13, "Type mismatch", on my system:

    Msgbox tbl
    

    I think you should ask for the TableDef.Name instead:

    Msgbox tbl.Name
    

    However, I'm not sure that's the only problem here. You seem to be trying to link to a table in another db file by copying that TableDef and adding it to CurrentDb.TableDefs. IF you can make that work, it won't give you a link to the source table, it would make a new copy in CurrentDb. But I'm skeptical whether it can work at all.

    You could create a new TableDef object, set its Name, Connect, and SourceTableName properties, then append it to CurrentDb.TableDefs. Include the database password in the Connect property.

    Here is code tested in Access 2007.

    Dim db As DAO.Database
    Dim tdf As DAO.TableDef
    Dim strConnect As String
    Dim strDbFile As String
    Dim strLinkName As String
    Dim strPassword As String
    Dim strSourceTableName As String
    
    strDbFile = "C:\share\Access\MyDb.accdb"
    strPassword = "foo"
    strSourceTableName = "Contacts"
    strLinkName = "link_to_contacts"
    
    strConnect = "MS Access;PWD=" & strPassword & _
        ";DATABASE=" & strDbFile
    Debug.Print strConnect
    Set db = CurrentDb
    Set tdf = db.CreateTableDef
    tdf.Connect = strConnect
    tdf.SourceTableName = strSourceTableName
    tdf.Name = strLinkName
    db.TableDefs.Append tdf
    Set tdf = Nothing
    Set db = Nothing
    
    0 讨论(0)
  • 2021-01-21 16:04

    Tables and queries share the same name space in MS Access. Chances are you have a query with the same name as the table you are trying to link.

    Also, Environ("username") is easily spoofed. Consider using the API function GetUserName instead. Of course, if you need real security you'll want to upgrade your back-end to SQL Server (Express) or some other RDBMS.

    0 讨论(0)
提交回复
热议问题