How To Programmatically Set A Listbox To The First Record

  • Thread starter CrazyAccessProgrammer
  • 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.
 
C

CrazyAccessProgrammer

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.
 
S

Stuart McCall

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
 
S

Stuart McCall

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
 
C

CrazyAccessProgrammer

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.
 
S

Stuart McCall

Dirk Goldgar said:
Or even,

.Value = .ItemData(Abs(.ColumnHeads))


--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)

Neat way of obtaining the index, but it's not too obvious what's going on -
needs a comment IMO
 
D

David H

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top