populate a combobox with an array from VBA

M

martinmike2

Hello:

I am trying to populate a combobox with and array as its rowsource,
but am having issues. Mainly, im not sure how I would accomplish
this. Here is my code:

Private Sub Form_Load()
Dim db As DAO.Database, rst As DAO.Recordset
Dim ws As DAO.Workspace, usr As DAO.User, grp As DAO.Group, allgrp As
DAO.Groups

Set ws = DBEngine(0)
Set allgrp = ws.Groups
Me.cboGroups.RowSource allgrp
End Sub

anyhelp would be appreciated.
 
D

Douglas J. Steele

Try:

Private Sub Form_Load()
Dim db As DAO.Database, rst As DAO.Recordset
Dim ws As DAO.Workspace, usr As DAO.User
Dim grp As DAO.Group, allgrp As DAO.Groups

Set ws = DBEngine(0)
Set allgrp = ws.Groups
Me.cboGroups.RowSourceType = "Value List"
For Each grp In allgrp
Me.cboGroups.AddItem grp.Name
Next grp

End Sub

or

Private Sub Form_Load()
Dim db As DAO.Database, rst As DAO.Recordset
Dim ws As DAO.Workspace, usr As DAO.User
Dim grp As DAO.Group, allgrp As DAO.Groups
Dim strData As String

Set ws = DBEngine(0)
Set allgrp = ws.Groups
Me.cboGroups.RowSourceType = "Value List"
For Each grp In allgrp
strData = strData & grp.Name & ";"
Next grp
strData = Left(strData, Len(strData)-1)
Me.cboGroups.RowSource = strData

End Sub
 
D

Danny Lesandrini

I've some something similar to this by creating an ADODB Recordset, loading
it and then slamming the recordset into the control. Below is an example,
albiet a mindless one. I usually load the recordset from another recordset
or array.


Function foo()
On Error Resume Next

Dim rstTemp As New ADODB.Recordset
Dim fld As ADODB.Field

' create a temp recordset with the correct fields
With rstTemp
.Fields.Append "ContactID", adInteger
.Fields.Append "ContactName", adVarChar, 64
.Fields.Append "Active", adBoolean
.CursorLocation = adUseClient
.LockType = adLockOptimistic
.Open
End With

' load the records
With rstTemp
.AddNew
!ContactID = 0
!ContactName = "Mickey Mouse"
!Active = True
.Update

.AddNew
!ContactID = 1
!ContactName = "Lamont Cranston"
!Active = False
.Update

.AddNew
!ContactID = 3
!ContactName = "Henry Hudson"
!Active = True
.Update
End With

' slam it into the recordset
Set Me!cboContact.Recordset = rstTemp

' clean up
Set rstTemp = Nothing

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top