I'm a newbie on Access and I'm trying to find an alternative (and more efficient) solution to my idea. I have a table with multiple fields and I'm interested in retrieving only one field (text values).
My goal is to create a combobox that shows all the values in that field (without duplicate) and I've managed to do it by linking the combobox to a manually-created query that eliminate duplicate for that field.
Here's an example to make it more clear:
Field 1 Combobox
AAA AAA
AAA BBB
BBB CCC
BBB
CCC
CCC
CCC
How can I have the same result through VBA? Of course it has to be dynamic for any record change in field 1.
Set the Row Source property of your Combobox to:
select distinct [Field 1] from YourTable
If you want it done by VBA, however the answer by @Lee Mac is more easy, you can use the form load event to trigger vba code like the following:
Private Sub Form_Load()
Dim rst As DAO.Recordset
Dim strRowSource As String
Dim strSQL As String
Dim dbs As DAO.Database
Set dbs = CurrentDb
strSQL = "SELECT DISTINCT rowname FROM tablename"
Set rst = dbs.OpenRecordset(strSQL)
If Not (rst.EOF And rst.BOF) Then
rst.MoveFirst
Do Until rst.EOF = True
strRowSource = strRowSource & rst!vehicle_name & ";"
rst.MoveNext
Loop
With Me.ComboBoxname
.RowSourceType = "Value List"
.RowSource = strRowSource
End With
End If
rst.Close
Set rst = Nothing
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.