Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to populate a drop-down list

Status
Not open for further replies.

gogetsome

Technical User
May 27, 2004
21
US
Hello, I have a form with a drop-down list that I would like to have populated with data from a database. Can someone show me an example of how this is accomplished or point me to a tutorial?

Much Thanks!
 
Without pointing you directly you will probably need to look at 2 tutorials / faq's.

1. For retreiving the data from a adatabase, perhaps using ADO
&
2. Populating a combo box (actually really simple, just using the .AddItem method of the combo box object.

Matt
 
Thanks niceguymattx for pointing me in the right direction. I'm learning.

I think that I'm on the right track but it's not working. I'm getting an error that the varible at: ReDim Preserve aryCodeList(0, lngIndex) is not declared. I admit that Im new to this and following an example. I'm also unsure if I have the code in the right location. I want the drop-down list to populate on page load. Could you please advise?

Here is the code:

Private Sub Window_AfterActivate()
Dim conn As New ADODB.Connection
conn.Open "Provider=sqloledb;Data Source=localhost;Initial Catalog=BBTN;User Id=xxx;Password=xxx"

Dim RecordSet As New ADODB.RecordSet
RecordSet.Source = "select RMDNUMWK from SV00564"
Set RecordSet.ActiveConnection = conn
RecordSet.Open
If RecordSet.EOF = True Then GoTo BYE
Dim lngIndex As Long
lngIndex = -1

Do Until RecordSet.EOF
lngIndex = lngIndex + 1
ReDim Preserve aryCodeList(0, lngIndex)
aryCodeList(0, lngIndex) = grsPrimary.Fields("RMDNUMWK")
RecordSet.MoveNext
Loop
RecordSet.Close
invBegNum.Value = RecordSet
End Sub





 
1. Use Option Explicit and declare the variable
2. Once you have your array, use .AddItem to populate the combo.

Gerry
 
Like I mentioned, I'm still learning and am unsure of how to accomplish what you wrote in your post.

Use option explicit is just adding that in the general section.

I was following an example so I do not understand completely this section of code:
ReDim Preserve aryCodeList(0, lngIndex)

How do you use the .additem with the combobox to get the recordset?
 
Option Explicit forces you to declare your variables. You need to declare aryCodeList as a variable.

Look up Ubound and LBound in Help. You need to loop through the array, taking each item in the array and using Combobox.AddItem array item to add to the combo.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top