So it would be something like the following:
Option Explicit
Private StoredValue() As Double
Dim ErrorFound As Boolean 'Added this line ##########################
Private CodeMode As Boolean 'to cancel events when they are
' triggered from my code and not
' from the user
'Filling the listbox first
Private Sub UserForm_Initialize()
Dim i As Long, ttl As Long
CodeMode = True 'prevent events
With lbx
.AddItem "aaa"
.AddItem "bbb"
End With
ttl = lbx.ListCount
ReDim StoredValue(0 To ttl - 1)
For i = 0 To ttl - 1: StoredValue(i) = i: Next
CodeMode = False
End Sub
'When listbox is clicked, show the corresponding
' value in the textbox txt
Private Sub lbx_Click()
'Cancel event if CodeMode
If CodeMode Then Exit Sub
txt = StoredValue(lbx.ListIndex)
End Sub
'Now the user can edit the textbox
'Validation: check data when the usere leaves the textbox
' it has to be a number else prevent the update
Private Sub txt_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Dim d As Double
CodeMode = True 'cancel events
'Validation
On Error Resume Next
d = CDbl(txt)
If Err <> 0 Then
MsgBox "you must enter a number"
Cancel = True 'Cancel the update of the textbox
ErrorFound = True 'Added this line #################
Else
StoredValue(lbx.ListIndex)
ErrorFound = False 'Added this line #################
End If
CodeMode = False
End Sub
'Added this event to check if an error was found with the user's entry
Private Sub lbx_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If ErrorFound Then Cancel = True
End Sub
Main Topics
Browse All Topics





by: tony_813Posted on 2005-09-14 at 20:01:19ID: 14886527
Hello,
= CDbl(txt)
you may add the following:
If Err <> 0 Then
MsgBox "you must enter a number"
Cancel = True 'Cancel the update of the textbox
ErrorFound = True 'Added this line
Else
StoredValue(lbx.ListIndex)
ErrorFound = False 'Added this line
End If
'Added a before update to the lbx
Private Sub lbx_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If ErrorFound Then Cancel = True
End Sub
And Declared ErrorFound As Boolean on the top
tony_813