VBA Code to Add Linked Table with Primary Key

Ed S.

Board Regular
Joined
Mar 26, 2002
Messages
90
(MS Access at version 97.)

Does anyone have a definite answer.

Been trying to add a Linked Table with a primary key using VBA. (Can be accomplished mannually using New Tables Wizard (very easy)).

Here is the main issue: Since a Linked Table w/Primary key can be done manually, would seem reasonable it can be done programatically (VBA)?

If the Linked Table must be deleted first - that's ok because the database is on a server and connected via odbc. I am only interested in adding (not modifying) the Linked table w/primary key. Once in place MS Access does not allow us to modify a Linked table's primary Key(s).

********
The following code gets real close but when executed, recieve error message: Run time error'3367':
Can't Append. An object with the name already exists in the collection.

Option Compare Database
Option Explicit
Dim db As Database
Dim tdf As TableDef
Dim strDSNName As String
Dim strTableName As String
Dim Idx As Index
Dim Fld As Field
'
Sub BuildTableLink()

Set db = CurrentDb
Set strTableName = "NewTable"
'
Set tdf = db.CreateTableDef(strTableName)
tdf.Connect = "ODBC;DSN="DSN01";;TABLE=" & strTableName & ""
'
tdf.SourceTableName = "ExternalTableName"
'
db.TableDefs.Append tdf
'
Set Idx = tdf.CreateIndex("PrimaryKey")
Set Fld = tdf.Fields("FldA")
Idx.Primary = True
Idx.Unique = True

* * * * Following line Failes * * * *
Idx.Fields.Append Fld
tdf.Indexes.Append Idx
'
End Sub

Watch variable indicates 0 objects for Fields in Idx collection???

To reemphasize: tring to add a Linked Table with primary key using VBA.
 
good news
=========
I've got a solution

bad news
========
It's in Spanish!

Basically you can execute a sql command against the tabledef to create the index. (The name of the index might be important.)

Here's an example of the sql that I ran against my test table and which worked.

CREATE UNIQUE INDEX _uniqueindex ON
NewTable (ReportId)
WITH PRIMARY;

The solution involves dynamically creating this SQL and then executing it against the tabledef.

I think you've got sufficient vba skills to take it from here.

..but let me know if you get stuck,

Dave.

Here's your link

http://www.recursos-as400.com/utilidades.shtml
 
Upvote 0

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Dave,

Wow, worked like a charm. For the next person - here is the working snippet:


Option Compare Database
Option Explicit

Sub BuildTableLink()

Dim db As Database
Dim tdf As TableDef
Dim strDSNName As String
Dim strTableName As String
Dim strDDL As String
Dim strFieldName As String

Set db = CurrentDb
strTableName = "NewTable"
strFieldName = "FldA"

Set tdf = db.CreateTableDef(strTableName)
tdf.Connect = "ODBC;DSN=DSN01;;TABLE=" & strTableName & ""

tdf.SourceTableName = "ExternalTableName"
'
db.TableDefs.Append tdf

strDDL = "CREATE UNIQUE INDEX _uniqueindex ON " & _
strTableName & " (" & strFieldName & ")" & _
"WITH PRIMARY;"

db.Execute strDDL

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,917
Members
449,093
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top