Recordsets

G

Guest

I have a form with a list box and when I Enter the listbox by tabbing from
the previous object I want the first item to be hightlighted. The listbox
has records but currently the first record is not highlighted. I was trying
something like this in the ON ENTER event:

Dim db As Database
Dim rs As Recordset
Set db = CurrentDb()
Set rs = db.OpenRecordset("QueryWithVariable", dbOpenDynaset)
rs.MoveFirst
Me.List248 = rs("Account")
rs.Close
db.Close

The QueryWithVariable query has a Criteria argument where it gets a variable
ReturnVariable1(). I get an error on the OpenRecorset line of code when
entering the listbox that says:
Too Few Parameters: Expected 1

What am I not doing correct here?

Thank you for your help.

Steven
 
D

Dirk Goldgar

Steven said:
I have a form with a list box and when I Enter the listbox by tabbing
from the previous object I want the first item to be hightlighted.
The listbox has records but currently the first record is not
highlighted. I was trying something like this in the ON ENTER event:

Dim db As Database
Dim rs As Recordset
Set db = CurrentDb()
Set rs = db.OpenRecordset("QueryWithVariable", dbOpenDynaset)
rs.MoveFirst
Me.List248 = rs("Account")
rs.Close
db.Close

The QueryWithVariable query has a Criteria argument where it gets a
variable ReturnVariable1(). I get an error on the OpenRecorset line
of code when entering the listbox that says:
Too Few Parameters: Expected 1

What am I not doing correct here?

Thank you for your help.

Steven

That seems like way too much work to be doing if all you want to do is
select the first item in the list box. If the list box is single-select
(that is, not multi-select), so that selecting the first item is
equivalent to setting the value of the list box to the value of the
first item in the list, then you can use code like this:

'----- begin code for single-select list box -----
With Me.List248
If .ListCount > 0 Then
.Value = .ItemData(Abs(.ColumnHeads))
End If
End With
'----- end code for single-select list box -----

If the list box is multi-select, then you can't set its value, but you
can select the first item (and clear all the other items) like this:

'----- begin code for multi-select list box -----
Dim intI As Integer

With Me.List248

' Clear all current selections
For intI = (.ItemsSelected.Count - 1) To 0 Step -1
.Selected(.ItemsSelected(intI)) = False
Next intI

' Select the first item.
If .ListCount > 0 Then
.Selected(Abs(.ColumnHeads)) = True
End If

End With

'----- end code for multi-select list box -----

Note -- that's "air code", but something very like that ought to work.
 
P

PC Datasheet

Put the following code in the On Enter event:
Me!NameOfListbox = Me!NameOfListbox.ListIndex(0)
 
D

Dirk Goldgar

PC Datasheet said:
Put the following code in the On Enter event:
Me!NameOfListbox = Me!NameOfListbox.ListIndex(0)

I think you meant ItemData, not ListIndex. But that doesn't take into
account the possibility of ColumnHeads being set to Yes, and it won't
work for a multiselect list box.
 

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