LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 694
Default Userform: listbox and controls' data entry validation

Hi ,
I have an issue with userform and validation of data (not on an excel sheet
here)
On the userform, Userform1, I have:
- a listbox called lbx
- a textbox called txt
- a command button called cmd
I fill the listbox during _Initialize and check the data in
txt_BeforeUpdate(Cancel) and cancels the update if not valid data.
Now, if the data is not valid, i warn the user with a message and i'd like
to give him a chance to change the entered data.
When running, say row 1 of listbox is selected. If you enter wrong data and
click another item in the listbox, row2, the message is displayed, but the
listbox updates to the new selection preventing the user to re-entrer the
data for his previous selection (row1). I have posted my code below.

I have tried the txt_Exit event too, but to no avail.
My real case scenario consists in many dataentry control on the side of the
listbox, so i cannot let the user loose all the data he has just entered.

Any idea would be greatly appreciated.

Thanks,
Sebastien

Here is the code
'-------------------------------------------------------------------------------
Option Explicit

Private StoredValue() As Double

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
Else
StoredValue(lbx.ListIndex) = CDbl(txt)
End If

CodeMode = False
End Sub
'----------------------------------------------------------------
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Removing Data Validation (Listbox) NoodNutt Excel Worksheet Functions 6 March 8th 08 12:35 PM
Data Validation Listbox problem RASEnt Excel Programming 0 June 12th 05 11:47 PM
Data Entry Listbox Tom Ogilvy Excel Programming 0 January 19th 05 04:03 PM
data entry with userform questions mbernardi Excel Programming 0 September 24th 04 02:06 AM
Creating a Data Entry Userform spurtniq[_8_] Excel Programming 1 January 8th 04 05:48 AM


All times are GMT +1. The time now is 08:08 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"