I want to build a form with a combobox that contains all the names of the queries i've got in my database. But since i've got over 80 queries i don't want to fill the combobox manually. Is there a way by using VBA to fill the combobox?
You could try this in the open event for the Form. Change the name of the combo box to your combo name.
Paul
Dim qdf As DAO.QueryDef
Dim strHolder As String
For Each qdf In CurrentDb.QueryDefs
strHolder = strHolder & qdf.Name & "; "
Next
Me.ComboName.RowSourceType = "Value List"
Me.ComboName.RowSource = Left(strHolder,Len(strHolder)-2)
There is a somewhat easier way to do this. Set the list box, or combo box RowSource Type to Table/Query and use the following query:
Select Name From MSysObjects Where Type = 5 And Name Not Like "~*"
This will bring back a list of query names. For the fields set the RowSource Type to Field List and set the rowsource to the table or query you want the fields listed from. If you want to make it dynamic, the let the user double click on the query they want to see the fields for and use the doubleclick event to populate the other list box's rowsource with the name clicked.
Jeff, i found a more elaborate function that, i think should do the same as your solution.
But with your solution i end up with a empty combobox, but the function works.
This is the function:
Function ReportsList(Optional strLikeReportName As String = "", Optional strReportList As String = "", Optional intStartPos As Integer = 1, Optional strContainer As String = "Reports" As String
On Error GoTo Err_ReportsList
'strLikeReportName - part of report name which is same all needed reports
'strReportList - exist listbox rowsource (text such "rptMyReport";"This is my report" what you want to keep in the new rowsource
'strContainer="Forms" for forms (must be included in called command)
Dim rst As Recordset
Dim strSQL As String
Dim strWhere As String
Dim strDescription As String
Dim intLenght As Integer
Dim prp As Property
strSQL = "SELECT MSysObjects.Name FROM MSysObjects "
If strContainer = "Reports" Then
strWhere = "WHERE MSysObjects.Type = -32764 "
Else
strWhere = "WHERE MSysObjects.Type = -32768 "
End If
If strLikeReportName <> "" Then
strWhere = strWhere & "And Mid(Name," & intStartPos & "," & intLenght & " = '" & strLikeReportName & "' "
End If
strSQL = strSQL & strWhere & "ORDER BY MSysObjects.Name;"
Set rst = CurrentDb.OpenRecordset(strSQL)
If Not rst.EOF Then
While Not rst.EOF
'strDescription = CurrentDb.Containers(strContainer).Documents(rst(0)).Properties("Description".Value
If strReportList <> "" Then
'strReportList = strReportList & ";"
End If
strReportList = strReportList & rst(0) & ";" '& strDescription
rst.MoveNext
Wend
End If
rst.Close
Set rst = Nothing
ReportsList = strReportList
Exit_ReportsList:
Exit Function
Err_ReportsList:
If Err.Number = 3270 Then 'Property not found.
MsgBox "You need to write descriptions of " & LCase(strContainer) & "!", vbCritical, "Property not found"
ReportsList = "Property not found"
Else
MsgBox "Error No " & Err.Number & vbLf & Err.Description, , "Public Function ReportList"
End If
Resume Exit_ReportsList
End Function
I disabled the description because i don't need it.
I call the function like this:
myList = ReportsList("frm", , , "Forms"
I use this method and it certainly works. Do you get any error message or just a blank box? I just tried it again in another db and got back the names of all of my forms.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.