Solved

Cancel a combobox change in MS Acces

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

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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