Solved

Cancel a combobox change in MS Acces

Posted on 2013-11-29
2
428 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 40

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Familiarize people with the process of utilizing SQL Server functions 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 Microsoft Ac…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

740 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