Results 1 to 14 of 14
  1. #1
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    San Francisco, CA, USA
    Posts
    184
    Thanks
    0
    Thanked 0 Times in 0 Posts

    List Box Top Row Go To? (2002)

    I have a list box named lstCustomers. I set the record source depending on differennt situations, to different queries. I'm using a macro that after the record source changes I go to control lstCustomers then use send keys {Down} to select and highlight the row and that updates the mainform's data with that record.
    The problem I have when the record source changes and the list box is selected it just selects a random row rather than going to the top of the list. I tried go to first record but that does not work.
    Any help would be appreciated.
    Frank Hoeffer

  2. #2
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: List Box Top Row Go To? (2002)

    When you say you set the record source, do you mean the rowsource of the listbox? Forget SendKeys. It's highly unreliable for stuff like this. If you want to set a default value on the listbox, just use =[list].itemdata(0) in the DefaultValue property of the listbox. That's assuming you replace listboxname with the actual name of the listbox control. If you have column headers turned on, you'll have to use itemdata(1) to get the first value rather than the column header. I use this frequently on subforms where the list source changes depending on a condition on the parent form.
    Charlotte

  3. #3
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    San Francisco, CA, USA
    Posts
    184
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: List Box Top Row Go To? (2002)

    Yes it's the rowsource. Tried =[lstCustomers].itemdata(0) in Default Value but it does not put me at the top of the list box

  4. #4
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: List Box Top Row Go To? (2002)

    Instead of trying to set default value recommend setting listbox value after changing its RowSource. Example:

    <pre> With Me.lstCustomers
    .RowSource = "qryCustomers"
    .Value = .ItemData(0) 'No column headings
    ' .Value = .ItemData(1) 'Column headings
    End With
    </pre>

    If you insist on doing this in a macro, after action that changes listbox's RowSource, select SetValue from Action list, then for Item enter [lstCustomers] and for Expression enter [lstCustomers].[ItemData](0) (change the "0" to "1" if listbox has column headings).

    HTH

  5. #5
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: List Box Top Row Go To? (2002)

    Is this a new record or is there already data in it? The default value will only work for new records. If there's data, you don't want to change the value do you?
    Charlotte

  6. #6
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    San Francisco, CA, USA
    Posts
    184
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: List Box Top Row Go To? (2002)

    This is for old data.
    All I want to do is after the rowsource is changed the scroll bar goes up to the first row.

  7. #7
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: List Box Top Row Go To? (2002)

    Is there a small demo you can attach, if you're still using the send keys, I guess the problem is their.

    I use list box's with several SQL statements using the ORDER BY facility, some thing is confusing the row source of your list's.

    Dave

  8. #8
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: List Box Top Row Go To? (2002)

    I apparently didn't make myself clear. If the listbox control is bound to a field, then the value the list shows will be the value of the underlying field ... unless that field is not in the list. You can only do what you want if the control is unbound, that is, if its controlsource is null. Is that the situation? I suspect not or you wouldn't be having this problem. If it *is* bound and you use code or any other method to set the control value to other than the underlying field, you will change the value in that field in the record and have a bunch of invalid data as a result.
    Charlotte

  9. #9
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    San Francisco, CA, USA
    Posts
    184
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: List Box Top Row Go To? (2002)

    Hi Charlotte,
    The list box is bound to a field. I just want the cursor to go to the top of the top of the list box when I set a new query. I tried page up using send keys but if it's more than one page it just goes to the middle of the box.

  10. #10
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: List Box Top Row Go To? (2002)

    "More than 1 page"? Do you mean if the listbox has too many rows to see all at once? When to goes to "the middle" of the list, is the value highlighted the underlying value of the field? I'm trying to tell you that bound listboxes display the value of the underlying field. You *can't* make them show someting else without changing their value.
    Charlotte

  11. #11
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    San Francisco, CA, USA
    Posts
    184
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: List Box Top Row Go To? (2002)

    I understand.
    I just want when the rowsource is changed the the to record is highlighted in the list box and it's ok that the data on the form is that record. I just want it to look more elegant when the query changes and the first row of the list box is selected, highlighted and the record is that record for the form. Presenty the highlighed record when the query changes is random.

  12. #12
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: List Box Top Row Go To? (2002)

    I get the impression from what you're saying that the listbox is merely there for choosing particular records. If so, then it should not be bound to a field (i.e. its controlsource should be null) as Charlotte has already pointed out, otherwise it will default to the value of that field in whatever record is current in your 'main' form. Charlotte has already told you how to set the initial value for an unbound listbox.
    Hope that helps.
    Regards,
    Rory

    Microsoft MVP - Excel

  13. #13
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    San Francisco, CA, USA
    Posts
    184
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: List Box Top Row Go To? (2002)

    I don't understand the situation.
    I use the list box to select records on the main form.
    I have various options of Accending Decending Find Customer etc. This is brought about seting the rowsource to different queries.
    I created the list box using the list wizard which produces the following code:

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

    Set rs = Me.Recordset.Clone
    rs.FindFirst "[CustomerID] = " & Str(Me![lstCustomers])
    Me.Bookmark = rs.Bookmark

    End Sub

    The control source is blank.

    I just want the cursor to go the the topmost record select it and that is the current record of the form.

    I'm a little confused on bound and unbound and how would I get the same functionality.

    Frank

  14. #14
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: List Box Top Row Go To? (2002)

    If the control has a field name in its ControlSource property, then it's a bound control. If it has something like a function or an expression in ControlSource, it's a calculated control. If it doesn't have anything there, it's an unbound control. Listboxes and Comboboxes have a controlsource, a RowSourceType and a RowSource property, and it is easy to get confused by them at first. If you look at the form in design view and you see a "unbound" in the control, it means the control is unbound, even though it may have something in its RowSource telling it where the list comes from. Does that clarify it for you?

    The confusion has come from the fact that it sounded like you were saying that the list came up with a random item selected. That normally suggests a bound listbox because otherwise, a requery caused by resetting the rowsource should have resulted in the list coming up with the cursor positioned at the top item, although without that item highlighted. Selecting (highlighting) one of the items requires either clicking in the unbound listbox or using code to select the item.

    I don't know if it will help, but I'm attaching an Access 2000 database with an unbound listbox on an unbound form. If you type one of the CustomerIDs into the textbox, it will select that item in the listbox. Otherwise, the first item is selected automatically. The rowsource is not being reset in any way. This only demonstrates selecting an item in a listbox from code.
    Attached Files Attached Files
    Charlotte

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •