How do I create a combo box to display the caption for the fields of the form source table ? (1 Viewer)

Falcon88

Registered User.
Local time
Today, 03:38
Joined
Nov 4, 2014
Messages
299
Hii all dears
How How do I create a combo box to display the caption for the fields of the form source table ?
 

ebs17

Well-known member
Local time
Today, 02:38
Joined
Feb 7, 2020
Messages
1,955
Use RowSourceType FieldList.
 

MajP

You've got your good things, and you've got mine.
Local time
Yesterday, 20:38
Joined
May 21, 2018
Messages
8,556
Maybe there is an easier way
The issue is that the Caption is a property of a table or query def. But it is a user defined property, which means it does not exist in the properties collection until you define it. So if you try to check the Caption property you will get an error on any field without a caption.


Code:
Me.Combo12.RowSource = GetCaptions("categories", dt_tableDef)


Code:
Public Enum DefType
  dt_tableDef = 1
  dt_queryDef = 2
End Enum

Public Function GetCaptions(Domain As String, TheDefType As DefType) As String
  Dim fld As DAO.Field
  Dim def As Object
  Dim db As DAO.Database
  Set db = CurrentDb
  Select Case TheDefType
    Case dt_tableDef
         Set def = db.TableDefs(Domain)
    Case dt_queryDef
       Set def = db.QueryDefs(Domain)
  End Select
  For Each fld In def.Fields
    If GetCaptions = "" Then
      GetCaptions = GetCaption(fld)
    Else
      GetCaptions = GetCaptions & ";" & GetCaption(fld)
    End If
  Next fld
End Function
Public Function GetCaption(fld As DAO.Field)
   On Error GoTo errlbl
  GetCaption = fld.Name
  GetCaption = fld.Properties("caption")
 
  Exit Function
errlbl:
  If Err.Number <> 3270 Then Debug.Print Err.Number & " " & Err.Description
End Function
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 20:38
Joined
Feb 19, 2002
Messages
43,396
Are you talking about the Column Heads property on the Format tab of the list/combo
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 17:38
Joined
Oct 29, 2018
Messages
21,510
Hii all dears
How How do I create a combo box to display the caption for the fields of the form source table ?
Some say using the Caption property can cause confusion when writing code, and it's recommended to use an Alias instead.
 

Users who are viewing this thread

Top Bottom