?
Solved

Cancel a combobox change in MS Acces

Posted on 2013-11-29
2
Medium Priority
?
433 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 1000 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 earned 1000 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
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.
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 “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Suggested Courses

770 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