I need to get all of the column names of a table using VBA or Access SQL and iterate through them for validation, does anyone have a solution to this, I have searched Google to no avail.
This will work
Set db = CurrentDb()
Set rs1 = db.OpenRecordset("Table1")
Dim fld As DAO.Field
For Each fld In rs1.Fields
MsgBox (fld.Name)
Next
Set fld = Nothing
-
3I generally never use a counter for collections that have their own data type for the items in the collection. I'd use For Each fld In rs1.Fields, instead (with the variable fld as type DAO.Field). – David-W-Fenton Jul 28 '10 at 19:44
-
I've now edited it to fix an oversight in your edit (you omitted changing the counter variable to a field variable). – David-W-Fenton Jul 30 '10 at 18:03
Dim l As Integer
For l = 0 To CurrentDb.TableDefs("tbl_Name").Fields.Count - 1
Debug.Print CurrentDb.TableDefs("tbl_Name").Fields(l).name
Next l
-
This is what Feral had 3 years ago, but changed it to the much better code proposed by David. I don't see why you added it (three years later); even less why it got upvotes. – Johanness Dec 24 '16 at 14:41
-