Microsoft access question
#1
Microsoft access question
I have written the following piece of code to extract field names from any table specified by the user.
Function ShowFieldNames(TableName As String)
Dim I As Integer
Dim FieldList As TableDef
Set FieldList = DBEngine(0)(0).TableDefs(TableName)
For I = 0 To FieldList.Fields.Count - 1
'Debug.Print FieldList.Fields(I).Name
Next I
End Function
I would like the list of fields returned to a combo box or list box on a form. Can anyone help. Please Ignore the line in red - this was inserted to return results when debugging in immediate mode. The solution I suspect is staring right at me but I am damned if I can resolve it!!! Am a bit rusty with Access / VBA.
I would reiterate it is the FIELD NAMES I want in the combo box - not the data they contain.
Thanks in advance,
TT
Function ShowFieldNames(TableName As String)
Dim I As Integer
Dim FieldList As TableDef
Set FieldList = DBEngine(0)(0).TableDefs(TableName)
For I = 0 To FieldList.Fields.Count - 1
'Debug.Print FieldList.Fields(I).Name
Next I
End Function
I would like the list of fields returned to a combo box or list box on a form. Can anyone help. Please Ignore the line in red - this was inserted to return results when debugging in immediate mode. The solution I suspect is staring right at me but I am damned if I can resolve it!!! Am a bit rusty with Access / VBA.
I would reiterate it is the FIELD NAMES I want in the combo box - not the data they contain.
Thanks in advance,
TT
#2
Try this:
Function ShowFieldNames(TableName As String, myCombobox As ComboBox)As Boolean
Dim I As Integer
Dim FieldList As TableDef
Set FieldList = DBEngine(0)(0).TableDefs(TableName)
For I = 0 To FieldList.Fields.Count - 1
myCombobox.AddItem FieldList.Fields(I).Name, I
Next I
If myCombobox.ListCount = 0 Then
ShowFieldNames = False
Else
ShowFieldNames = True
End If
End Function
Call it like this:
If ShowFieldNames("myTable", Me.Combo1) Then
' It updated it
Else
' It didn't update it
End If
This is for a combo box. A list box is similar.
Function ShowFieldNames(TableName As String, myCombobox As ComboBox)As Boolean
Dim I As Integer
Dim FieldList As TableDef
Set FieldList = DBEngine(0)(0).TableDefs(TableName)
For I = 0 To FieldList.Fields.Count - 1
myCombobox.AddItem FieldList.Fields(I).Name, I
Next I
If myCombobox.ListCount = 0 Then
ShowFieldNames = False
Else
ShowFieldNames = True
End If
End Function
Call it like this:
If ShowFieldNames("myTable", Me.Combo1) Then
' It updated it
Else
' It didn't update it
End If
This is for a combo box. A list box is similar.
#3
Cheers man, found a solution - or should I say was shown the solution. Modify the combo box "record source type" to field names and record source to the name of the table.
Two lines of code - piece of ****. Had to be shown it by a developer with only 6 months experience, who took great pleasure in making me look like a ***....
should point out this is the first time I have worked in Access 2003 - not sure if that makes it better or worse!
thanks again...
TT
Two lines of code - piece of ****. Had to be shown it by a developer with only 6 months experience, who took great pleasure in making me look like a ***....
should point out this is the first time I have worked in Access 2003 - not sure if that makes it better or worse!
thanks again...
TT
Thread
Thread Starter
Forum
Replies
Last Post
Brzoza
Engine Management and ECU Remapping
1
02 October 2015 05:26 PM