Solved

How do you trap a combo box index change and cancel it if required

Posted on 2006-10-30
11
2,023 Views
Last Modified: 2013-12-25
Hi,

I need to stop and ask the user if they really want to change the selected index in a combo box whenever there are changes to the data on the form.  If the user cancels the index change then I need to set the index back to the original value and prevent the values in any other controls dependent on the combo box selection from being changed.  I got it working but its very convoluted and I figured there must be an easier way.

Thanks
0
Comment
Question by:ahillier
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 17837108
Use the combo box's BeforeUpdate to read the value, and based on whatever logic you choose function accordingly...

Private Sub YourComboBox_BeforeUpdate(Cancel as Integer)

Select Case Me.YourComboBox.Value
   Case "Banana"
        if msgBox("Are you really sure you want to change this to banana?", vbYesNo, "?") = vbNo then
              'User hit 'No button.  Bail.
              Cancel = True
              Docmd.CancelEvent
              exit sub
       else
             'User hit 'Yes' button. ok.
       end if
   Case "Orange"
       'Do Orange stuff here
End Select

End Sub
0
 

Author Comment

by:ahillier
ID: 17837166
Am I missing something?  There is no method "BeforeUpdate" in the list of available methods for a combo box.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 17837294
Please disregard my comment.  I gave you an Access answer, and didn't notice that you asked the question in the VB Controls TA.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17839412
Is this less convoluted that what you have?

Option Explicit

Private lastIndex As Integer
Private dirty As Boolean

Private Sub Form_Load()
    lastIndex = -1
    dirty = True ' set this when your "record" has been changed
End Sub

Private Sub Combo1_Click()
    Dim curIndex As Integer
    curIndex = Combo1.ListIndex
    If lastIndex <> -1 And curIndex <> lastIndex Then
        If dirty Then
            If MsgBox("Change without saving?", vbYesNo, "Changes will be lost") = vbNo Then
                Combo1.ListIndex = lastIndex
                Exit Sub
            End If
        End If
    End If
   
    If curIndex <> lastIndex Then
        lastIndex = curIndex
        Debug.Print "Index Changed: " & curIndex
    End If
End Sub
0
 
LVL 14

Accepted Solution

by:
Shiju Sasidharan earned 125 total points
ID: 17839470
'Change combo box style to drop down
'---------------------------------------------------

Dim iComboIndex As Integer
Private Sub Combo1_Click()
    If Not Combo1.Visible Then
        iComboIndex = Combo1.ListIndex
        Exit Sub 'No message when form is loading
    End If
   
    If iComboIndex <> Combo1.ListIndex Then
        If MsgBox("Do you want to change item to " & Combo1.Text, vbQuestion + vbYesNo) = vbYes Then
            iComboIndex = Combo1.ListIndex
        Else
            Combo1.ListIndex = iComboIndex
        End If
    End If
End Sub
Private Sub Form_Load()
Dim i As Integer
    For i = 1 To 10
        Combo1.AddItem "Item" & i
    Next
    Combo1.ListIndex = 0
End Sub
0
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 17839480
;-)  Little bit late

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17842268
*sigh*...
0
 

Author Comment

by:ahillier
ID: 17842330
sorry - next time I'll pay more attention to who answered first
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17842741
=)
0
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 17847313
Actually Idle_Mind deserved the grade
he made the move first

0
 

Author Comment

by:ahillier
ID: 17849170
I already appologized to him :(
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

856 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