How To Programmatically Set A Listbox To The First Record

  • Thread starter Thread starter CrazyAccessProgrammer
  • Start date Start date
C

CrazyAccessProgrammer

I want to use a form's OnCurrent event to automatically set a listbox to the
first record listed in the listbox.

I need code that will work regardless of whether or not the listbox has the
focus.
I also need code that will work for listboxes whose Multi-Select property is
set to NONE.

Thank you.
 
Answered my own question... here's what I did for anyone else who wants to do
this:

Me.LstCtrls.Value = Me.LstCtrls.ItemData(1)

LstCtrls is the name of my Listbox.
 
CrazyAccessProgrammer said:
Answered my own question... here's what I did for anyone else who wants to
do
this:

Me.LstCtrls.Value = Me.LstCtrls.ItemData(1)
<snip>

The index number 1 only refers to the 1st list item if the listbox's Column
Heads property is Yes. Otherwise, the 1st item should be referenced using
index 0:

Me.LstCtrls.Value = Me.LstCtrls.ItemData(0)

or (more generic):

With Me.LstCtrls
.Value = .ItemData(Iif(.ColumnHeads, I1, 0))
End With
 
Oops. Correction below:

Stuart McCall said:
"CrazyAccessProgrammer" <[email protected]>
wrote in message

<snip>

The index number 1 only refers to the 1st list item if the listbox's
Column Heads property is Yes. Otherwise, the 1st item should be referenced
using index 0:

Me.LstCtrls.Value = Me.LstCtrls.ItemData(0)

or (more generic):

With Me.LstCtrls
.Value = .ItemData(Iif(.ColumnHeads, I1, 0))
End With

With Me.LstCtrls
.Value = .ItemData(Iif(.ColumnHeads, 1, 0))
End With
 
Thanks for the additional info. I was wondering why I had to use 1 and not 0
when the help info in Access says to use 0 for the first row... but now I
understand why thank you.
 
I'd go with the code that check if .ColumnHeads is set so that if you later
turn on the Headers you don't have to remember to go back and change the code.
 
Back
Top