?
Solved

Data from database to VB Drop Down list / list box

Posted on 2006-03-21
7
Medium Priority
?
254 Views
Last Modified: 2010-04-07
I have a form load, I want to have a list box in this form that pulls data from my database table.
I know how to connect to Database and pull the data in a recordset but I dont know how to put those data in my List box.

 sSQL = "Select ProjectId, ProjectDescription from projects"
 Set rs = Conn.Execute(sSQL)
       
Whats next?
0
Comment
Question by:AutomaticSlim
  • 3
  • 3
7 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16250533
You have to manually loop through the dataset and populate the listbox.

Something like:

    While Not rs.EOF
        listBox.AddItem rs("ProjectID")
        rs.MoveNext
    Wend


Hope this helps.
0
 

Author Comment

by:AutomaticSlim
ID: 16250720
That worked, but do you know how to show the 'ProjectDescription' in the list but when we select and pass the value to send  the 'ProjectId'.

Just like in HTML when we have drop down menu... can we do that here?
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 200 total points
ID: 16250786
You need to set the ItemData for the corresponding item:

    While Not rs.EOF
        listBox.AddItem rs("ProjectID")
        listBox.ItemData(listBox.ListCount - 1) = rs("ProjectID")
        rs.MoveNext
    Wend


You would then retrieve the ID for the selected item like:

    ID = listBox.ItemData(listBox.ListIndex)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:AutomaticSlim
ID: 16250889
I was playing around and I think what I need is a ComboBox.
Does combobox work the same way?


        Combo1.AddItem rs("ProjectID")
        Combo1.ItemData(listBox.ListCount - 1) = rs("ProjectID")
        rs.MoveNext
0
 

Author Comment

by:AutomaticSlim
ID: 16251339
Ok, That did with the combo box. The only problem was that I got an error on this line:
Combo1.ItemData(listBox.ListCount - 1) = rs("ProjectID")

and that was because my 'ProjectID' its not numeric. Temperary I changed it to Int. But is there any way to make it work with AlphaNumeric?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16251399
Unfortunatley not, it actually works with long under the hood but can do the implicit cast to int.

What you would have to do is maintain an array withyour project ID's in an sync it with the index positions of the combo.

So item 1 in the combo would map to item 1 in the array.
0
 
LVL 4

Expert Comment

by:g_johnson
ID: 16251435
you can also add a "padded" item to a list box such that what you want visible stays visible and any "key" you want to maintain is off the screen, but available, e.g.,

lst1.additem strVisible & space(100) & strKey

if you format such that your strVisible is always X characters and strKey is always Y characters, then your values are:

main value    left(lst1.list(i),X)
key value      right(lst1.list(i),Y)

0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month14 days, 23 hours left to enroll

839 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