Jason Bayldon
Jason Bayldon

Reputation: 1296

Link Table via DAO

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 of "user sensitive" so I do not want to let every user have access to this table in my front end (have the front-end/back-end split), only specific users. What I would like to do is to check the username of the computer, then allow the front-end to link to the data if the username is correct:

Select Case Environ("username") 'select case user environment name

Case "jsmith" 'if username is jsmith then
Set db = DAO.OpenDatabase("Audit.accdb", False, False, _
";pwd=adaudit12") 'create connection to my other db
Set tbl = db.TableDefs(14) 'selects the table via index
CurrentDb.TableDefs.Append tbl 'create a link to my current DB with this table (throws ex here)

Case Else

End Select

This returns runtime error '3367' Cannot Append. An object with that name already exists in the collection.

So I thought to do this:

For Each tbl In CurrentDb.TableDefs
Msgbox tbl
Next tbl

But the table doesnt exist in my database, so what should I do?

Upvotes: 2

Views: 7430

Answers (2)

HansUp
HansUp

Reputation: 97101

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

Upvotes: 4

mwolfe02
mwolfe02

Reputation: 24207

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.

Upvotes: 2

Related Questions