ListBox focus

R

Russ

When I open my form that has a listbox, although the cursor appears to
be at the first item in the list, the remainder of the form does not
display what the listbox appears to be sitting on. Instead, it
displays the data from the record with the lowest key field.

When I do click on the listbox first item, the data for it is
displayed. How can the form show the data for the first item in the
listbox when it is first opened?
 
D

Dirk Goldgar

Russ said:
When I open my form that has a listbox, although the cursor appears to
be at the first item in the list, the remainder of the form does not
display what the listbox appears to be sitting on. Instead, it
displays the data from the record with the lowest key field.

When I do click on the listbox first item, the data for it is
displayed. How can the form show the data for the first item in the
listbox when it is first opened?


I guess that you have the list box set up so that selecting an item in the
list box moves the form to the selected record. Maybe you also have some
code to select the first item in the list box when the form opens. That
same code is going to have to also call the event procedure (or run the
macro) that would normally be triggered when an item is selected; probably
the list box's AfterUpdate event procedure.

For example, if you have this code in the form's Open or Load event:

Me.lstMyListbox = Me.lstMyListbox.ItemData(0)

.... you might follow it with a call to the control's AfterUpdate event
procedure:

Me.lstMyListbox = Me.lstMyListbox.ItemData(0)
Call lstMyListbox_AfterUpdate

Note that I could be guessing wrong about how you have this set up. This is
just one way you might do it.
 
L

Linq Adams via AccessMonster.com

I think Dirk's guess is probably correct. He's explained how to get the
results you want. The ***reason*** for having to explicitly call the
AfterUpdate event, with the code he provided, is that events such as the
AfterUpdate event are fired when you ***physically*** make a selection from
the Listbox, but ***do not fire when you make a selection thru code.***
 
R

Russ

The only way that the listbox is showing a cursor is that it is first
in the tab order. I have no code there except what Access put there
when I created the listbox.

Private Sub List13_AfterUpdate()
' Find the record that matches the control.
Dim rs As Object

Set rs = Me.Recordset.Clone
rs.FindFirst "[MechID] = " & Str(Me![List13])
Me.Bookmark = rs.Bookmark
End Sub
 
D

Dirk Goldgar

Russ said:
The only way that the listbox is showing a cursor is that it is first
in the tab order. I have no code there except what Access put there
when I created the listbox.

Private Sub List13_AfterUpdate()
' Find the record that matches the control.
Dim rs As Object

Set rs = Me.Recordset.Clone
rs.FindFirst "[MechID] = " & Str(Me![List13])
Me.Bookmark = rs.Bookmark
End Sub


Argh, that code is ... suboptimal. Although it will work as is, may I
suggest that you change it to this:

'----- start of revised code ----
Private Sub List13_AfterUpdate()

With Me.List13
If Not IsNull(.Value) Then
Me.Recordset.FindFirst "[MechID] = " & .Value
End If
End With

End Sub

'----- end of revised code ----

But that is not enough in itself. If, as you say, there is nothing
currently setting the list box's initial value, then it doesn't even really
have a value when the form is first opened. I suggest you create this event
procedure for the form's Load event:

'----- start of code -----
Private Sub Form_Load()

With Me.List13

If .ListCount > 0 Then
.Value = .ItemData(Abs(.ColumnHeads))
Call List13_AfterUpdate
End if

End With

End Sub
'----- end of code ----

--

Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
R

Russ

That helped alot! I'm going to have to remember that one as I've got
several listboxes in other dbs that need attention. Thank you.

Russ said:
The only way that the listbox is showing a cursor is that it is first
in the tab order. I have no code there except what Access put there
when I created the listbox.

Private Sub List13_AfterUpdate()
' Find the record that matches the control.
Dim rs As Object

Set rs = Me.Recordset.Clone
rs.FindFirst "[MechID] = " & Str(Me![List13])
Me.Bookmark = rs.Bookmark
End Sub


Argh, that code is ... suboptimal. Although it will work as is, may I
suggest that you change it to this:

'----- start of revised code ----
Private Sub List13_AfterUpdate()

With Me.List13
If Not IsNull(.Value) Then
Me.Recordset.FindFirst "[MechID] = " & .Value
End If
End With

End Sub

'----- end of revised code ----

But that is not enough in itself. If, as you say, there is nothing
currently setting the list box's initial value, then it doesn't even really
have a value when the form is first opened. I suggest you create this event
procedure for the form's Load event:

'----- start of code -----
Private Sub Form_Load()

With Me.List13

If .ListCount > 0 Then
.Value = .ItemData(Abs(.ColumnHeads))
Call List13_AfterUpdate
End if

End With

End Sub
'----- end of 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

Similar Threads


Top