Any way to edit data on MS-Access form using SQL View with two tables

左心房为你撑大大i 提交于 2021-02-05 06:11:26

问题


From what I've read it should be possible to edit data described by a view if the data being modified represents only one table and a unique identifier field is included in the data.

According to Microsoft

Any modifications must reference columns from only one base table

Has anyone had any luck creating an MS-Access form that is editable when the underlying recordset is based on a view or stored procedure merging data from two tables?

My form only used the secondary table to join a code table to get a text based description - but doing so makes the form uneditable.

Do I need to pull that out and create additional lookup code for the form? Or is there someone that can point me to an example that works for them?

This is what my view looks like now for the form I'm trying to use to edit PreliminaryInvoices table

SELECT        t1.*, t2.JobTypeGroup AS Grouping
FROM          dbo.PreliminaryInvoices AS t1 
INNER JOIN    dbo.tblJobTypes AS t2 
ON t1.JobTypeID = t2.JobTypeID

EDIT - updated to show current working solution - no second table

The only way I've been able to get it to work is to split out the second table and add a lookup to display the text from the JobTypeID

Current Editable View:

SELECT        t1.*
FROM          dbo.PreliminaryInvoices AS t1 

Lookup procedure:

CREATE PROCEDURE [dbo].[spJobTypeGrouping]
    -- Add the parameters for the stored procedure here
    @TypeID int
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT  dbo.tblJobTypes.[Grouping] 
    FROM    dbo.tblJobTypes 
    WHERE   dbo.tblJobTypes.JobTypeiD = @TypeID
END

回答1:


You should create instead of triggers on the view so you can update the the underlying table. You don't have to update all of the fields in the trigger. If you create the appropriate triggers, Access will treat it like a table.

As for fields that aren't covered by your trigger. If a user tries to update the table, the UI will appear to modify fields, but if you reopen the view the original values will return.

Here's a trigger that will handle an insert, update, or delete. Note that in the event of an update, insert and delete results sets are populated. This isn't the most efficient way to handle things, but you're updating linked views in Access so...

CREATE TRIGGER vwInvoices_Modify ON vwInvoices
  INSTEAD OF INSERT, UPDATE, DELETE
AS 

  SET NOCOUNT ON;

  -- delete non-updates
  DELETE FROM PreliminaryInvoices WHERE Id IN (
    SELECT Id FROM deleted EXCEPT SELECT Id FROM inserted
  );

  -- insert non-updates
  INSERT INTO PreliminaryInvoices (Id, colA, colB)
    SELECT Id, colA, colB FROM inserted WHERE Id NOT IN (SELECT Id FROM deleted)

  -- updates
  UPDATE PreliminaryInvoices SET
    colA = ins.colA
    colB = ins.colB
  FROM inserted ins
  WHERE ins.Id IN (SELECT Id FROM deleted)
    AND PreliminaryInvoices.Id = ins.Id

END



回答2:


If you want to update a view that's not fully updateable from within Microsoft Access, I recommend you use triggers in SQL server to execute the update.

You can define an INSTEAD OF UPDATE trigger to override the default update behaviour.

You can read about INSTEAD OF triggers on the MSDN. These allow you to specify exactly what happens when a field is updated, and even allow you to update calculated columns (for example, changing someone's birthdate when a calculated age column is changed).

To work with T-SQL queries and updateable views, you need to bind the form to an ADO recordset. You can use code similar to the following code:

Private Sub Form_Load()
    Dim conn As ADODB.Connection
    'Open a valid connection here. I recommend a reusable function or class for opening connections
    Dim rs As ADODB.Recordset
    Set rs = conn.Open("SELECT * FROM dbo.MyView")
    Set Me.Recorset = rs
End Sub


来源:https://stackoverflow.com/questions/49987822/any-way-to-edit-data-on-ms-access-form-using-sql-view-with-two-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!