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, "<xmlData>"
While Not rs.EOF
Print #1, "<tblWES>"
For i = 0 To numFields
Print #1, "<" & rs(i).Name & ">" & rs(i).Value & "</" & rs(i).Name & ">"
Next
rs.MoveNext
Print #1, "</tblWES>"
Wend
rs.Close
Set rs = Nothing
Print #1, "</xmlData>"
Close #1
MsgBox "Export completed successfully!", vbOKOnly, "File Exported"
End Sub
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, "<xmlData>"
While Not rs.EOF
Print #1, "<tblWES>"
For i = 0 To numFields
Print #1, "<" & rs(i).Name & ">" & rs(i).Value & "</" & rs(i).Name & ">"
Next
rs.MoveNext
Print #1, "</tblWES>"
Wend
rs.Close
Set rs = Nothing
Print #1, "</xmlData>"
Close #1
MsgBox "Export completed successfully!", vbOKOnly, "File Exported"
End Sub