473,398 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

How to populate a vba listbox or a vba comboBox with data from Access?

I'm trying to build a very large database that is calling on elements of Microsoft word, excel, infoPath, and access.

The main program that is controlling everything is excel, which I'm very experienced in, using VBA and VB forms. Unfortunately, I don't have a lot of experience in access, and I believe I need it now.

I'm storing information about users of the system in an access file that consists of three vertical fields: Username, Password, and System Rights.

How can I program VBA to read information from the database file {called UserAccess.accdb} and process it into a comboBox {called userComboSelect} and a listBox {called listBoxDisplay}? I thought it would be similar to referencing data in excel, but I'm sadly mistaken.
Feb 10 '11 #1
1 6629
MikeTheBike
639 Expert 512MB
Hi

Below is an extract of code to do just what you want although this is a Combobox, but it works for s listbox.
Expand|Select|Wrap|Line Numbers
  1. Sub UserForm_Initialize()
  2.     Dim i As Integer
  3.     Dim cn As New ADODB.Connection
  4.     Dim rs As New ADODB.Recordset
  5.     Dim ConnectionString As String
  6.     Dim WIPArray() As String
  7.  
  8.     ConnectionString = "File Name=H:\My Data Sources\CES Maintenance KPI DB DEV.udl"
  9.  
  10.     cn.Open ConnectionString
  11.     rs.Open "SELECT ID, ClientName FROM tblClients ORDER BY ClientName", cn, adOpenStatic, adLockReadOnly
  12.  
  13.  
  14.     ReDim WIPArray(rs.RecordCount - 1, 1)
  15.     For i = 0 To rs.RecordCount - 1
  16.         WIPArray(i, 0) = rs(0)
  17.         WIPArray(i, 1) = rs(1)
  18.         rs.MoveNext
  19.     Next i
  20.     rs.Close
  21.  
  22.     Me.cboClient.List = WIPArray
  23.  
  24. End Sub
All you need now is a UDL file to gererate the connection string. This need to be in a system directory that anyone using the spreadsheet has access to.

To create a UDL file :-

1. Create a blanf txt file,(with notepad or simlar).
2. Change the extention from .txt to .udl (ignore any warnings!).
3. Double clic the udl: a dialogue box opens.
4. elect the Prover (Provider Tab)
5. Browse to the .accdb file ans select.
6. Check connection ans save (OK).

This is the file/loction to be used for the connection string in the obove code.

I assume you will be able to mod the SQL string to suit your DB.

My only disclaimer to all this is I do not use Access 2007 (or late).

HTH, but if any probs, plaes post back.

There is an article on UDLs by ADezii here
http://bytes.com/topic/access/insigh...data-link-file
There a other ways of specifying a connection string, but, IMHO, this the easiest, paticularly for a the inexperianced


MTB

ps: you will also need to set a reference to Microsofts ActiveX Data Object library. (Tools -> References...).
Feb 10 '11 #2

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

Similar topics

0
by: luci | last post by:
Hello I've developed several data access pages and I searched for the same problems in different internet pages but I never got back a usable answer. Here are my questions: 1) How is it...
1
by: Grant Hammond | last post by:
I have a callback function that populates a listbox with a list of file names in a folder. This code came from Terry Kreft/Dev Ashish some years ago. I now want to add another column to the list...
4
by: Oyvind | last post by:
I'm working on a Windows forms/C# database application. My background is 6-7 years of VB 4 - 6, MS Access, VC++, mixed in with a lot of T-SQL and MS SQL Server in general and some OOA/OOD. ...
2
by: amber | last post by:
Hello I'm using the Data Access Application Block in my code I'm trying to populate a list box It works right up to the last step - displaying the datamember Can someone tell me where the problem...
2
by: Sean Holt via DotNetMonster.com | last post by:
In my application I have about 15 comboboxes and checkboxes scattered throughout 5 tabs. After I populate the combobox and try to null it out (by just highlighting and deleteing), the data...
0
by: masterej | last post by:
I'm trying to populate a ComboBox (dropdown style) with strings of text from an ArrayList. The ComboBox is actually being populated with the correct items, however, they appear only as blank...
2
by: rperreta | last post by:
I exported a xml file using .writexml from a dataset and now I would like to populate a combo box from that. Can someone supply a link or sample vb.net code that show's how to do this... much...
7
by: samoore33 | last post by:
I am trying to dynamically add items to a listbox or combobox. The items add to either, but when I look through those items, there is nothing there. If I choose an item, it shows up. Not sure...
4
by: polaris431 | last post by:
All the examples I've seen showing how to populate a combobox using the DataSource property and an ArrayList show the ArrayList object containing objects with at least two properties. I want to...
2
by: alinagoo | last post by:
Hello all I'm working on a database application with MS-Access 2007 and now i have a problem with dynamic report creation process! I need to guess the data type of a ListBox! This my problem. The...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.