Why doesn't preselecting my listbox record from VB work?

M

Max Moor

Hi All,

I have a form in Access 2002 with a listbox and some textboxes. The
form's record source is dependent on the listbox's selection. In the
listbox's click routine, I requery the form.

The form's Open routine requeries the listbox to bring it to life, then
sets the first record to be selected, then requeries the form. Pretty
straightforward.

The code is:
Me!lstListings.Requery
Me!lstListings.Selected(0) = True
Me!Requery

When the form opens, the first record in the listbox is selected, but
the form's textboxes don't reflect that the form has been requeried. They
are blank as if "unhooked". If I click on the listbox, the form updates and
works fine after that.

Why is the form nor requerying on the listbox info on first open? Any
thoughts?

- Max
 
D

Dirk Goldgar

Max Moor said:
Hi All,

I have a form in Access 2002 with a listbox and some textboxes. The
form's record source is dependent on the listbox's selection. In the
listbox's click routine, I requery the form.

The form's Open routine requeries the listbox to bring it to life, then
sets the first record to be selected, then requeries the form. Pretty
straightforward.

The code is:
Me!lstListings.Requery
Me!lstListings.Selected(0) = True
Me!Requery

When the form opens, the first record in the listbox is selected, but
the form's textboxes don't reflect that the form has been requeried. They
are blank as if "unhooked". If I click on the listbox, the form updates
and
works fine after that.

Why is the form nor requerying on the listbox info on first open? Any
thoughts?


Assuming this is not a multiselect list box, try this:

With Me!lstListings
.Requery
.Value = .ItemData(0)
End With
Me!Requery

I'm not sure why you need to requery the list box; you might try it without
the .Requery to see if it works the same.
 
M

Max Moor

.Value = .ItemData(0)

Hi Dick,

That did it. Thank you!

It doesn't work without the requery, and it finally dawned on me why.
The SQL for the list box depends on a combobox on the same form. I'm no
expert on how Access deals with initialization of controls on forms, but I'd
imagine that when the listbox tries to initialize, the combobox isn't ready,
so the listbox's SQL fails, and it comes up blank. By the time I get to the
form's Open routine, the combobox is updated, so the listbox's query can
complete. There's probably a safer way to do this, but I don't know it. :)

- Max
 

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