1

In SQL Server Management Studio, I created both indexed and regular (non indexed) views. When I updated the data, the base table's data was changed as well. However, when I link my views to Access using VBA or using Access GUI linked tables I need to specify a index. If I do not do so the view will be read only.

Why is non indexed view read only in Access but not in SQL?

jedu
  • 1,211
  • 2
  • 25
  • 57

2 Answers2

2

I personally found it very difficult to digest the accepted answer and work out what the problem was and how to resolve it.

Why aren't linked views editable?

Essentially, SQL Server views do not have a Primary Key (PK) and, therefore, are considered read-only (non-updateable) by MS Access because MS Access requires a unique column to use for updates.

How to edit the data in a linked view

A UNIQUE INDEX must be created in MS Access after the view has been linked.

For example, if the table is called dbo_table and the PK column in SQL is called id, create a new query and paste the following into it:

CREATE UNIQUE INDEX myPK ON dbo_table (id ASC)

The word myPK is the name of the index (this can be renamed if needed). Execute the query.

The linked view will now be updateable.

The reference documentation for MS Access CREATE INDEX is available here.

Martin
  • 16,093
  • 1
  • 29
  • 48
1

Well, first of all, when you use the Access GUI to link to a table, it NEVER prompts you for an index, it prompt you for the primary key. So let’s get that crazy and silly misinformation corrected like REAL fast, shall we?

When you link to a SQL server view, you get this prompt:

enter image description here

That above prompt is not asking you for a index.

The reason why access does this is because it needs a PK to update a linked table, or a linked view.

PROBLEM: SQL server does not have a PK defined for a view. And SQL server DOES NOT LET YOU SPECIFY a PK for a view. If SQL server had some way to define a PK for a view, then Access could use that information to grab the PK.

However, since views don’t have a PK on sql server, then when linking to such Views YOU THE HUMAN have to choose a PK.

Access linked tables are read only without a PK. Access linked views are read only without a PK.

However, sql server views do NOT have a PK defined.

Again: SQL server views do NOT have a PK defined.

If you create the linked view with VBA code, then no GUI prompt appears asking you for the PK. You can tell Access which column is to be “assumed” as the PK by executing a local create index command. This command DOES NOT ACTUALLY CREATE a index. It does not create one server side, and it does NOT create a actual working index on the access client side.

All the create index command does is tell access what PK column to use. So you not actually creating a index in this case.

Again: SQL server views do NOT have a PK defined.

If SQL server allowed one to create or set a PK for a view, then Access could simply use that information and know what PK to use, and then set it for you.

However (and again) SQL server does not have a PK defined for a view.

However for the access client to update a linked table, it needs a PK. When linking to a table, Access can simple ask SQL server what column is the PK. However, you cannot ask SQL server

“what is the primary key” of a view.

(however, you can for a table).

So you not creating a index in Access (it does nothing since a index on the client side of access with a linked table, or a linked view is NOT a working index and does NOTHING!!! (except provide a place to tell access what colum is the PK). It is NOT working index nor is it even a real index.

The existence of an index in Access for linked tables/views does not actually create a working index – only that some “extra” information such as if a PK exists in the table is stored when you “create” that index.

Again: Access does not create indexs for linked table. When you execute create index on a linked table A INDEX IS NOT CREATED IN THIS CASE. ONLY INFORMATION ABOUT THE TABLE OR VIEW IS STORED LOCAL IN ACCESS.

Again: you not creating a index.

Again: If sql server had a feature or option to define a PK for a view, then no doubt Access would also use that information. However, views in sql server don’t have a defined PK, and thus Access can’t use the PK, and if access can’t find a PK, then it assumes the table is read only.

The SQL management studio can often update tables without a PK, but Access has to use ODBC to connect to sql server, and ODBC has FAR LESS features then SSMS when connecting to a database.

Albert D. Kallal
  • 42,205
  • 3
  • 34
  • 51
  • I will also add that you can create a index on a sql view, but access needs you to create a PK, and you can't do that. Creating indexes on sql server does bunkus and nothing for access to get and find a PK. You need a PK defined for a linked table, and you need a PK defined for a view - but views on SQL server don't allow you to define a PK. Access is not looking for a index on sql server, it looking for a PK and views don't have a PK, and you can't define a PK for a view on sql server. – Albert D. Kallal Dec 23 '18 at 04:17
  • You said that Access needs to have primary key to be able to update the views or tables. But unique index seems to work as well. I have a linked updatable view whose unique index (not primary key) is created in VBA using "CREATE UNIQUE INDEX PK ON Books bookID" – jedu Dec 23 '18 at 10:27
  • But that command does not create an index, it only serves to tell access what the PK is supposed to be. You can’t actually create an index on a linked table (it not a real index and does not function as an index). And worse, you can have a sql table with a zillion indexes set and that also does not help access. What ONLY helps is having a PK on the sql table – a zillion index on the sql table does zero to fix this issue. That command only tells access what the PK is – it does not create an actual index. So that VBA create index does NOT actually create a index. – Albert D. Kallal Dec 23 '18 at 21:59
  • Even after an index is created on sql server, you can then later on re-execute a “similar” command to include the fact of which column is to be the PK. For last 40 years the create index command has DUEL purpose. So in Access to define a PK for a view then a “similar” command is used. SQL server does not allow a PK for a view. However, Access unlike SQL server DOES allow you to define a PK for a view, and you use the create index command on the access client to tell access which column is the PK. SQL is able to figure out the PK for views, but Access cannot, so you have to tell it. – Albert D. Kallal Dec 23 '18 at 23:30