Steps to Reproduce the Problem
Microsoft
provides programming examples for illustration only, without warranty either
expressed or implied. This includes, but is not limited to, the implied
warranties of merchantability or fitness for a particular purpose. This article
assumes that you are familiar with the programming language that is being
demonstrated and with the tools that are used to create and to debug
procedures. Microsoft support engineers can help explain the functionality of a
particular procedure, but they will not modify these examples to provide added
functionality or construct procedures to meet your specific requirements.
Note The sample code in this article uses Microsoft ActiveX Data
Objects. For this code to run correctly, you must reference the Microsoft
ActiveX Data Objects 2.x Library (where 2.x is 2.1 or later). To do so, click
References on the
Tools menu in the Visual
Basic Editor, and make sure that the
Microsoft ActiveX Data Objects 2.x
Library check box is selected.
- Start Access
2003 or an earlier version of Access.
- Add a new Access project that uses existing data.
You can do this by selecting either Project using existing
data... or Project using new data.... - In the File New Database dialog box, type
Adp1.adp, and then click
Create.
- In the Data Link Properties dialog box on
the Connection tab, move to the Select or enter server
name box. Type or select the name of the instance of SQL Server that
you want to connect to.
- In the Enter information to log on to the
server box, type the username and the
password to connect to the computer that is running
SQL Server.
- In the Select the database on the server
box, type pubs, and then click
OK.
- Create the following stored procedure, and then save the
stored procedure. Use the default name
StoredProcedure1.
Create Procedure "StoredProcedure1"
As
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[authorshistory]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[authorshistory]
CREATE TABLE [dbo].[authorshistory]
(
[ID] [integer] IDENTITY (1, 1) PRIMARY KEY NOT NULL ,
[au_id] [id] NOT NULL ,
[au_lname] [varchar] (40) NOT NULL ,
[au_fname] [varchar] (20) NOT NULL ,
[phone] [char] (12) NOT NULL ,
[address] [varchar] (40) NULL ,
[city] [varchar] (20) NULL ,
[state] [char] (2) NULL ,
[zip] [char] (5) NULL ,
[contract] [bit] NOT NULL
) ON [PRIMARY]
return
- In the Database window, double-click the
StoredProcedure1 stored procedure.
This adds a table
to the database that is named authorshistory.
Note The authorshistory table stores all the old records that are modified by the
frmauthors form. - To add the form that is named frmauthors, follow these
steps:
- In the Database window under the
Objects section, click Forms.
- Double-click Create form by using
wizard.
- In the Form Wizard dialog box under
the Tables/Queries list, select Table:
Authors.
- Click >> to select all fields,
and then click Finish.
- On the View menu, click Design
View.
- Add a command button with the following
properties:
Caption: Show List
Name: cmdOpenAuthorsList - On the File menu, click Save
As.
- In the Save As dialog box in the
Save Form <name> box, type
frmAuthors, and then click
OK.
- On the View menu, click
Code, and then paste the following code in the Microsoft
Visual Basic Editor:
Option Compare Database
Option Explicit
Private Sub cmdOpenAuthorsList_Click()
DoCmd.OpenForm "frmAuthorsList", acFormDS
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim CN As ADODB.Connection
Dim RS As ADODB.Recordset
Dim frmRS As ADODB.Recordset
Dim FLD As Field
Dim strFieldName As String
If Me.Dirty = True Then
Set CN = CurrentProject.Connection
Set RS = New ADODB.Recordset
Set frmRS = Me.Recordset
With RS
.ActiveConnection = CN
.CursorLocation = adUseClient
.CursorType = adOpenDynamic
.LockType = adLockOptimistic
.Source = "AuthorsHistory"
.Open
.AddNew
For Each FLD In frmRS.Fields
strFieldName = FLD.Name
RS(strFieldName) = frmRS(strFieldName).OriginalValue
Next FLD
.Update
End With
RS.Close
Set RS = Nothing
Set frmRS = Nothing
Set CN = Nothing
End If
End Sub
Private Sub Form_Load()
Set MyRS = New ADODB.Recordset
MyRS.Open "Select * FROM Authors", CurrentProject.Connection, adOpenDynamic, adLockOptimistic, adCmdText
Set Me.Recordset = MyRS
End Sub
- On the File menu, click Close
and return to Microsoft Access.
- Close the frmauthors form.
- To create another form that is named frmAuthorsList, follow these steps:
- In the Database window under
Objects, click Forms.
- Double-click Create form by using
wizard.
- In the Form Wizard dialog box under
the Tables/Queries list, select Table:
Authors.
- Select the following fields in the
Authors table:
au_id
au_lname
au_fname
Click Finish. - On the View menu, click Design
View.
- On the View menu, click
Properties, and then set the following
properties:
Caption: Authors List
Default View: Datasheet
- Save the form as
frmAuthorsList.
- On the View menu, click
Code, and then paste the following code in the Visual Basic
Editor:
Option Compare Database
Option Explicit
Private Sub Form_Load()
Set Me.Recordset = MyRS
End Sub
- On the File menu, click Close
and return to Microsoft Access.
- Close the frmauthorslist form.
- Add a module, and then declare MyRS as a global variable. To do this, follow these steps:
- In the Database window under
Objects, click Module.
- On the Insert menu, click
Module.
- Add the following code to the Visual Basic Editor:
Option Explicit
Public MyRS As ADODB.Recordset
- On the File menu, click Save
Adp1.
- On the File menu, click Close
and return to Microsoft Access.
- Open the frmAuthors form. To do this, follow these steps:
- In the Database window under
Objects, click Forms.
- In the right pane, double-click
frmauthors.
- Click Show List on the form to open
frmAuthorsList in Datasheet view.
- In the frmAuthors form, modify the last
name in the first record.
- Select the second record in the
frmAuthorsList.
- Open the authorshistory table. To do this,
follow these steps:
- In the Database window under
Objects, click Tables.
- In the right pane, double-click
authorshistory.
Note Instead of saving the modified record in the
authorshistory table as you expect, the code in the BeforeUpdate event procedure saves the other record that you selected in the
second form.