Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set a combobox to a previous value

Status
Not open for further replies.

EwS

Programmer
Dec 30, 2002
398
US
I have a combobox with two items: ABC and XYZ. If my current selection is "ABC" and the user switches to "XYZ", in myComboBox_Click event I ask, "Do you want to save the changes? - Yes, No, Cancel" (I want to display the prompt AFTER a selection has been made).

When Cancel is clicked, I'm trying to set the combobox to "ABC", but it doesn't work. I guess I shouldn't be doing this in the Click event, where then? When I tried coding this in myComboBox_Change, my code was not even fired.

Thanks.
 
Have you tried storing the selection or its ListIndes in a module variable from the GotFocus event before it's changed and the Click event fires?

Paul Bent
Northwind IT Systems
 
The combo box .Tag property is an even better place to store this, if you aren't using it for somethig else.

Robert
 
Try this:

Option Explicit

Private m_ComboSelection As String
Private m_OldValue As String

Private Sub cboSelect_Click()
Dim iRet As Integer

iRet = MsgBox("Do you want to save", vbYesNo + vbQuestion)
Select Case iRet
Case vbYes
'Save selection
m_ComboSelection = cboSelect.Text
m_OldValue = m_ComboSelection

Case vbNo
'Replace with old value
m_ComboSelection = m_OldValue
cboSelect.Text = m_ComboSelection
m_OldValue = ""
End Select
End Sub
 
paulbent - yes, I did. It doesn't work.

TheVampire - I'm not using .Tag for anything else, but it doesn't work either.

Kenjan - your code looks very similar to mine, replacing the current selection with the old value works for text boxes, but not the combobox.

It just doesn't work.
 
both paulbent and TheVampire have together provided a good answer. The key is to use the GotFocus event to capture the original value. It will be too late the get the original value once the click event has been fired.
Code:
Sub cboSelect_GotFocus()
   cboSelect.Tag = cboSelect.Text
End Sub
Then inside your click event
Code:
Private Sub cboSelect_Click()

   Select Case (MsgBox("Do you want to save", vbYesNo + vbQuestion))
      Case vbYes
         ' Don't need to do anyting
      Case Case vbNo
         'Replace with old value
         cboSelect.Text = cboSelect.Tag
    End Select
End Sub
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
CajunCenturion -
Actually, the original value is stored in a global variable, I checked with a msgbox that the value is correct. It is, I set the combobox to it, but it doesn't seem to work.
 
One more thing...When I use the .Tag property, the combobox is set to blank, but when I click on the dropdown, I see "ABC", "XYZ".
 
Perhaps you can post all of the relavent code Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
In the GotFocus event I verified that gstrCompany has proper value. In the Click event I verified that cboCompany.Tag has proper value. Even hard-coding the value didn't work.

Private Sub cboCompany_GotFocus()
cboCompany.Tag = gstrCompany
End Sub


Private Sub cboCompany_Click()
If gblnRecChanged Then
Dim sanswer As Long
sanswer = MsgBox("Do you want to save changes?", vbYesNoCancel)
If sanswer = vbYes Then
Call Accept_Click
ElseIf sanswer = vbCancel Then
frmEntryList.cboCompany.text = cboCompany.Tag
End If
End If
End Sub
 
Use the DropDown event to set the tag, like this.

Private Sub cboCompany_DropDown()
cboCompany.Tag = cboCompany.Text
End Sub

The reason not to use got focus is that if they change the box twice without it losing focus, that the Tag value will not get reset.

The rest of the code works OK.

Robert
 
I think you might try setting the value of the global variable (or the .Tag) in the Got Focus event. It is in the GotFocus event that you want to capture the original value. You don't need both the Global variable and the .Tag property. Either one will work as the placeholder for the original value.
Code:
Private Sub cboCompany_GotFocus()
' If you're using a global variable
    gstrCompany = cboCompany.Text
' if You're using the .Tag property
    cboCompany.Tag = cboCompany.Text
End Sub
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
TheVampire -
When I use the DropDown event, it still sets the combobox to a blank.

CajunCenturion -
Yes, either one will work, I was playing with my code and I forgot to replace gstrCompany with cboCompany.Text.

I got a suggestion in another forum and this code does set the combobox to its previous value, however there is one PROBLEM: the validate event does not occur until I click in another field on the form. All I'm using here is the Validate event with CausesValidation property of cboCompany set to True (I'm not using GotFocus nor DropDown).

Private Sub cboCompany_Validate(Cancel As Boolean)
If gblnRecChanged Then
Dim sanswer As Long
sanswer = MsgBox("Do you want to save changes?", vbYesNoCancel)
If sanswer = vbYes Then
Call Accept_Click
ElseIf sanswer = vbCancel Then
frmEntryList.cboCompany.text = gstrCompany
End If
End If

gstrCompany = frmEntryList.cboCompany.text
End Sub

How can I force the Validate event to fire programmatically? I tried setting focus (SetFocus) to another control and then back to cboCompany, it doesn't work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top