We help IT Professionals succeed at work.
Get Started

VBA Recordset How to Assign Field Names

526 Views
Last Modified: 2012-05-12
Hello again,
I have a table that will hold header records.  These are going to be used to insert into a recordset which then will be populated with the data from a separate csv file.  The number of fields will vary with each header record.  The header table has 3 fields: HeaderID, FieldName and FieldOrder.  For the first two header records, the data would look like this:
HeaderID   FieldName   FieldOrder
1               HeaderID     10
1               AcctName   20
1               AcctNum     30
2               HeaderID     10
2               AcctName   20
2               AcctNum     30
2               AcctMgr      40
2               AcctRegion 50

I need to create a recordset using FieldName as the Column Headings in my new recordset.  For the first Header record, It should look like this:
recordset:
HeaderID     AcctName    AcctNum

It seems like it should be a simple problem, but I haven't been able to get anything to actually work.  I've attached my latest attempt.

Thanks, Lynn


'pull the header record data
    strSQL = " SELECT tblHeader.*"
    strSQL = strSQL & " From tblHeader"
    strSQL = strSQL & " WHERE (((tblHeader.HeaderID)=" & rptNum & "))"
    strSQL = strSQL & " ORDER BY tblHeader.FieldOrder;"


    Set rc = db.OpenRecordset(strSQL, dbOpenDynaset)
    rCnt = rc.RecordCount
    
    Debug.Print (rCnt)
    
    'test: 21 records for header 004
    For i = 1 To rCnt
        rc.MoveFirst
        Set rc.Fields(i).Name = rc.Fields("FieldName")
    Next

Open in new window

Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 12 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE