smtazulislam
Member
- Local time
- Tomorrow, 02:49
- Joined
- Mar 27, 2020
- Messages
- 808
Hi, There have any ways to display multiple table Name (Table1, Table2, Table3) within there Field (Field1, Field2, Field3) in the combo box list,
But Table name is readonly/Header, its can't select to insert.
Table1
Field1
Table2
Field2
Table3
Field3
This line need to do something,
Setting the Form,
LimitToList: Set this property to Yes.
ColumnCount: Set this property to 1
BoundColumn: Set this property to 0.
But Table name is readonly/Header, its can't select to insert.
Table1
Field1
Table2
Field2
Table3
Field3
Code:
Private Sub Combo0_AfterUpdate()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Dim strItem As String
Dim currentTable As String
Dim isFirstTable As Boolean
' Define your SQL query to get field names from multiple tables
strSQL = "SELECT 'Table1' AS TableName, Field1 AS FieldName FROM Table1 " & _
"UNION SELECT 'Table1', Field1 FROM Table1 " & _
"UNION SELECT 'Table2', Field2 FROM Table2 " & _
"UNION SELECT 'Table3', Field3 FROM Table3 " & _
"ORDER BY TableName" ' Ensure the records are sorted by table name
Set db = CurrentDb
Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)
' Loop through the recordset and add items to the combo box
Do While Not rs.EOF
If currentTable <> rs("TableName") Then
' Add a separator to distinguish different tables
If Not isFirstTable Then
Me.Combo0.AddItem "-----------------------"
Else
isFirstTable = False
End If
' Store the current table name
currentTable = rs("TableName")
' Add the table name as a non-selectable header
Me.Combo0.AddItem rs("TableName")
End If
'Here neeed some code adding,
' Add field name as an item with left-side gap
Me.Combo0.AddItem " " & rs("FieldName")
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
Set db = Nothing
End Sub
This line need to do something,
Code:
Me.Combo0.AddItem " " & rs("FieldName")
LimitToList: Set this property to Yes.
ColumnCount: Set this property to 1
BoundColumn: Set this property to 0.