Improve company productivity with a Business Account.Sign Up

x
?
Solved

AddNew to Dynaset in VB5

Posted on 1998-07-08
5
Medium Priority
?
396 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 100 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

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
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…

606 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