Solved

Cancel a combobox change in MS Acces

Posted on 2013-11-29
2
423 Views
Last Modified: 2013-12-03
I have an Access database (attached) with a form (Form1) containing a combobox and a subform.

The row source of the combobox is a table containing a list of state abbreviations.

The record source of the subform is a query that retrieves records from a table (Table1).

The table contains monetary data related to states.

In that table, multiple records pertaining to a particular state may appear.

Just for illustration, I loaded the table with records from a handful of states (AL, CA, NY,TX).

When you open the form, select NY from the combobox and you will see the subform is populated with “NY” records.

Once you do that, then select a different state from the combobox. You will see a messagebox appear.

Select cancel.  You’ll note that ‘NY’ records still appear in the subform (as they should) but the combobox now displays the different value you selected.

What I’m trying to accomplish:

When Cancel is selected from the messagebox (which SHOULD be serving the purpose of canceling a change to the record(s) displayed in the subform), I want the combobox to revert back to the value before the change (I want it to match the state value in the subform).  In this case it would be NY.  What’s happening now is that the cancel command is properly canceling a change to the subform data. However, the combobox value still changes, so I’m winding up with NY records displayed in the subform yet the combobox displays a different value (whatever different state I may have selected).

When I select Cancel from the messagebox (which cancels a change to the records displayed in the subform), I want the combobox to display the same state as displayed in the subform.

Thank you.
Database3.accdb
0
Comment
Question by:dbfromnewjersey
2 Comments
 
LVL 39

Assisted Solution

by:als315
als315 earned 250 total points
ID: 39685781
May be there is more elegant solution, but you can simply add line after
Cancel=True
SendKeys ("{ESC}")
0
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 250 total points
ID: 39685852
There are some things I prefer to use the AfterUpdate event for.  Since you are using an unbound form and control (combo), none of the Undo or Cancel events are really going to have an effect on your combo box.  I would recommend the following code, or something like it.
Private Sub cmb_State_AfterUpdate()

    Dim strMsg As String
    Dim intResponse As Integer

    'By defining strPrevState as static, it will retain its value between 
    'calls to this subroutine.  But will start out as an empty string
    Static strPrevState As String
    
    'Compare the current and previous values of the combo box.  Only do 
    'something if the values have changed
    If Me.cmb_State <> strPrevState Then
    
   'If there was no previous value, then automatically accept the change
   'Otherwise request confirmation of change   
        If strPrevState = "" Then
            intResponse = vbOK
        Else
            strMsg = "You have selected a different state than the one " _
                  & "currently displayed in the datasheet.  Data pertaining " _
                  & "to only one state may appear in the datasheet at any " _
                  & "given time.  Are you sure you want to switch states?"
            intResponse = MsgBox(strMsg, vbCritical + vbOKCancel)
        End If
        
        'If user elected to change then requery the form and store the 
        'current value of the combo in the variable strPrevState
        'If not, set the value of the combo back to its previous value
        If intResponse = vbOK Then
            Me.Table1_subform.Form.Requery
            strPrevState = Me.cmb_State
        Else
            Me.cmb_State = strPrevState
        End If
    
    End If

End Sub

Open in new window


with this code, you should delete the code for the combo_Change and combo_BeforeUpdate event
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now