Solved

AddNew to Dynaset in VB5

Posted on 1998-07-08
5
343 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
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now