Form Bound to SQL Indexed View

A

AG

Using Access 2000.

I have a form that opens in datasheet view and is bound to a SQL Server 2005
indexed view.
The view contains several tables and utilizes an 'Instead Of' trigger to
handle inserts and updates in order to apply the data to the correct tables.

The trigger works fine for updates and inserts.

The form works fine for updates.

However, after userting a new record, instead of the new row displaying the
data, it shows #deleted.
In order to show the new record, I need to requery the form.

I am guessing that Access has no way to identify the new row.

Is there a way to avoid the requery?
 
A

AG

Thanks for the reply, but the question is how to avoid a requery. Requering
the form after every insert is not acceptable.
 
D

Dirk Goldgar

AG said:
Using Access 2000.

I have a form that opens in datasheet view and is bound to a SQL Server
2005 indexed view.
The view contains several tables and utilizes an 'Instead Of' trigger to
handle inserts and updates in order to apply the data to the correct
tables.

The trigger works fine for updates and inserts.

The form works fine for updates.

However, after userting a new record, instead of the new row displaying
the data, it shows #deleted.
In order to show the new record, I need to requery the form.

I am guessing that Access has no way to identify the new row.

Is there a way to avoid the requery?


I don't know if this will help or not, but does the table have a timestamp
field? That's been known to help Access identify the specific record, when
dealing with SQL Server tables.
 
A

AG

Thanks for the reply Dirk.

The view consists of about 8 tables, only two of which get updates or
inserts. The rest are lookup tables that are included in the view mainly so
that the form can be sorted on their values. However, if the new record
requires a new record entered in a lookup table, the trigger handles that
also.

Both of the main tables have timestamps and both timestamps are included in
the view. Without the timestamps, Access raised the error of 'someone else
changed the record...'.

If the insert only involves the two main tables, there is no problem. Access
displays the new record.
The problem occurs is the insert requires an addition to one of the lookup
tables.
 
M

M.

Perhaps you could use the following properties of the recordset object:
ADO: recordset.bookmark?
DAO: recordset.bookmark OR recordset.lastmodified
I don't know if this will work out with a backend SQL server. Only have
experience with Access backend, where I use the combination (DAO)

rs.addnew
and
rs.lastmodified

to find the autonumber (primary key) of the new record in this rs to insert
into a child table in the same transaction.

Regards,

M.
 
A

AG

Thanks for the reply, but I don't understand. It is a bound datasheet form,
not code adding the record via code.
 
C

Charles Wang [MSFT]

Hi AG,
Access 2000 is not supported by Microsoft now. Could you please first check
if this issue can be reproduced in Access 2003 or later version? If so, I
recommend that you mail me (changliw_at_microsoft_dot_com) a sample
database so that I can reproduce your issue at my side. Per my test, if I
created a linked table from an updatable view in SQL Server 2005, I could
not insert/update any row in Access 2007, though I could insert new rows in
SSMS (SQL Server 2005 Management Studio).

My test script is as following:
============================================================================
==
CREATE TABLE A1
(ID int not null primary key,
NAME nvarchar(20) not null,
CreateDate datetime null)

CREATE TABLE A2
(ID int not null identity(1,1) primary key,
A1_ID int not null,
Qty int not null,
A3_ID int not null,
CreateDate datetime)

CREATE TABLE A3
(ID int not null primary key,
NAME nvarchar(20) not null
)

ALTER VIEW dbo.v_A1A2A3
WITH SCHEMABINDING
AS
SELECT A1.ID AS A1ID, A1.[NAME] AS A1Name,A1.[CreateDate],A2.ID As A2ID,
A2.Qty,A3.ID As A3ID, A3.Name as A3Name
FROM dbo.A1 JOIN dbo.A2 ON A1.ID=A2.A1_ID JOIN dbo.A3 ON A2.A3_ID=A3.ID


CREATE UNIQUE CLUSTERED INDEX IDX_vA1A2A3 ON dbo.v_A1A2A3(A2ID)

