I have only been doing this for a few months, but I have similar code
that may do the job for you...
I use 1 button instead of 2, I change the button caption depending on
what the user is doing and then on button press doing the action the
caption says it is going to do.
My application is more of a form administration so I don't have to be
around for someone to keep the names of staff members up to date.
The form includes a listbox, a textbox and a command button. When the
form loads, the list box has no selection, the textbox has no text and
the button is disabled.
If the user makes a selection on teh list box, the button caption
changes to 'Remove Name' and removes any text from the textbox.
If the focus is shifted to the textbox, the button caption changes to
'Add Name' and any selection on the list box is removed.
When the button is pressed it checks the value of the buttons caption
to decide what it should do.
Code is below:
'#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#
' listbox functions
Private Sub lstManager_Enter()
'this will run each time the list box is entered
'check to see if there is any text in the manager text box
If Not Me.txtManager.Value = "" Then Me.txtManager.Value = ""
Me.btnAddRemoveManger.Enabled = False
Me.btnAddRemoveManger.Caption = "Add/Remove Manager"
End Sub
---------------------------------------------------------------------------------------------------------------------------------
Private Sub lstManager_Change()
'this runs each time the listbox changes selection
'change the caption of the button
Me.btnAddRemoveManger.Caption = "Remove Manager"
'enable the button
Me.btnAddRemoveManger.Enabled = True
End Sub
--------------------------------------------------------------------------------------------------------------------------
Private Sub txtManager_Enter()
'this will run each time the text box is entered
'check to see if there is a selection in the list box
If Me.lstManager.ListIndex > -1 Then Me.lstManager.ListIndex = -1
Me.btnAddRemoveManger.Enabled = False
Me.btnAddRemoveManger.Caption = "Add/Remove Manager"
End Sub
-----------------------------------------------------------------------------------------------------------------
Private Sub txtManager_Change()
'this runs each time the listbox changes selection
'change the caption of the button
Me.btnAddRemoveManger.Caption = "Add Manager"
'enable the button
Me.btnAddRemoveManger.Enabled = True
End Sub
-----------------------------------------------------------------------------------------------------------------
'@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@
' Button Code Below
Private Sub btnAddRemoveManger_Click()
'check to see if we have anything to do. no selection in the list or
no text in box
If Me.lstManager.ListIndex = -1 And Me.txtManager.Value = "" Then
MsgBox "You don't have a manager selected in the list and you
haven't entered a manager name in the textbox.", vbInformation, "I have
nothing to do?!?"
GoTo LastLine
End If
'check to see if we are adding names or removing names
If Me.btnAddRemoveManger.Caption = "Remove Manager" Then
'looks like we are removing a name
'ensure the list contains items
If Me.lstManager.ListCount < 1 Then
'alert the user there is nothing to delete
MsgBox "You don't any Managers to delete, Sorry bro!",
vbExclamation, "No Managers"
Else
If Me.lstManager.ListIndex = -1 Then
MsgBox "You don't have a Manager selected, please make
a selection and try again.", _
vbExclamation, "Make a Selection"
Else
Dim yesno As Byte
yesno = MsgBox("You are about to delete " &
Me.lstManager.Value & " from the Manager list." & vbCrLf & _
"Please be sure this is what you want to do.",
vbYesNo + vbExclamation, "Are you sure?")
Select Case yesno
Case vbYes
'find and remove the item from the list variables
page
Dim MgrRng As Range
Dim Mgr As Range
Set MgrRng = shtVar.Range("F4:F18")
For Each Mgr In MgrRng
If Mgr.Value = Me.lstManager.Value Then
Mgr.Value = ""
Exit For
End If
Next Mgr
'remove the item from the list box
Me.lstManager.RemoveItem (Me.lstManager.ListIndex)
'clear the selection (if any)
Me.lstManager.ListIndex = -1
'disable the button
Me.btnAddRemoveManger.Enabled = False
'rename the button
Me.btnAddRemoveManger.Caption = "Add/Remove
Manager"
Case vbNo
End Select
End If
End If
Else
'adding a name
Set MgrRng = shtVar.Range("F4:F18")
'find the first empty box in the range and insert the new value
For Each Mgr In MgrRng
If IsEmpty(Mgr.Value) Then
Mgr.Value = txtManager.Value
Exit For
End If
Next Mgr
'select the range to sort
shtVar.Range("F4:F18").Select
selection.Sort Key1:=Range("F4"), Order1:=xlAscending,
Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False,
Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
'clear the current list
Me.lstManager.Clear
'rebuild the MGR listbox
For Each Mgr In MgrRng
If Not IsEmpty(Mgr.Value) Then
Debug.Print Mgr.Value
lstManager.AddItem (Mgr.Value)
End If
Next Mgr
'clear the text box
txtManager.Value = ""
'disable the button
Me.btnAddRemoveManger.Enabled = False
'rename the button
Me.btnAddRemoveManger.Caption = "Add/Remove Manager"
End If
LastLine:
End Sub
'@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@
' END OF CODE
'@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@
If anything else, I hope this gives you some ideas.
theSquirrel