It has been a long time since I have added a VBA post, so here is something that I had to quickly put together for a little project that I am working on this evening.
I have a really large table with a bunch of fieldnames that I need to query through to make some sample data for another project.
I thought that it would be easiest to quickly grab all of the field names from my target table so I can filter out only the ones I need and not manually type a bunch of field names.
I am sure there are other trivial ways of doing this, but since you found my post, you are probably looking to do what I am doing here.
Get Column Names from Access Recordset with VBA
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
Sub getFieldNamesFromTable(targetTable As String) Dim db As DAO.Database Dim rs As DAO.Recordset Dim strSQL As String Set db = CurrentDb strSQL = "SELECT TOP 1 * FROM " & targetTable Set rs = db.OpenRecordset(strSQL) For Each fName In rs.Fields Debug.Print fName.Name Next fName 'Do While Not rs.EOF 'Loop db.Close Set db = Nothing Set rs = Nothing End Sub
And then to use this method, set up another sub in a module or wherever
1 2 3
Sub tableNamesRunner() getFieldNamesFromTable ("MyAccessDBTableName") End Sub