Solved

AddNew to Dynaset in VB5

Posted on 1998-07-08
5
367 Views
Last Modified: 2012-05-04
I'm trying to open a recordset as a dynaset using a select statement.  If the select does not find the record in the where clause than it does an AddNew to the recordset.  This works fine on the first open but the next attempt results in an error 3426 - This action was cancelled by an associated object.  Am I using the correct process?  Is there a better solution?  I also need to set the primary keys.  After AddNew, the user must enter data, then I need to user lastmodified to update the rec keys before updating.  Any and all help would be appreciated.  Thanks

'***  Here is the script that you requested ***'

Private Sub OpenPDate()
   
    On Error GoTo Requester_Err:
     
    Dim strparm         As String
    Dim myset           As Recordset
   
    Me.MousePointer = 11    

    strparm = _
        "SELECT project_recno, begin_date, select_site_date, lease_date, reg_date, " & _
            "developed_date, award_date, estimated_end_date, actual_end_date " & _
            "From project_dates " & _
            "WHERE (project_recno = " & Str(gs_project_rec) & " );"
   
    Set myset = MyDb.OpenRecordset(strparm, dbOpenDynaset)
    If myset.RecordCount > 0 Then
        Data11.RecordSource = strparm
        Data11.Refresh
    Else
        Call cmdAdd_Click
    End If
    myset.Close
   
    me.MousePointer = 0    ' Change mouse pointer to normal.
    Exit Sub
end sub
   

Private Sub cmdAdd_Click()
    On Error GoTo Requester_Err:
   
    Me.MousePointer = 11    ' hourglass

    Select Case SSTab1.Tab
       
        Case 2
       
            Data11.Recordset.AddNew
end sub
 
         
Private Sub Data11_Validate(Action As Integer, Save As Integer)
    On Error GoTo Requester_Err:
   
    Select Case Action
       
        Case vbDataActionAddNew
         
            If Data11.Recordset.Bookmarkable = True Then
                Data11.Recordset.Bookmark =                 Data11.Recordset.LastModified
            End If
end sub


'***  wait for user input ***

Private Sub cmdUpdate_Click()
    On Error GoTo Requester_Err
   
    Dim wvalue          As Variant
    Dim retval          As Variant
    Dim typeid          As Long
 
    Me.MousePointer = 11   'hourglass
 
    Select Case SSTab1.Tab
     
        Case 2
               
            Data11.Recordset.MoveLast
            If Data11.Recordset.Fields("project_recno") = 0 Or _
                IsNull(Data11.Recordset.Fields("project_recno"))                 Then
                Data11.Recordset.Fields("project_recno") =                 gs_project_rec
                Call CheckDates
                Data11.Recordset.Fields("date_updated") = Date
                Data11.Recordset.Update
            Else
                Data11.Recordset.Update
            End If
            Data11.Recordset.Bookmark =                          Data11.Recordset.LastModified
end if

Sorry this is so long; I got everything but problem.  Thanks in advance for your help.        
       



0
Comment
Question by:NJMet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 4

Expert Comment

by:tomook
ID: 1465250
From your description, it appears you are doing the following steps:
1. Open a recordset with a specific WHERE clause, ex. "SELECT Field1 FROM Table1 WHERE Field1=1"
2. If no record found, add a record, ex.
 rs.AddNew
 rs.Field1=1
 rs.Update
3. Adding another record, ex.
 rs.AddNew
 rs.Field1 = 2
 rs.Update

If so, the WHERE clause in the original SELECT statement is your problem. You may need to change you logic to
1. (No change)
2. If no record found, add a record.
2a. Open a new recordset
3. Add a new record.
0
 
LVL 1

Expert Comment

by:shagnasty
ID: 1465251
Post the SQL statement you are using to open the recordset...
0
 
LVL 5

Expert Comment

by:yronnen
ID: 1465252
Are you using a Data control?
0
 

Author Comment

by:NJMet
ID: 1465253
Edited text of question
0
 
LVL 1

Accepted Solution

by:
shagnasty earned 50 total points
ID: 1465254
You need to add the following lines:

---Snip---
Set myset = MyDb.OpenRecordset(strparm, dbOpenDynaset)

myset.movelast
myset.movefirst  'Record count need to load the whole recordset to work!

If myset.RecordCount > 0 Then
---Snip---


0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question