setting position in multiselect listbox

  • Thread starter Thread starter Jesper F
  • Start date Start date
J

Jesper F

I have a multiselect listbox with times of the day such as 07:00, 08:00 etc.
form wich the user select a timerange.
Based on the current time when the form opens, I'd like to select one of
these (the closest to the current time).
Is this possible to preselect a value in a multiselect listbox?
Thanks.
 
Hi,
This will work for you if you're using a 24 hour clock (13:00, 14:00 etc)
If not, you'll have to tweak it a bit using a different Format and parsing routine.
Try this code in your form's open event, just substitute the name of your listbox

Dim intHour As Integer
Dim intSeconds As Integer
Dim i As Integer

'get the hour
intHour = Left(Format(Now, "hh:mm"), InStr(1, Format(Now, "hh:mm"), ":", vbTextCompare) - 1)
'get the minutes
intSeconds = Mid(Format(Now, "hh:mm"), InStr(1, Format(Now, "hh:mm"), ":", vbTextCompare) + 1)

'if minutes is greater than 30 bump he hour by one
If intSeconds > 30 Then
intHour = intHour + 1
End If

'go through the values and select the correct hour
For i = 0 To Me.lstTimes.ListCount - 1
If Me.lstTimes.Column(0, i) = intHour & ":00" Then
Me.lstTimes.Selected(i) = True
End If
Next i
 
Thanks Dan. That works good for selecting values based on the current time.
What I'd also like to do however is to move the scrollbar in the list box
accordingly.
Is that possible?
 
Hi,
Are you saying the although the value is selected in the listbox, you have to manually scroll
down to see it?
In my sample form, the listbox scrolls automatically to the selected entry, so I don't know what to suggest.
By the way, it's probably better to put this code in the form's load event, I had some trouble with it in the
Open event.
 
Hey Dan,
the ListBox control is not fully instantiated in parent Form's Open
event. As you stated, the Load event is the proper place for the code in
question.

With MulitSelect ListBox controls, preselecting a value for the control
does not always bring that row into view. An alternative solution is
here:
http://www.lebans.com/List_Combo.htm#ScrollListbox

--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.


Dan Artuso said:
Hi,
Are you saying the although the value is selected in the listbox, you have to manually scroll
down to see it?
In my sample form, the listbox scrolls automatically to the selected
entry, so I don't know what to suggest.
By the way, it's probably better to put this code in the form's load
event, I had some trouble with it in the
 
-----Original Message-----
Hey Dan,
the ListBox control is not fully instantiated in parent Form's Open
event. As you stated, the Load event is the proper place for the code in
question.

With MulitSelect ListBox controls, preselecting a value for the control
does not always bring that row into view. An alternative solution is
here:
http://www.lebans.com/List_Combo.htm#ScrollListbox

This is great. Got it working with your examples.
Thank you very much both of you.
 
Back
Top