Trying to loop through Field Names

G

Guest

I'm trying to look through each firld in a table using the following code:


Sub PopulateForm()
On Error GoTo TableInfoErr
Dim db As Database
Dim tdf As TableDef
Dim fld As Field
Set db = DBEngine(0)(0)
Set tdf = db.TableDefs("Service Address")

MsgBox ("Service Address table has " & tdf.Fields.Count & " fields")
For Each fld In tdf.Fields
MsgBox (fld.Name)
Call PopulateField(fld.Name)
Next


TableInfoExit:
Set db = Nothing
Exit Sub

TableInfoErr:
Select Case Err
Case 3265 ' Supplied table name invalid
MsgBox strTableName & " table doesn't exist"
Case Else
MSGBOX("TableInfo() Error " & Err & ": " & Error)
End Select
Resume TableInfoExit
End Sub

I have the msgbox's in there so I can tell where the problem is, it displays the MsgBox ("Service Address table has " & tdf.Fields.Count & " fields") correctly, buit then doesn't seem to be doing the "For each fld" part (it doesn't even display the msgbox.) Nor does it call the PopulateField sub (which populates a form in word iwht the value from each field.) What am I doing wrong?

Thanks
 
W

Wayne Morgan

Your code is DAO. Both DAO and ADO have an object called Field. The
References listed in Tools|References in the code window are used in the
order that the checked items are listed. If ADO is above DAO then your "Dim
fld As Field" will be an ADO field. To correct this you either need to
change the order in the References or remove the ambiguity from your code by
changing the Dim statement to "Dim fld As DAO.Field". I would recommend this
for all DOA objects instead of trying to remember which ones have duplicates
in ADO.

--
Wayne Morgan
MS Access MVP


InspAppsNY said:
I'm trying to look through each firld in a table using the following code:


Sub PopulateForm()
On Error GoTo TableInfoErr
Dim db As Database
Dim tdf As TableDef
Dim fld As Field
Set db = DBEngine(0)(0)
Set tdf = db.TableDefs("Service Address")

MsgBox ("Service Address table has " & tdf.Fields.Count & " fields")
For Each fld In tdf.Fields
MsgBox (fld.Name)
Call PopulateField(fld.Name)
Next


TableInfoExit:
Set db = Nothing
Exit Sub

TableInfoErr:
Select Case Err
Case 3265 ' Supplied table name invalid
MsgBox strTableName & " table doesn't exist"
Case Else
MSGBOX("TableInfo() Error " & Err & ": " & Error)
End Select
Resume TableInfoExit
End Sub

I have the msgbox's in there so I can tell where the problem is, it
displays the MsgBox ("Service Address table has " & tdf.Fields.Count & "
fields") correctly, buit then doesn't seem to be doing the "For each fld"
part (it doesn't even display the msgbox.) Nor does it call the
PopulateField sub (which populates a form in word iwht the value from each
field.) What am I doing wrong?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top