How to get a listbox to display the first row

JONeill

Board Regular
Joined
Sep 2, 2018
Messages
58
I'm binding a runtime query to a listbox. I hit the cancel button and the listbox displays the last several rows in the recordset rather than display the first row. ListIndex and the properties I use in Excel cannot be assigned values in Access. How can I get the listbox to display the first row? Tried to refresh the recordset but that doesn't work either.
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
One way: you can edit the query source to run a Top (1) query.
 
Upvote 0
Googling MSAccess Top 1 query gets tons of hits to explain what that is. Have you tried that yet?
 
Upvote 0
After rereading my initial post, I realized I didn't explain my question very well. I don't need just the first row. I need the whole recordset. So, say I have 50 records in the recordset but the listbox is only tall enough to display 25 of them. When I initially load the form, I see the first 25 records but on all subsequent refreshes after an some sort of update. Access scrolls down and see the last 25. I'd like to see the first 25 again when I first brought up the form. Sorry about the confusion.
 
Upvote 0
Hmm that is a little different. Offhand I'd assume you are doing something different in the subsequent refreshes compared to the first one. How is the listbox being used? Because otherwise a refresh should simply refresh it and it would look the same (to test that theory, add a button so you can call a refresh right after you load it for the first time to confirm that the refresh itself isn't changing the way it looks).

Note that you do have ways of accessing list properties but it's a little different depending on what type of list box you are actually using (and how you set it up (multiselect, single select, extended selection). So you have to be patient and figure it out.
 
Last edited:
Upvote 0
I'm new to Access so, I think I'm programming more like VB/C# programmer. I'm not using ADO the same way since I'm hacking through it. All my queries are in modules and they either return a recordset or rowsAffected. So, I don't have any queries wired to controls. I have seen the DoCmd.??? to refresh a record or recordset. It was originally going to be an Excel app but it's better suited to Access.
 
Upvote 0
It's true that there are lot of ways to get data into a listbox. You probably don't have to use ADO for *all* your queries - if you literally are doing that. Access Queries can be created without ADO just going to query designer and typing in the SQL. For that matter listboxes can just have the query source in the listbox properties (rowsource I think is what it's called).

No matter, the main thing I think is that probably you want to do a refresh at the earliest possible point just to determine the behavior of your refreshes before anything else happens. If it is different later on then probably their is a change in the state of the form that is causing the difference (which I guess is obvious - but you would need to figure out what the difference is).
 
Last edited:
Upvote 0
I realized that the listbox is actually scrolling to the bottom of the recordset on Form_Load. So, it has to be something I changed in between and didn't notice. I'm using DAO but I guess I'm using it in an ADO kind of way.
In Form_Load, the call is
lstMaintLog.Recordset = GetListboxData()

GetListboxData() actually calls the function that calls the query into a recordset, checks for errors or 0 record counts and returns a recordset. Not sure if that's overkill for Access or not. I will do some more research and post what I find.
 
Upvote 0
Interesting. I guess I'm not in possession of any other ideas at the moment, although I would hazard that in general a recordset starts with the record pointer at the beginning of the recordset. If you are checking records counts though, you may be moving forward to the end of the recordset to make sure the recordset is fully loaded before you get the count (and with some types of recordsets that could even be necessary to get a correct count). In which case you'd want to move the recordset pointer back to the beginning.
 
Upvote 0

Forum statistics

Threads
1,213,554
Messages
6,114,280
Members
448,562
Latest member
Flashbond

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top