ListBox on Access form

I have a subform that has a listbox on it. When I open the main form to a specific record I want the listbox on my subform to select the first record in it. I have tried so many variations of Me.lstItems.ItemData(0), etc. but none of them have worked. Can someone please share with me how I can have the first record in my listbox selected which will also highlight that record so I can see that it's actually selected?
LVL 1
Lawrence SalvucciInformation Technology ManagerAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Rey Obrero (Capricorn1)Connect With a Mentor Commented:
see this sample db

open the form Customers and see that te first item in the listbox in subform is selected
Q_28979975.accdb
0
 
Rey Obrero (Capricorn1)Commented:
first you need to specify where the listbox is

Me![subformcontrolname].form.lstItems.ItemData(0)

brb
0
 
Lawrence SalvucciInformation Technology ManagerAuthor Commented:
Should that go in the "On Open" or "On Load" event of the main form?
0
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

 
Rey Obrero (Capricorn1)Commented:
try placing the codes in Load and Current event of the form
0
 
Lawrence SalvucciInformation Technology ManagerAuthor Commented:
of the main form or the subform?
0
 
Rey Obrero (Capricorn1)Commented:
of the main form
0
 
Lawrence SalvucciInformation Technology ManagerAuthor Commented:
I put that code in both the On Load and On Current events of the main form and I am getting the following error:

"You entered an expression that has an invalid reference to the property Form/Report"
0
 
Rey Obrero (Capricorn1)Commented:
@Lawrence,

I can't determine the error if you are not going to post the codes that you used.
0
 
Lawrence SalvucciInformation Technology ManagerAuthor Commented:
The code you posted:

Me![sfrmSalesOrderItems].Form.lstItems.ItemData (0)

Open in new window

0
 
Rey Obrero (Capricorn1)Commented:
ok... now I need to see your db
upload a copy of the db

try these codes
with Me![sfrmSalesOrderItems].Form.lstItems
    .Selected(0) = True
    .Value = .ItemData(0)
end with

are you sure that ![sfrmSalesOrderItems] is the name of the CONTROL that house your subform?
0
 
Lawrence SalvucciInformation Technology ManagerAuthor Commented:
I cannot upload a copy of the DB. It's too big and has too many links to other sources, etc.

sfrmSalesOrderItems is the name of the SUBFORM. The name of the ListBox is lstItems
0
 
Rey Obrero (Capricorn1)Commented:
<sfrmSalesOrderItems is the name of the SUBFORM>
I am asking for the name of the CONTROL where the subform is.

in the design view of the main form, click on the edge of the subform and hit F4, click on the Other tab on the Property window and look what is listed in the Name property
0
 
Lawrence SalvucciInformation Technology ManagerAuthor Commented:
sfrmSalesOrderItems is the name as well
0
 
Rey Obrero (Capricorn1)Connect With a Mentor Commented:
try these codes

 with Me![sfrmSalesOrderItems].Form.lstItems
     .Selected(0) = True
     .Value = .ItemData(0)
 end with
0
 
Lawrence SalvucciInformation Technology ManagerAuthor Commented:
In the On Current & On Load event of the main form?
0
 
Rey Obrero (Capricorn1)Commented:
try first the LOad event of the main form
0
 
Lawrence SalvucciInformation Technology ManagerAuthor Commented:
Same error as before:

"You entered an expression that has an invalid reference to the property Form/Report"
0
 
Rey Obrero (Capricorn1)Commented:
well. there is something in your form that is causing this error.

create a blank db and import the main form and the subform, upload the new db
0
 
Lawrence SalvucciInformation Technology ManagerAuthor Commented:
I will look into it more in a little bit. I have to move on to other tasks. I will post back when I create a blank DB and test it out in there.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.