Results 1 to 6 of 6

Thread: AddNew, associated object error. Please help!

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Posts
    103

    Unhappy AddNew, associated object error. Please help!

    Hello,

    Got a problem with my program. This is the code for adding data into the database (Access 97):

    Private Sub cmdAdd_Click()
    'set button states
    cmdSave.Enabled = True
    cmdAdd.Enabled = False
    cmdEdit.Enabled = False
    'Unlock textboxes so changes can be made
    Call UnlockTextBoxes
    'Clear all data from textboxes
    Call ClearTextBoxes
    'Go into Add New Records mode
    Data1.Recordset.AddNew
    End Sub

    Whenever I click on it, an error appears saying: "This action was cancelled by an associated object", with the Data1.Recordset.AddNew line highlighted. What's wrong?

    BTW, this is how the table in the database linked to the program looks like:

    SHRUB_ID (AutoNumber)
    SHRUB_DESCR (Text, 25 char)
    REQ_TEMP (Integer)
    GREENHOUSE_NBR (Integer)

    Is the AutoNumber field somehow bound to the program and causing the error when .AddNew is run?

    Thanks!

  2. #2
    Member
    Join Date
    Nov 2002
    Location
    England
    Posts
    34
    Hi quipy,

    Have a look at this MS Knowledge Base Topic

    This is a paragraph from that Topic:
    This error is being generated because the AddNew command of a bound recordset
    causes Visual Basic to try to save the current record if the data has changed.
    Because the data control is currently pointing to a NULL record and not an empty
    record, the data cannot be saved so the "action was cancelled by an associated
    object" error is reported. This is commonly seen when using the data form wizard
    in Visual Basic versions 4.0 and 5.0. The data form wizard in Visual Basic 6.0
    generates code for the ADO data control so this error is less likely to occur.
    I think your line "Call ClearTextBoxes" is causing the problem.

    Hope it helps.

    TC

  3. #3
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    You have to be sure when an addnew action is called there isn't another action still pending like addnew or edit.


    Be sure you have a .update somewhere in your code to write the added or edited record to the db.

    You can also call a cancelupdate if there is a action pending.

    Try it first with putting the cancelupdate statement before the addnew and look if you still get an error.
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Posts
    103
    Thanks for your replies:

    swatty
    i tried to call a "cancelupdate" before the "addnew" but it said something like "cancelupdate without edit/update" so there isn't another action pending

    TC99
    I tried removing ClearTextBoxes completed from code but the error's still there....

  5. #5
    Member
    Join Date
    Nov 2002
    Location
    England
    Posts
    34
    Hi quipy,

    It could still be worthwhile looking at the MS Kowledge base article anyway. There is some code in the article which would be worth trying on your cmdAdd button.

    If that still doesn't help then can you let me know a few other details: Are you using Bound controls? What type of data object is Data1? Does it happen every time, even if there is nothing in the three controls on your form?

    TC

  6. #6
    Member
    Join Date
    Feb 2002
    Posts
    43
    It looks like you are clearing the textboxes before you add the actual record. Try commenting out the call clear textboxes and see what happens.

    It also seems like you are disabling the cmdAdd button as soon as it is clicked.

    If the SHRUB_ID field is an auto number, it will be set when the new record is added, the code should't try to change that field.

    Just guessing here.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width