create trigger trg_v_A1A2A3
ON v_A1A2A3
INSTEAD OF INSERT, UPDATE
AS
DECLARE @rc as INT
SET @rc = @@rowcount

IF @rc = 0
RETURN;

IF EXISTS(SELECT * FROM inserted)
BEGIN
IF EXISTS(SELECT * FROM deleted)
BEGIN
UPDATE A1 SET [NAME]=deleted.A1NAME FROM deleted WHERE A1.ID=deleted.A1ID
UPDATE A2 SET Qty = deleted.Qty,CreateDate=getdate() FROM deleted WHERE
A2.ID=deleted.A2ID
UPDATE A3 SET [NAME]= deleted.[A3NAME] FROM deleted WHERE
A3.ID=deleted.A3ID
END
ELSE
BEGIN
IF NOT EXISTS(SELECT * FROM A1, inserted WHERE A1.ID=inserted.A1ID)
BEGIN
INSERT INTO A1(ID,[Name],CREATEDATE) SELECT A1ID,A1NAME,getdate() FROM
inserted;
END
IF NOT EXISTS(SELECT * FROM A3, inserted WHERE A3.ID=inserted.A3ID)
BEGIN
INSERT INTO A3(ID,[Name]) SELECT A3ID,A3NAME FROM inserted;
END
INSERT INTO A2(A1_ID,Qty,A3_ID,CreateDate) SELECT A1ID,QTY,A3ID,GETDATE()
FROM inserted
END
END


INSERT INTO v_A1A2A3 VALUES(2,'H2',getdate(),2,20,1,'A3.1')
===============================================================

Look forward to your response. If you have any other questions or concerns,
please feel free to let me know.

Best regards,
Charles Wang
Microsoft Online Community Support
===========================================================
Delighting our customers is our #1 priority. We welcome your
comments and suggestions about how we can improve the
support we provide to you. Please feel free to let my manager
know what you think of the level of service provided. You can
send feedback directly to my manager at: (e-mail address removed).
===========================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for
non-urgent issues where an initial response from the community
or a Microsoft Support Engineer within 1 business day is acceptable.
Please note that each follow up response may take approximately
2 business days as the support professional working with you may
need further investigation to reach the most efficient resolution.
The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by
contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
============================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
=========================================================
 
A

AG

Thanks Charles.
It works fine in Access 2003. I was considering having client upgrade
anyway, so now that clinches it.
I did, however, find out what is going on. The problem is in Access
identifying the new data row in order to repopulate the row on screen.
http://support.microsoft.com/default.aspx?scid=kb;en-us;128809
I guess 2003 handles it better.

My actual project is converting a complex application with an mdb front end
and back end to a SQL Server back end.
Would there be any additional advantages/features (regarding SQL Server) if
I were to go to 2007?

This does raise another question.
It takes Access two or three tries to get the new data row because it does
not have the new PK value.
Initially, it queries for a null PK, which of course, will return nothing,
so it then queries again using other field data, which returns the new data
row.

When the row is inserted, there must be some communication from SQL Server
to Access to let Access know that the insert was successful. Isn't there
some way to return the new PK value so that Access can get the data on the
first try?

--

AG
Email: discussATadhdataDOTcom
"Charles Wang [MSFT]" said:
Hi AG,
Access 2000 is not supported by Microsoft now. Could you please first
check
if this issue can be reproduced in Access 2003 or later version? If so, I
recommend that you mail me (changliw_at_microsoft_dot_com) a sample
database so that I can reproduce your issue at my side. Per my test, if I
created a linked table from an updatable view in SQL Server 2005, I could
not insert/update any row in Access 2007, though I could insert new rows
in
SSMS (SQL Server 2005 Management Studio).

My test script is as following:
============================================================================
==
CREATE TABLE A1
(ID int not null primary key,
NAME nvarchar(20) not null,
CreateDate datetime null)

CREATE TABLE A2
(ID int not null identity(1,1) primary key,
A1_ID int not null,
Qty int not null,
A3_ID int not null,
CreateDate datetime)

CREATE TABLE A3
(ID int not null primary key,
NAME nvarchar(20) not null
)

