Avatar of AndrewBanfer
AndrewBanfer
 asked on

Open an Access Database and Fill a ListBox in VB.Net Code

How do I open an Access Database and Fill a ListBox in VB.Net Code
Visual Basic.NETDatabases

Avatar of undefined
Last Comment
PatHartman

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Scott McDaniel (EE MVE )

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
AndrewBanfer

ASKER
Thanks Scott!
SOLUTION
PatHartman

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
AndrewBanfer

ASKER
Thanks Pat!  I'll give it a try.
Scott McDaniel (EE MVE )

Pat: This is VB.NET, not Access. .NET listboxes don't have properties like RowSource.

You can bind a Listbox to a datatable:

Dim dt As New Datatable
dt.Load(cmd.ExecuteReader)
YourListBox.Datasource = dt
YourListBox.DisplayMember = "SomeField"
YourListBox.ValueMember = "SomeField"

Also, the method I suggested would not require anyone to "remember to run an update procedure should the list ever need to change". It would update AUTOMATICALLY (caps intended). The method I suggested gets data from a database, so if the information in that database changes, the list would show that new data. Granted you may need to refresh after a data update, but you'd have to do the same if you were using an Access listbox and you added new data.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
PatHartman

@Scott,
Please excuse my confusion.  I realize the question asked how to do this with .Net code but Andrew seemed to want the solution to affect the actual Access application rather than a .Net form connected to Jet/ACE.  I prefer your second solution to the first if in fact this question is related to a .Net form.

@Andrew,
If you are not working with an Access application, technically you are using Jet (.mdb) or ACE (.accdb) as a database engine and you do not even need to have Access installed unless you want to use the Access GUI to act as the SSMS for the Jet/ACE database.  Your client PCs never need Access installed unless you want to allow them to modify the schema.   Without Access installed, All you need to have installed is the Jet or ACE database engine which is free.  you would simply use ADO or DAO to create/modify schemas if you didn't have Access the RAD tool installed for you to use as a schema maintenance GUI.  When using Jet/ACE as a datastore, for .Net or any other FE application, you treat the Jet/ACE database exactly as you would any other RDBMS.  You need to write your queries using "Access" SQL Syntax and you need to figure out the correct connection string but otherwise, ODBC is ODBC.
Scott McDaniel (EE MVE )

The second method still requires you to open a connection and command object, so the only thing you save is the loop. It's more a matter of preference than anything else. There are other ways to do it as well - create a BindingSource, or a Class, or an Array. Each has their own pros and cons, and none are "better" than any other.

Not sure how a discussion of what ACT/JET is, or how it's installed, or how you work with schema changes, is relevant.
PatHartman

It's relevant because Andrew thinks he is working with Access and you think (and I do as well) that he is not working with Access.  He is in fact working with Jet or ACE (we don't know which) and Access doesn't even need to be loaded to work with Jet and ACE unless you want to use the Access GUI to manage the database schema much as you would use SSMS to manage the schema for SQL server.  People who don't develop with Access tend to think that Jet/ACE ARE Access and so confusion reigns.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.