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!

Using Active Directory List as Combo Box Source

Status
Not open for further replies.

CharlieT302

Instructor
Mar 17, 2005
406
US
Hi Folks,

I have the following command code that extracts the first and last names of all users in the Active Directory. It displays the names in the Immediate Window. However, I need to pull them into a combo box as the record source.

I am thinking that I need a Select statement and probably not a Debug.Print command. I am unsure how to set it up. Any ideas?

Current Code:

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")


objConnection.Open "Provider=ADsDSOObject;"
objCommand.ActiveConnection = objConnection

objCommand.CommandText = _
"<LDAP://dc=ad,dc=Oggwin,dc=org>;(objectcategory=user);sn,givenname;subtree"

Set objRecordset = objCommand.Execute
While Not objRecordset.EOF
Debug.Print objRecordset.Fields("givenname") & "," & objRecordset.Fields("sn")


objRecordset.MoveNext
Wend


objConnection.Close
 
don't remember from what version but access listbox has a recordset property

try
Code:
Set me.listbox.recordset =objCommand.Execute
 
This is the old style
Code:
Option Compare Database

Function AddItemsToCombo(ByVal cmb As ComboBox)
cmb.RowSourceType = "Value List"
    For x = 0 To 10
        cmb.AddItem (x)
    Next
End Function

Private Sub Form_Load()
   Call AddItemsToCombo(Me.Combo0)
End Sub

Zameer Abdulla
 
Hi guys,

I really appreciate the input. I am a little confused. How do I make the suggested changes? Are they in addition to my current code? For example, PWise's suggestion: how does that retrieve the Active Directory names that my code is pulling?

Also, I have been given two tasks.
1.Use the results as the record source for a list/combo box. This was my original question.
2.Send the results to a query. Can this be done.


Thanks again for any help.
 
Code:
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")

 
objConnection.Open "Provider=ADsDSOObject;"
objCommand.ActiveConnection = objConnection
 
objCommand.CommandText = _
"<LDAP://dc=ad,dc=Oggwin,dc=org>;(objectcategory=user);sn,givenname;subtree"
 
Set objRecordset = objCommand.Execute


[COLOR=red]Me.ComboBox1.RowSourceType = "Value List"[/color]

While Not objRecordset.EOF
[COLOR=green] 'Debug.Print objRecordset.Fields("givenname") & "," & objRecordset.Fields("sn")[/color]
[COLOR=red] Me.ComboBox1.AddItem (objRecordset.Fields("givenname") & "," & objRecordset.Fields("sn"))[/color]
 
objRecordset.MoveNext
Wend
 
 
objConnection.Close

Zameer Abdulla
 
Thank you Zameer,

I truly appreciate your help.

But tell me, how do I execute this code? Is it attached to the RecordSource of the combo box?

Also, is it possible to send the results of this code to an Access query?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top