ALTER VIEW dbo.v_A1A2A3
WITH SCHEMABINDING
AS
SELECT A1.ID AS A1ID, A1.[NAME] AS A1Name,A1.[CreateDate],A2.ID As A2ID,
A2.Qty,A3.ID As A3ID, A3.Name as A3Name
FROM dbo.A1 JOIN dbo.A2 ON A1.ID=A2.A1_ID JOIN dbo.A3 ON A2.A3_ID=A3.ID


CREATE UNIQUE CLUSTERED INDEX IDX_vA1A2A3 ON dbo.v_A1A2A3(A2ID)

create trigger trg_v_A1A2A3
ON v_A1A2A3
INSTEAD OF INSERT, UPDATE
AS
DECLARE @rc as INT
SET @rc = @@rowcount

IF @rc = 0
RETURN;

IF EXISTS(SELECT * FROM inserted)
BEGIN
IF EXISTS(SELECT * FROM deleted)
BEGIN
UPDATE A1 SET [NAME]=deleted.A1NAME FROM deleted WHERE A1.ID=deleted.A1ID
UPDATE A2 SET Qty = deleted.Qty,CreateDate=getdate() FROM deleted WHERE
A2.ID=deleted.A2ID
UPDATE A3 SET [NAME]= deleted.[A3NAME] FROM deleted WHERE
A3.ID=deleted.A3ID
END
ELSE
BEGIN
IF NOT EXISTS(SELECT * FROM A1, inserted WHERE A1.ID=inserted.A1ID)
BEGIN
INSERT INTO A1(ID,[Name],CREATEDATE) SELECT A1ID,A1NAME,getdate() FROM
inserted;
END
IF NOT EXISTS(SELECT * FROM A3, inserted WHERE A3.ID=inserted.A3ID)
BEGIN
INSERT INTO A3(ID,[Name]) SELECT A3ID,A3NAME FROM inserted;
END
INSERT INTO A2(A1_ID,Qty,A3_ID,CreateDate) SELECT A1ID,QTY,A3ID,GETDATE()
FROM inserted
END
END


INSERT INTO v_A1A2A3 VALUES(2,'H2',getdate(),2,20,1,'A3.1')
===============================================================

Look forward to your response. If you have any other questions or
concerns,
please feel free to let me know.

Best regards,
Charles Wang
Microsoft Online Community Support
===========================================================
Delighting our customers is our #1 priority. We welcome your
comments and suggestions about how we can improve the
support we provide to you. Please feel free to let my manager
know what you think of the level of service provided. You can
send feedback directly to my manager at: (e-mail address removed).
===========================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for
non-urgent issues where an initial response from the community
or a Microsoft Support Engineer within 1 business day is acceptable.
Please note that each follow up response may take approximately
2 business days as the support professional working with you may
need further investigation to reach the most efficient resolution.
The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by
contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
============================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
=========================================================
 
C

Charles Wang [MSFT]

Hi AG,
Regarding your question, "Would there be any additional advantages/features
(regarding SQL Server) if I were to go to 2007?", though few documents
clearly talk about this. Per my use experiences, I do feel that Access 2007
has better performance and more effective communications with SQL Server.
In addition, you cannot utilize the SQL Server 2005 features from older
version ADP project. I recommend that you upgrade your Access 2000 to 2007
so that you can get more benefits from Access 2007. You can refer to the
articles:
Microsoft Office Access 2007 top 10 benefits
http://office.microsoft.com/en-us/access/HA101650211033.aspx
Database specifications
http://office.microsoft.com/en-us/access/HA100307391033.aspx

Regarding new PK value, I think that you need not worry about this in
Access 2007. Assume that your SQL table has an identity column, you can
conveniently add new rows as well as in SQL Server Management Studio. The
new PK value will be returned immediately after you add a new record.

Hope this helps. Please feel free to let me know if you have any other
questions or concerns.


Best regards,
Charles Wang
Microsoft Online Community Support
=========================================================
Delighting our customers is our #1 priority. We welcome your
comments and suggestions about how we can improve the
support we provide to you. Please feel free to let my manager
know what you think of the level of service provided. You can
send feedback directly to my manager at: (e-mail address removed).
=========================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
=========================================================
 
