Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

sql server - How to set primary key when linking with CreateTableDef

In a MS Access database, I'm connecting to views in a SQL Server database like this:

Dim s  As String
s = "ODBC;DSN=mydb;Trusted_Connection=Yes;DATABASE=mydb;"

Dim td As TableDef
Set td = CurrentDb.CreateTableDef("vwMyView", 0, "MySchema.vwMyView", s)

CurrentDb.TableDefs.Append td
CurrentDb.TableDefs.Refresh

This creates a linked table, which is linked to a view in SQL Server.

However, I cannot insert/update/delete, because Access does not know the "primary key". How can the information about the primary key added in VBA ?

When using the Linked Table Wizard, you are always asked to select the unique key columns from a listbox. I want to reproduce this behaviour in VBA.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
CREATE INDEX <YourIndexName> ON <YourTableName>(<YourFieldName>) WITH PRIMARY

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...