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
Solved

Cancel a combobox change in MS Acces

Posted on 2013-11-29
2
427 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 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
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 …

808 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