A

AG

Thanks Charles.

So are you saying that with Access 2007 the PK for the new row would be
returned with the 'save' and Access could then retrieve the complete new row
on the first try, rather than two or three tries based on other fields?

The articles in the links are very general. I can't seem to find anything
that explains any real advantages of 2007 over 2003 regarding SQL Server,
other than 'it's better'.

Does 2007 still use ODBC to connect to SQL server?
 
C

Charles Wang [MSFT]

Hi AG,
Regarding your concerns, please see the following answers:
So are you saying that with Access 2007 the PK for the new row would be
returned with the 'save' and Access could then retrieve the complete new row
on the first try, rather than two or three tries based on other fields?

Yes. Once you finished inputting a new row, the PK value will be
automatically returned into the new row's PK field. You can see it.
Does 2007 still use ODBC to connect to SQL server?

Yes, for Access 2007 database.

Please feel free to let me know if you have any other questions or
concerns. Have a nice day!

Best regards,
Charles Wang
Microsoft Online Community Support
=========================================================
Delighting our customers is our #1 priority. We welcome your
comments and suggestions about how we can improve the
support we provide to you. Please feel free to let my manager
know what you think of the level of service provided. You can
send feedback directly to my manager at: (e-mail address removed).
=========================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
=========================================================
 
A

AG

Thanks Charles. I am going to install 2007 and start exploring.

--

AG
Email: discussATadhdataDOTcom
"Charles Wang [MSFT]" said:
Hi AG,
Regarding your concerns, please see the following answers:
So are you saying that with Access 2007 the PK for the new row would be
returned with the 'save' and Access could then retrieve the complete new row
on the first try, rather than two or three tries based on other fields?

Yes. Once you finished inputting a new row, the PK value will be
automatically returned into the new row's PK field. You can see it.
Does 2007 still use ODBC to connect to SQL server?

Yes, for Access 2007 database.

Please feel free to let me know if you have any other questions or
concerns. Have a nice day!

Best regards,
Charles Wang
Microsoft Online Community Support
=========================================================
Delighting our customers is our #1 priority. We welcome your
comments and suggestions about how we can improve the
support we provide to you. Please feel free to let my manager
know what you think of the level of service provided. You can
send feedback directly to my manager at: (e-mail address removed).
=========================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
=========================================================
 
A

AG

Charles,

I installed Office 2007 and converted my small test mdb to Access 2007
format.
I opened SQL Profiler, like I did when running the Access 2003 version.
There seems to be no difference in the way Access interacts with SQL Server
2005.
After an insert, Access tries to retrieve the row via a select <fields> From
<myview> Where <pk field> is null.
Obviousely, that will return nothing, but it then tries again with the exact
same request.
When that fails, Access then makes a new request using all the other fields
(except the PK) for the where criteria, and that returns the new row.

I guess this is by design, but just does not make sense. Access will not
recognize a view (or table) as updateable without a unique key, so what is
the sense in doing a select based on a null key (twice)? Just seems very
inefficient to me.

--

AG
Email: discussATadhdataDOTcom
"Charles Wang [MSFT]" said:
Hi AG,
Regarding your concerns, please see the following answers:
So are you saying that with Access 2007 the PK for the new row would be
returned with the 'save' and Access could then retrieve the complete new row
on the first try, rather than two or three tries based on other fields?

Yes. Once you finished inputting a new row, the PK value will be
automatically returned into the new row's PK field. You can see it.
Does 2007 still use ODBC to connect to SQL server?

Yes, for Access 2007 database.

Please feel free to let me know if you have any other questions or
concerns. Have a nice day!

Best regards,
Charles Wang
Microsoft Online Community Support
=========================================================
Delighting our customers is our #1 priority. We welcome your
comments and suggestions about how we can improve the
support we provide to you. Please feel free to let my manager
know what you think of the level of service provided. You can
send feedback directly to my manager at: (e-mail address removed).
=========================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
=========================================================
 
