Solved

ListBox on Access form

Posted on 2016-10-31
19
38 Views
Last Modified: 2016-11-03
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?
0
Comment
Question by:Lawrence Salvucci
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 9
19 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 41866860
first you need to specify where the listbox is

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

brb
0
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 41866865
Should that go in the "On Open" or "On Load" event of the main form?
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 41866869
try placing the codes in Load and Current event of the form
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 41866872
of the main form or the subform?
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 41866878
of the main form
0
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 41866883
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
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 41866886
@Lawrence,

I can't determine the error if you are not going to post the codes that you used.
0
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 41866890
The code you posted:

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

Open in new window

0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 41866895
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
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 41866900
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
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 41866910
<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
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 41866914
sfrmSalesOrderItems is the name as well
0
 
LVL 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 500 total points
ID: 41866917
try these codes

 with Me![sfrmSalesOrderItems].Form.lstItems
     .Selected(0) = True
     .Value = .ItemData(0)
 end with
0
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 41866922
In the On Current & On Load event of the main form?
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 41866926
try first the LOad event of the main form
0
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 41866943
Same error as before:

"You entered an expression that has an invalid reference to the property Form/Report"
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 41866951
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
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 41866968
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
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 41867001
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question