Solved

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

Posted on 2006-10-30
11
2,019 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now