Listbox performance issue with value list - Access - VBA

Posted on 2014-03-25
Last Modified: 2014-03-26
Hi experts, I have an issue with a list box performance when loading  the data, It is loading fast when the form open first time based on a combo box value, But after some  form operations (successful insert/update data to table using the form) - this list box  taking too much time to load (more than 3 minutes). When i checked the code with break point - seems like it is hanging on the while loop to add stritem  to the list box.
Can somebody please help me.

Dim db As DAO.Database
   Dim rs As DAO.Recordset
   Dim strSQL1 As String
   Dim strItem As String
   Set db = CurrentDb
     strSQL1 = "SELECT  * FROM Employee WHERE ((Person_Number)  not in (select person_number from Employee_Room_Status WHERE Employee_Room_Status.Employee_Active = true and Employee_Room_Status.Room_ID =" & Me.Combo_Room.Value & "))"
     Set rs = db.OpenRecordset(strSQL1)
   Do Until rs.EOF
      strItem = rs.Fields("Last_Name").Value & ";" _
         & rs.Fields("First_Name").Value & ";" _
         & rs.Fields("Middle_Initial").Value & ";" _
         & rs.Fields("Person_Number").Value
      Me.List1Edit.AddItem strItem      ' Row Source Type must be Value List

   Set rs = Nothing
   Set db = Nothing
Question by:gtmathewDallas
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
  • 7
  • 4
  • 2
LVL 37

Assisted Solution

PatHartman earned 50 total points
ID: 39954603
Try creating a second query with the necessary selection criteria and join to it using a Left Join rather than using a subquery.  Joins are sometimes more efficient than subqueries.
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 39954662
Your problem is the Not IN clause in your SQL statement.  IN clauses are notoriously slow.  Try:

strSQL1 = "SELECT  * FROM Employee "
               & "LEFT JOIN (" _
               & "SELECT person_number from Employee_Room_Status " _
               & "WHERE Employee_Room_Status.Employee_Active = -1 " _
               & " AND Employee_Room_Status.Room_ID = " & Me.Combo_Room.Value _
               & ") as Temp " _
               & "ON Employee.Person_Number = Temp.Person_Number " _
               & "WHERE Temp.Person_Number IS NULL"

Is there a reason for the value list rather than using this as the RowSource for a listbox with a RowSourceType of 'Table/Query'?

Author Comment

ID: 39954776
It still showing the delay, the noticed thing is - this problem only happening after the table update.
Is that because of not refreshing the table? Its not showing any issues to load the contents, when we close and re open the database.
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

LVL 47

Assisted Solution

by:Dale Fye (Access MVP)
Dale Fye (Access MVP) earned 450 total points
ID: 39954856
As I asked before, is there a reason that the RowSourceType must be a ValueList, and not Table/Query.  That query should run relatively quickly if you rewrote it like:

SELECT  [Last_Name], [First_Name]. [Middle_Initial[, [Person_Number]
FROM Employee
SELECT person_number
FROM Employee_Room_Status
WHERE Employee_Room_Status.Employee_Active = -1
AND Employee_Room_Status.Room_ID = [Forms]![YourFormName].Combo_Room
) as Temp
ON Employee.Person_Number = Temp.Person_Number
WHERE Temp.Person_Number IS NULL

If you save that query, you can set the RowSourceType to Table/Query and set the RowSource to that query, and simply requery the listbox whenever something changes.

Author Comment

ID: 39956122
I am sorry, I missed to answer that question, Yes, I have to compare two list boxes (to add/remove from one to another), so using the value list.

I will check with the above query now
Thanks a lot
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 39956128
I do something similar with queries, and avoid all of the additional overhead of having to clear the list and add items to the list.
LVL 37

Expert Comment

ID: 39956137
I also use queries.  As data is "moved" from one listbox to the other, data is updated and the queries rerun.

Author Comment

ID: 39956216
Getting - Error 6014 - row source must be value list to use this method - when I am trying to move items from this list(List1Edit) to other one(List2Edit) - or moving from other one to this list box.

Author Comment

ID: 39956226
I used the below  method to move items to and fro
LVL 47

Accepted Solution

Dale Fye (Access MVP) earned 450 total points
ID: 39956257
I use a similar technique, but I use the tables and insert and delete SQL operations to add items from one list to another.

I don't have a sample readily available, but If I can find one or whip one up, I'll try.

Author Comment

ID: 39956287
Thank you so much, I really appreciate your help...

Author Comment

ID: 39957318
I am using now the query as row source of this list box now, but to update the table need the values from the list,
Is there any way to get all column values (i have 4 columns in my list)  of a selected row in a list box? I think that will help me to finish this task.

Author Comment

ID: 39957438
Yes I got it, Thanks so much fyed  and PatHartman

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

734 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