Results 1 to 4 of 4
  1. #1
    MatthewGrace is offline Competent Performer
    Windows 8 Access 2010 64bit
    Join Date
    Jan 2013
    Posts
    159

    Combobox record lookup and .oldvalue compatibility issue.

    I have attached a super-small file to demonstrate: 1 table and 1 form. Form has a combobox that has VBA code synching the combobox selection with the forms current record like you've seen a great many times:



    Code:
    Private Sub Combo8_AfterUpdate()
    ' Find the record that matches the control.
     With Me.Recordset.Clone
     .FindFirst "[ID] = " & Nz(Me![Combo8], 0)
     If Not .EOF Then Me.Bookmark = .Bookmark
     End With
    End Sub
    That portion works fine, but things get weird when I subsequently change the text value in a field and then use the aforementioned combobox again to jump to another record... Specifically because I setup this code to determine if commit that text change:

    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    If MsgBox("You have made changes.  Commit?", vbQuestion + vbYesNo) = vbNo Then
    txtStuffName.Value = txtStuffName.OldValue
    txtStuffBrand.Value = txtStuffBrand.OldValue
    txtOrderCode.Value = txtOrderCode.OldValue
    End If
    End Sub
    If you choose "vbNo" to execute the .oldvalue stuff, error messages involving CancelUpdate, Update, AddNew, and Edit methods are brought up. I've used them before in different databases, but I can't figure out why Access thinks they should be invoked by the code above. Try running it and see.

    Thank you to anyone who considers helping me.

    Matt
    Attached Files Attached Files

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    I am not getting error when I select No from the confirmation popup. Works just fine for me.

    Wait, the error happens if make selection in combobox while record is still in edit. Instead of OldValue, try Me.Undo.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    If you answer "No", then you need to cancel the update with Cancel = -1 before the endif.

    John

  4. #4
    MatthewGrace is offline Competent Performer
    Windows 8 Access 2010 64bit
    Join Date
    Jan 2013
    Posts
    159
    Thank you June, the code works.
    Maybe this kind of error is very common (no clue). I'll post in case it helps someone. Here's my understanding of it... this code causes a failure later on:

    Code:
    Private Sub Combo8_AfterUpdate()
    ' Find the record that matches the control.
     With Me.Recordset.Clone
     .FindFirst "[ID] = " & Nz(Me![Combo8], 0)
     If Not .EOF Then Me.Bookmark = .Bookmark
     End With
    End Sub
    Sure, it works immediately, but while there's nothing wrong with it, I believe "me.bookmark = .bookmark" programmatically changes the record and fires the BeforeUpdate event (bear in mind, the form is still in .edit mode from typing in textboxes for the original, pre-bookmark-changing record):

    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    If MsgBox("You have made changes.  Commit?", vbQuestion + vbYesNo) = vbNo Then
    txtStuffName.Value = txtStuffName.OldValue ' The error will fire here, because this line not only makes the visible text undo, 
    but also attempts to change the value of the record it's linked to... in this case the record you just bookmarked to, which is 
    not currently in .edit mode.  It's exactly the same error (#3020) as if you tried to say recordset![fieldname] = "text" without
     first doing a recordset.edit
    End If
    End Sub

    In the context of the problem I'm having, you pointed out that the error only occurs IF the forms record selector has a pencil icon indicating you're in .edit mode already for the original row. Someway somehow this jumbling of both the original row being edited AND programmatic changing of the .bookmark before getting a chance to update the changes causes discord.

    You suggested me.undo as an alternative, and this seems to work.

    Thank you,
    Matt

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Subform Compare Value to Oldvalue
    By CementCarver in forum Programming
    Replies: 4
    Last Post: 09-12-2013, 03:19 PM
  2. Replies: 1
    Last Post: 06-24-2013, 05:14 PM
  3. Lookup wizard & add-ins: vba issue
    By rogueknight in forum Access
    Replies: 0
    Last Post: 03-28-2012, 02:56 PM
  4. Autofill / lookup issue
    By bertollini in forum Access
    Replies: 9
    Last Post: 02-24-2012, 06:08 AM
  5. Combobox issue
    By maluna in forum Access
    Replies: 8
    Last Post: 09-08-2010, 01:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums