Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I sorry Tom, I quite new at this game!
I did change my code to: 'check for a Month/Year If Me.ListBox2.ListIndex = 0 Then msg = "Please enter Valid Date" MsgBox msg UserForm1.ListBox2.SetFocus Exit Sub End If And this worked because I have a "blank" for the first item in my list with the listindex of 0. What I am wondering now is would there be a better place to put this code to ensure the user has filled out the form completely prior to the "save" button on the form? Thanks, Janice "Tom Ogilvy" wrote: I showed you how to check the listbox to see if a selection has been made. How you implement the check is up to you. Your code is writing the data. If you don't want to write the data, make the check before you write the data. -- Regards, Tom Ogilvy "jjordan" wrote: Hi Tom, Thanks for your reply, however this will still allow the form data to populate the spreadsheet without an error msg appearing. I'm sure it's something I'm overlooking here. I realize this is probably too much info, but here is my code for the listbox, and the code to save the form data to the spreadsheet: Private Sub UserForm_Initialize() With ListBox2 .AddItem "" .AddItem "April 2007" .AddItem "May 2007" .AddItem "June 2007" .AddItem "July 2007" .AddItem "August 2007" .AddItem "September 2007" .AddItem "October 2007" .AddItem "November 2007" .AddItem "December 2007" .AddItem "January 2008" .AddItem "February 2008" .AddItem "March 2008" End With ListBox2.ListIndex = 0 End Sub 'save data and unload form data Private Sub CommandButton1_Click() Dim iRow As Long Dim ws As Worksheet Set ws = Worksheets("MonthlyData") 'find first empty row in database iRow = ws.Cells(Rows.Count, 1) _ .End(xlUp).Offset(1, 0).Row 'check for a Month/Year If Me.ListBox2.ListIndex = -1 Then msg = "Please enter Valid Date" MsgBox msg UserForm1.ListBox2.SetFocus Exit Sub End If 'copy the data to the database ws.Cells(iRow, 1).Value = Me.ListBox2.Value ws.Cells(iRow, 2).Value = Me.ListBox1.Value ws.Cells(iRow, 3).Value = Me.TextBox2.Value ws.Cells(iRow, 4).Value = Me.TextBox3.Value 'clear the data Me.ListBox2.ListIndex = 0 Me.ListBox1.Value = "" Me.TextBox2.Value = "" Me.TextBox3.Value = "" Me.CommandButton2.SetFocus End Sub Hope this helps! JJ "Tom Ogilvy" wrote: If Me.ListBox2.ListIndex = -1 Then msg = "Please enter Valid Date" MsgBox msg UserForm1.ListBox2.SetFocus Exit Sub End If -- Regards, Tom Ogilvy "jjordan" wrote: Hi there, I have a userform with a listbox. I need to verify that the user has selected an item from this listbox, and if not have a message appear to tell them to do so. I've written the following code: 'check for a Month/Year If Me.ListBox2.Value = "" Then msg = "Please enter Valid Date" MsgBox msg UserForm1.ListBox2.SetFocus Exit Sub End If This code will work on a Textbox, but not a listbox. Any help would be greatly appreciated! JJ |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
And this worked because I have a "blank" for the first item in my
list with the listindex of 0. My question to you is why do you have a "blank" item in your ListBox at all? What does having that accomplish for you? I ask because I can't think of any reason where doing this makes sense. Think about all the commercial and/or professional programs you have used... do you ever remember seeing a "blank" item in a ListBox (or ComboBox for that matter)? Rick |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Darn, you had to ask that question! :)
I'm embarrassed to admit that being a newbie to this game this was a work-around, as I did not have time to find out the correct way to achieve the look I wanted. As you mentioned about referring to any program having a blank item at the top of a list, yes, it does appear that way to a user - being that when a form is opened, all fields appear blank and ready for them to enter something. When the form I created opened the first item in my list appeared in the field, so to achieve the look I wanted I added the "blank" item to the top of my list. I am creating this spreadsheet for an inept computer user (and I mean that in the nicest possible way!) and I am attempting to make it as fool-proof as possible. I can imagine this user opening a form, and pressing the enter key with the intent to move to the first field, and instead accepting the first name in the list without even noticing it, and proceeding to complete the remainder of the form. Do I get any points for creativity? :) But, now that I have your attention Rick, is there a correct way to do this? I would really appreciate your feedback. Thanks, Janice "Rick Rothstein (MVP - VB)" wrote: And this worked because I have a "blank" for the first item in my list with the listindex of 0. My question to you is why do you have a "blank" item in your ListBox at all? What does having that accomplish for you? I ask because I can't think of any reason where doing this makes sense. Think about all the commercial and/or professional programs you have used... do you ever remember seeing a "blank" item in a ListBox (or ComboBox for that matter)? Rick |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Darn, you had to ask that question! :)
LOL But, now that I have your attention Rick, is there a correct way to do this? I would really appreciate your feedback. Remove the "blank" item and set the ListIndex property of the ListBox to -1. The -1 means no item is selected (hence, none is highlighted). Tom already gave you the processing method in his first post... test the ListIndex property for -1... if it is -1, that means no item is currently selected. The user cannot create this condition (the -1 value for the ListIndex) once he/she selects something in the list, so this testing method is foolproof. However, you set the ListIndex property to -1 in code anytime you need want to in order to present the user with a "new" (unselected) ListBox. Rick |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Call me thick - I must be missing something here!
I thought perhaps I wasn't understanding Tom's message because it didn't work the way I had expected. I have now removed my "blank" item from my list, set my listbox2.listindex = -1, confirmed that my "TopIndex" in the properties window is at -1. When I open the form, the top item in my list still appears. No, it is not highlighted, and yes, the verification works, however I wanted the field to appear blank. I know the computer is smarter than me - what could I be doing to cause this? JJ "Rick Rothstein (MVP - VB)" wrote: Darn, you had to ask that question! :) LOL But, now that I have your attention Rick, is there a correct way to do this? I would really appreciate your feedback. Remove the "blank" item and set the ListIndex property of the ListBox to -1. The -1 means no item is selected (hence, none is highlighted). Tom already gave you the processing method in his first post... test the ListIndex property for -1... if it is -1, that means no item is currently selected. The user cannot create this condition (the -1 value for the ListIndex) once he/she selects something in the list, so this testing method is foolproof. However, you set the ListIndex property to -1 in code anytime you need want to in order to present the user with a "new" (unselected) ListBox. Rick |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I have now removed my "blank" item from my list, set my listbox2.listindex
= -1, confirmed that my "TopIndex" in the properties window is at -1. When I open the form, the top item in my list still appears. No, it is not highlighted, and yes, the verification works, however I wanted the field to appear blank. What "field" are you talking about that isn't blank? Rick |
#7
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
The "field" I am referring to is the listbox on my userform. I have it only
large enough to show the first item (which I want to be blank). Then, when the user moves to that field (listbox) she will start typing and the item from the list will appear (based on her keystrokes). I'm sorry if I'm not making myself clear, as I said before I am new to this game and trying to be succinct :( JJ "Rick Rothstein (MVP - VB)" wrote: I have now removed my "blank" item from my list, set my listbox2.listindex = -1, confirmed that my "TopIndex" in the properties window is at -1. When I open the form, the top item in my list still appears. No, it is not highlighted, and yes, the verification works, however I wanted the field to appear blank. What "field" are you talking about that isn't blank? Rick |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Removing Data Validation (Listbox) | Excel Worksheet Functions | |||
Finding window handle of data validation listbox | Excel Programming | |||
Userform: listbox and controls' data entry validation | Excel Programming | |||
Data Validation Listbox problem | Excel Programming | |||
Change event for data validation listbox | Excel Programming |