Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9002993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:39:06+00:00 2026-06-16T00:39:06+00:00

I am doing some maintenance work on a linked-table application in Microsoft Access 2010

  • 0

I am doing some maintenance work on a linked-table application in Microsoft Access 2010 and experiencing this little gem of a problem. The application is linked to a SQL Server 2008 database. The application has a main form that allows a user to choose a combination of park code and resource and pops up an edit form for the details of that particular combination. If that combo doesn’t exist in the database, the application inserts a new record in, but the issue is that 2 records get inserted.

Here’s the seat of the problem code, it gets called when I need to insert a new record in a details popup form:

Private Sub New_Rec(unit_code As String, resource As String, sql As String)

   DoCmd.RunSQL ("INSERT INTO PARK_RESOURCES (unit_code, resource, sensitivity) VALUES " _
    & "('" & unit_code & "','" & resource & "','public')")

   'Force an explicit save
   'http://www.pcreview.co.uk/forums/update-cancelupdate-without-addnew-edit-t1150554.html
   If Me.Dirty Then
      Me.Dirty = False
   End If

   Me.RecordSource = sql
End Sub

Creating a “new” record results in 2 records getting inserted into the Recordset. It doesn’t seem to matter if I move the explicit save code before or after setting the RecordSource. In either order (and stopping after either) produces 2 new records inserted in the database (verified by querying in SSMS).

When I set the RecordSource property and step through the code, the event chain looks like: Me.RecordSource = sql –> Form_BeforeUpdate() –> Form_AfterUpdate() –> Form_After_Insert() –> Form_Current(). The duplicate is not present at the close of BeforeUpdate, but by the time I get to AfterUpdate, the duplicate has already been inserted. What happens between BeforeUpdate and AfterUpdate that causes this to happen?

According to MSDN, the order is: BeforeInsert → BeforeUpdate → AfterUpdate → AfterInsert. They also state that setting the value of a control through Visual Basic doesn’t trigger these events. But when I update the RecordSource in code, the last 3 events certainly fire; BeforeInsert is the only one that a step-through doesn’t stop on.

As per Daniel Cook’s request, here is the calling code.

Private Sub Form_Load()
   On Error GoTo Err_Form_Load

   Me.KeyPreview = True
   If Not IsNull(Me.OpenArgs) Then
      ProcessOpenArgs (Me.OpenArgs)
      Me.lblHeader.Caption = Me.unit_code & ": Resource - " & Me.resource
   Else
      Me.lblHeader.Caption = "Information Needs"
   End If
   ... (error trapping)
End Sub

And the ProcessOpenArgs sub (OpenArgs get set as “park;resource”):

Private Sub ProcessOpenArgs(open_args As String)
   On Error GoTo Err_ProcessOpenArgs

   Dim Args() As String
   Args = Split(open_args, ";")

   Me.unit_code = Args(0)
   Me.resource = Args(1)

   'Check to see if there are records in the database for current unit/resource combo'
   Dim rs As DAO.Recordset
   Dim sql As String
   sql = "SELECT * FROM PARK_RESOURCES " & _
    "WHERE resource='" & Me.resource & "' AND unit_code='" & Me.unit_code & "'"
   Set rs = CurrentDb.OpenRecordset(sql, dbOpenDynaset, dbSeeChanges)

   'if there aren''t, create a new record'
   If (rs.RecordCount = 0) Then
      New_Rec Me.unit_code, Me.resource, sql
   Else 'go to the current listing'
      Me.RecordSource = sql
   End If

Exit_ProcessOpenArgs:
   Exit Sub
Err_ProcessOpenArgs:
   MsgBox Err.Number & Err.description
   Resume Exit_ProcessOpenArgs
End Sub

I will continue to comb through the event documentation and as a last resort I may go totally nuts and just stick every possible event in my VBA code and step through them, but does anyone know what could be happening to cause the duplicates?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:39:07+00:00Added an answer on June 16, 2026 at 12:39 am

    When I’m setting Me.unit_code and Me.unit here:

    Private Sub ProcessOpenArgs(open_args As String)
       On Error GoTo Err_ProcessOpenArgs
    
       Dim Args() As String
       Args = Split(open_args, ";")
    
       Me.unit_code = Args(0)
       Me.resource = Args(1)
    

    the code is creating 1 record and then New_Rec inserts a second record in the DB. When the Form automatically Requeries after Me.RecordSource = sql, it sticks the first record (created by the Me.xxx = yyyy statements in ProcessOpenArgs into the DB too and then pulls both back out to the Form Recordset. That’s where the double insert is coming from.

    In order to correct it, I changed Me.unit_code and Me.resource to local subroutine variables l_unit_code and l_resource and used those instead in ProcessOpenArgs. That took care of this problem as well as a second problem that I had with records form one resource type bleeding into other resource types.

    Thanks all for the assist!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing some code maintenance at work, and I ran across this little snippet...
I am doing some maintenance work for a postgres database, and one particular table
I'm doing some maintenance work on a database application and I've discovered that, joy
I am doing some maintenance work on an older system that is running PHP
I'm doing maintenance work on an existing Rails site and am having some problems
I am doing some maintenance on a VB6 Windows application. I have a .Net
I'm doing some maintenance work and ran across something like the following: std::string s;
I am doing some maintenance work to a fairly large existing site written in
I'm doing some maintenance work on one of our old applications that is written
I've been working writing fairly small applications and doing some maintenance work for some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.