我在VBA MS-Access中有以下代码。下面的代码用文本填充compbobox。
Private Sub PopulateEmployers()
With Me.cmbEmployer
.RowSourceType = "Value List" 'Set rowsource type as Value list
.RowSource = "" 'Clean combo contents
End With
Dim adoRS As ADODB.Recordset
Set adoRS = CurrentProject.Connection.Execute("SELECT * from tblEmployers;")
Do While Not adoRS.EOF
Me.cmbEmployer.AddItem (adoRS!UserName)
adoRS.MoveNext
Loop
End Sub
问题:有没有办法填充Text和ID?问题是,当我检查cmbEmployer.Value
时,它会提供文字,而不是ID
答案 0 :(得分:1)
您可以直接将Combo的RowSource设置为SQL命令。
With Me.cmbEmployer
.ColumnCount=2
.RowSourceType = "Table/Query"
.RowSource = "SELECT ID, UserName from tblEmployers;"
End With
刷新清单:
Me.cmbEmployer.Requery