473,505 Members | 13,696 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Filling A Listbox with a Query in VBA

17 New Member
Hi All,

I am new to Access programming and it has been a long time since I have done any programming at all, so I am a bit rusty. I am building a form in Access 2002 that displays a list of Customers.

When the form first opens, all Customers are displayed in a Listbox with the Address1, Address2, City, State, Zip, and Customer Number in Textboxes that populate when you select the customer. The listbox has the property "Row Source" set to "qryFillListBox."

This form has a search box and option buttons to search on Name, Address, City, State, or Zip. I cannot figure out how to connect my query to fill the listbox. Here is the code I have so far:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSearch_Click()
  2.  
  3. strSQL As String
  4.  
  5. strSQL = "SELECT CustomerMasterTemp.CUSTOMERSYSTEMID, CustomerMasterTemp.CUSTOMERNAME, CustomerMasterTemp.ADDRESS1, CustomerMasterTemp.ADDRESS2, CustomerMasterTemp.CITY, CustomerMasterTemp.STATE, CustomerMasterTemp.POSTALCODE FROM CustomerMasterTemp "
  6.  
  7. Select Case Me.optSearchIn
  8. Case 1
  9. strSQL = strSQL + "WHERE CustomerMasterTemp.CUSTOMERNAME LIKE '%" & Me.txtSearch & "%' "
  10. Case 2
  11. strSQL = strSQL + "WHERE CustomerMasterTemp.ADDRESS1 LIKE '%" & Me.txtSearch & "%' or CustomerMasterTemp.ADDRESS2 LIKE '%" & Me.txtSearch & "%' "
  12. Case 3
  13. strSQL = strSQL + "WHERE CustomerMasterTemp.CITY LIKE '%" & Me.txtSearch & "%' "
  14. Case 4
  15. strSQL = strSQL + "WHERE CustomerMasterTemp.STATE LIKE '%" & Me.txtSearch & "%' "
  16. Case 5
  17. strSQL = strSQL + "WHERE CustomerMasterTemp.POSTALCODE LIKE '%" & Me.txtSearch & "%' "
  18. End Select
  19.  
  20. strSQL = strSQL + "ORDER BY CustomerMasterTemp.CUSTOMERNAME"
  21.  
  22. ......... THIS IS THE PART I'M NOT SURE OF ...........
  23.  
  24. End Sub
  25.  
I want to run the query, clear the list box, and put the new data into the listbox.

I would appreciate your help.

Thanks,

BPLANTES
Feb 21 '08 #1
2 9754
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Hi BPlantes. All you need now is to set the rowsource property for your listbox to be strSQL then requery the listbox to fill it. Add
Expand|Select|Wrap|Line Numbers
  1. [listbox control name].RowSource = strSQL
  2. [listbox control name].Requery
substituting for[listbox control name] with the real name of your control.
-Stewart

** Edit **
Fixed using the [ CODE ] tags (and adding an extra CRLF).
Removed the comment explaining (I get what you meant now though ;)).
Feb 21 '08 #2
bplantes
17 New Member
This worked great! Thanks for the help. Trying to get back into the developer's mindset :)
Feb 26 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
367
by: JMorrell | last post by:
I’m trying to populate a listbox with data that comes from my SQL server. I’ve created the connection, filled the dataset and loaded the data adapter. Apparently, I’m missing something...
3
4817
by: louise | last post by:
hi i am trying to set up a mail merge button which takes records from a multi-select listbox (the contents of which are decided by a query created by a search from) and not from a specific...
2
3665
by: Simon P | last post by:
Hello group, I'm in desperate need of help. Here goes : I have the following tables : CONTACTS (ContactID, FirstName, LastName, Company, etc.), SHOWS (ShowID, ShowDescription) and SHOWDETAILS...
2
4561
by: Geir Baardsen | last post by:
Hi! From a listbox I'd like to send only selected items to a report. Items will include: OrderNr,Date,EmployeeNr from tblOrders ZipCode,City from tblZipCodes Name,Adr,ZipID from...
4
5347
by: carl.barrett | last post by:
Hi, I have a list box that displays 2 columns. Behind it sits a query with five columns. These are Column1 (DOB), column2 (a concatenated string of Surname Forname, Title), Column3 (Surname),...
6
17139
by: Brian | last post by:
Hello, Basically, I'm running a query on a form's activation, and I'd like to have the results of the query be placed into other fields on the same form automatically. Does anybody know how...
1
2069
by: lance2001 | last post by:
Hi, After viewing the entire Visual Basic 2005 Express Edition for Beginners video series, I have begun building a database driven application that will make use of listbox1 (multi-select)...
5
7404
by: markr1000 | last post by:
I must have looked searched in 500+ places that showed up in Google searchs, but not one has an example of what I want to do. I have a Listbox on a User Control because I want to control the...
20
5185
by: exipnakias | last post by:
Hello Guys. In a form I created a listbox which looks up the values of a table. I want: 1) ..to create a query where a parameter will be needed in order to be loaded. But I do not want to...
0
7213
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7298
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4698
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
406
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.