Solved

AddNew to Dynaset in VB5

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

856 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