Listbox Default Value

K

kabaka

Hi,

I have a form from which the user selects a date from a listbox. The
listbox values are generated from a sql statement in the form's OnOpen event.
I would like the listbox to have the maximum (i.e. latest) date as its value
when the form opens up. That much I can accomplish.

The problem is having the listbox appear as though the latest date has been
selected. The latest date is always row 0. I have tried using the
"Selected" property which does not work. Actually does not work is
inaccurate. It prevents ANYTHING from happening on the form. I cannot
select other dates, I cannot click on my tabs, I cannot click on buttons.
The only way out is by going to design view and close from there. Is this an
error on my part or (I hate to use the b-word but) a bug?

Thanks,

The code follows:

Private Sub Form_Open(Cancel As Integer)
Dim dbs As Database, rst As Recordset
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("Select max([allocation_date]) from
[INVS_pacesmtb History]")
Me.AllocDate.Value = Format(rst.Fields(0).Value, "yyyy-mm-dd")
'*******This is the bad line*******
Me.AllocDate.Selected(0) = True
'***************************
rst.Close
Set dbs = Nothing
End Sub
 
K

kabaka

Nevermind. If you put the line:

me.allocdate.selected(0) = true

into the form's Current event it works. I did some more reading, and found
that with the listbox (I don't know how to say this properly) it is not
completely done "loading" (?) in the OnOpen event, and any referencing to its
rows cannot be made yet.
 
Top