• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 380
  • Last Modified:

Populate combo box with column names of table

I need to populate a combo box (combo31) with the column names of the table chosen in another combo box (combo1).  I can go into the properties of Combo31 and choose a row source type of "Field List" and then enter a table name for the Row Source but that leaves the column names only being populated by the table entered.  I need something that displays the column names based on the value chosen in combo1.  I tried things like entering "me.combo1.value" in the Row Source box but no luck.

Any ideas???  Thanks!
0
ajslentz
Asked:
ajslentz
  • 2
  • 2
1 Solution
 
Rey Obrero (Capricorn1)Commented:
try this

cboRefTable is the combo where you select the table Name
cboFieldName is the combo that will list the names of the fields of the table selected


set the row source of cboRefTable to

SELECT MSysObjects.Name, MSysObjects.Type
FROM MSysObjects
WHERE ((MSysObjects.Name) Not Like "msys*") AND ((MSysObjects.Type)=1)



Private Sub cboRefTable_AfterUpdate()
Dim tdf As TableDef, strField As String, db As DAO.Database
Dim intFld As Integer, strFieldList As String
Set db = CurrentDb
Set tdf = db.TableDefs("" & Me.cboRefTable & "")
With tdf
    For intFld = 0 To .Fields.Count - 1
        strField = .Fields(intFld).Name
        strFieldList = strFieldList & ";" & [strField]
    Next
End With

If Len(strFieldList) > 0 Then
    strFieldList = Mid(strFieldList, 2)
End If
    Me.cboFieldName.RowSourceType = "Value List"
    Me.cboFieldName.RowSource = strFieldList
End Sub
0
 
ArjiCommented:
Depending on what column in Combo1 has the value(assume it's in column(0)

[Combo31].Rowsource = "Select field1, field2 from YourTable where field1 = " & [Combo31].Column(0)

Field1, field2 are generic field names you would need to replace with your field names.  Field1 is used here as the field in which you want to sort [Combo31] by.  [Combo31].Column(0) is the value in Column(0) of the combobox.
0
 
ArjiCommented:
Also, you should set the Row Source Type of [Combo31] to Table/Query and set up your column widths and count.
0
 
ajslentzAuthor Commented:
capricorn1, that was AWESOME!!!  Thanks so much.  Here is what I used...  Thanks again!!!

Private Sub Combo1_AfterUpdate()
Me.Combo31 = ""
Dim tdf As TableDef, strField As String, db As DAO.Database
Dim intFld As Integer, strFieldList As String
Set db = CurrentDb
Set tdf = db.TableDefs("" & Me.Combo1 & "")
With tdf
    For intFld = 0 To .Fields.Count - 1
        strField = .Fields(intFld).Name
        strFieldList = strFieldList & ";" & [strField]
    Next
End With

If Len(strFieldList) > 0 Then
    strFieldList = Mid(strFieldList, 2)
End If
    Me.Combo31.RowSourceType = "Value List"
    Me.Combo31.RowSource = strFieldList
End Sub
0
 
Rey Obrero (Capricorn1)Commented:
U R welcome!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now