Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamicall reference field

Status
Not open for further replies.

smartFly

Programmer
Apr 30, 2001
16
US
I'm trying to loop through a recordset and loop through the fields collection within each record to dynamically write an xml file. For some reason I get an error with:

while not recordSet1.EOF
for i = 0 to numFields ' number of fields in table
Print #1, recordSet1(i).Value
next
recordSet1.MoveNext()
wend

... but recordSet1(0).Value would work

here's the full code:

Private Sub cmdExport_Click()

Dim strInput As String, strMsg As String, strFileName As String, numField As Integer, refField As Integer

strFileName = "c:\Documents and Settings\mehalicka\desktop\file1.xml"

Kill strFileName
Open strFileName For Append As #1

Dim rs As New ADODB.Recordset
rs.Open "tbl_user", CurrentProject.Connection, adOpenKeyset, adLockOptimistic

numFields = rs.Fields.Count

Print #1, &quot;<xmlData>&quot;

While Not rs.EOF
Print #1, &quot;<tblWES>&quot;
For i = 0 To numFields
Print #1, &quot;<&quot; & rs(i).Name & &quot;>&quot; & rs(i).Value & &quot;</&quot; & rs(i).Name & &quot;>&quot;
Next
rs.MoveNext
Print #1, &quot;</tblWES>&quot;
Wend

rs.Close
Set rs = Nothing

Print #1, &quot;</xmlData>&quot;

Close #1
MsgBox &quot;Export completed successfully!&quot;, vbOKOnly, &quot;File Exported&quot;
End Sub
 
use

recordSet1.Fields(i).Value


Mike Pastore

Hats off to (Roy) Harper
 
I figured it out. Instead of:

for i = 0 to numFields

I should've had:

for i = 0 to numFields - 1

I was just getting confused by the error message. Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top