[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 562
  • Last Modified:

revert unbound combobox to original data using BeforeUpdate (A97)

i have an unbound combo box on an unbound form.
on the BeforeUpdate event i want the user to confirm the data change using a messagebox
if the user selects 'No', then i want the combo box to revert back to it's original data.

normally i would do this using:
Cancel=true
ctl.undo
(see below for full code)

however, this doesn't make any difference - the data in the combobox is still the new data.
(NOTE: pressing ESC twice doesn't revert back to original data either)

Question: How can i revert back to the original data in an unbound combobox, on an unbound form, using BeforeUpdate?

cheers.


--------------------------------------------------------------------------
Private Sub cbo_customer_id_BeforeUpdate(Cancel As Integer)

    If MsgBox("Are you sure you want to change the Customer on this Rental Order?", vbQuestion + vbYesNo, "Change Customer") = vbNo Then
        Cancel = True
        cbo_customer_id.Undo
     End If
   
End Sub
--------------------------------------------------------------------------
0
CLL
Asked:
CLL
  • 2
1 Solution
 
Jeffrey CoachmanCommented:
CLL,

The problem is that since the ComboBox is unbound, the value does not exist until "After" it is updated.

You could do the Reversion "After Update", (The code is a lot more complex, but I tried it and it works)

1. Set the Default Value of "cbo_customer_id" to:   0

(Note that I used a Module Variable in the code to store the "Original" value of the ComboBox)

Here is the entire code:

Option Compare Database
Option Explicit
Private lngNowValue As Long  'Module Variable, stores the Original Value of the CustID ComboBox

Private Sub cbo_customer_id_AfterUpdate()
   
    'If the ComboBox does not match the Module Variable
    If Me.cbo_customer_id <> lngNowValue Then
        'Ask if change should be made
        'If No, then set the Customer ID ComboBox
        ' back to it's original Value
        If MsgBox("Are you sure you want to change the Customer on this Rental Order?" _
        , vbQuestion + vbYesNo, "Change Customer") = vbNo Then
            Me.cbo_customer_id = lngNowValue
        'If Yes, then set the Module Variable to this new Customer ID
        Else
            lngNowValue = Me.cbo_customer_id
        End If
    End If

End Sub

Private Sub cbo_customer_id_BeforeUpdate(Cancel As Integer)
'Prevent user form leaving the Customer ID ComboBox blank
   
    If IsNull(Me.cbo_customer_id) Then
        MsgBox "You cannot leave the Customer ID Field blank"
        Cancel = True
    End If

End Sub

Private Sub cbo_customer_id_GotFocus()
' Sets the Module Variable to the value of the Customer ID Combobox
   
    lngNowValue = Me.cbo_customer_id
End Sub
0
 
CLLAuthor Commented:
wonderful
thats works sweet putting the code into the AfterUpdate event.

i also put the following code:

If IsNull(Me.cbo_customer_id) Then
        MsgBox "You cannot leave the Customer ID Field blank"
        me.cbo_customer_id = lngNowValue
        me.cbo_customer_id.SetFocus
End If

into the AfterUpdate event so that i could revert back to the original value if the user had deleted it.

thanks for working that out for me.
great solution.

cheers.
0
 
Jeffrey CoachmanCommented:
CLL,

Glad I could help!
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.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now