C

Charles Wang [MSFT]

Hi AG,
Thank you for your response. I think that there must be something different
between our tests.

Per my test, the new PK value was returned immediately. I connected to a
SQL Server 2005 instance (ODBC) from Access 2007 and opened the table with
Datasheet View, then I added a new row, and when I finished my input, the
new ID was automatically returned.

Could you please elaborate your process so that I can perform a test to
check if I can reproduce your issue?

Look forward to your response. Have a nice day!


Best regards,
Charles Wang
Microsoft Online Community Support
=========================================================
Delighting our customers is our #1 priority. We welcome your
comments and suggestions about how we can improve the
support we provide to you. Please feel free to let my manager
know what you think of the level of service provided. You can
send feedback directly to my manager at: (e-mail address removed).
=========================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
=========================================================
 
A

AG

Charles,

Please re-read my last post.
I did not say it was never returned. Fire up SQL Server Profiler and monitor
what is going on. You will see that it takes Access three tries to get the
new row. It just seems inefficient to me.
 
C

Charles Wang [MSFT]

Hi AG,
What I saw from SQL Profiler was as following:
====================================
[RPC:Completed] exec sp_executesql N'INSERT INTO "dbo"."T2" ("NAME")
VALUES (@P1)',N'@P1 varchar(20)','asdfasdf'

[SQL:BatchStarting] SELECT @@IDENTITY
[SQL:BatchCompleted] SELECT @@IDENTITY

[RPC:Completed] declare @p1 int
set @p1=1
exec sp_prepexec @p1 output,N'@P1 int',N'SELECT "ID","NAME" FROM
"dbo"."T2" WHERE "ID" = @P1',6
select @p1

[RPC:Completed] exec sp_execute 1,6

[RPC:Completed] declare @p1 int
set @p1=2
exec sp_prepexec @p1 output,N'@P1 int,@P2 int,@P3 int,@P4 int,@P5 int,@P6
int,@P7 int,@P8 int,@P9 int,@P10 int',N'SELECT "ID","NAME" FROM "dbo"."T2"
WHERE "ID" = @P1 OR "ID" = @P2 OR "ID" = @P3 OR "ID" = @P4 OR "ID" = @P5
OR "ID" = @P6 OR "ID" = @P7 OR "ID" = @P8 OR "ID" = @P9 OR "ID" =
@P10',1,2,3,4,5,6,6,6,6,6
select @p1
======================================

That looks fine to me. Could you please let me know what you saw and what
your expected result were from SQL Profiler?

Best regards,
Charles Wang
Microsoft Online Community Support
=========================================================
Delighting our customers is our #1 priority. We welcome your
comments and suggestions about how we can improve the
support we provide to you. Please feel free to let my manager
know what you think of the level of service provided. You can
send feedback directly to my manager at: (e-mail address removed).
=========================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
=========================================================
 
A

AG

Thanks Charles.

In my case I don't get 'SELECT @@IDENTITY'
I looked at your example again and it is similar to mine, but I am not clear
on how you are adding the new record.

Where are you executing the statement INSERT INTO v_A1A2A3
VALUES(2,'H2',getdate(),2,20,1,'A3.1')?

My situation is a bound Access datasheet view form.
 
C

Charles Wang [MSFT]

Hi AG,
I am sorry that my last test was using a table but not an updatable view.
Regarding an updatable view, it indeed had the issue as you mentioned. I
agree with you that it is really inefficient. However I am curious why a
single table worked fine, but an updatable view failed. Now I am trying to
consult our product team to see why this issue happened. I may need more
time to get back to you. Appreciate your patience.

Best regards,
Charles Wang
Microsoft Online Community Support
=========================================================
Delighting our customers is our #1 priority. We welcome your
comments and suggestions about how we can improve the
support we provide to you. Please feel free to let my manager
know what you think of the level of service provided. You can
send feedback directly to my manager at: (e-mail address removed).
=========================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
=========================================================
 
A

AG

Thanks Charles,

I am glad that you saw the same thing that I did. I too, am very interested
in their answer!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top