exporting xml file from pass through query (1 Viewer)

focus10

Registered User.
Local time
Today, 18:39
Joined
Mar 8, 2009
Messages
38
i have xml data stored in sql server table.
because sql server dose not support utf-8 encoding
i want to export it to an xml file from access.
i can retrive the xml data by using a pass through query.
is there any code to can help me to export this data to an xml file?

thanks
 

sxschech

Registered User.
Local time
Today, 10:39
Joined
Mar 2, 2010
Messages
802
I've taken regular query data and used vba to export into a specific xml layout, but haven't taken already xml data and exported that. I can post code if you are going to take the data and have it formatted into xml. Essentially I was given an xml file and I copied it into vba then replaced the actual data with the field names so that when data were exported it will fill out the file with the information.
 

focus10

Registered User.
Local time
Today, 18:39
Joined
Mar 8, 2009
Messages
38
i'll be glad to have your code
 

sxschech

Registered User.
Local time
Today, 10:39
Joined
Mar 2, 2010
Messages
802
Here is the code. You will need to redo the xml part based on how the layout of your xml file is and of course change the recordset and fields needed to your data source.

Code:
Private Sub btnExportXML_Click()
'http://forums.augi.com/showthread.php?125024-Create-XML-file-using-VBA
'Output selected Product Code back to xml format
'after making corrections
'20150506
     
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim stsql As String
    Dim stProductCode As String
    Dim stQuestionNumber As String
    Dim stFolder As String
    Dim i As Integer
    Dim xmlFile
    
    If IsNull(Me.cboProductCode) Then
        MsgBox "Please choose a product code and then export to xml", vbOKOnly, "Missing Product Code"
        GoTo Exitsub
    End If
    Me.lblExportXMLNote.Visible = True
    xmlFile = DLookup("FileLocation", "tblFileLocations", "FileType='xml Files'") & Me.cboProductCode & ".xml"
    If Dir(xmlFile) <> "" Then
        yesno = MsgBox("File exists.  Overwrite / Replace / New Location?", vbYesNoCancel, "File Exists")
        If yesno = vbNo Then
            GoTo Exitsub
        ElseIf yesno = vbCancel Then
            stFolder = selectFolder
            xmlFile = stFolder & "\" & Me.cboProductCode & ".xml"
        End If
    End If
    
    Set db = CurrentDb
    Set rs = db.OpenRecordset("SELECT * FROM xmlExam where ProductCode='" & Me.cboProductCode & "'")
    
    rs.MoveFirst
    stQuestionNumber = rs!questionNumber
    Open xmlFile For Output As #1
    Print #1, "<?xml version=" & Chr(34) & "1.0" & Chr(34) & _
          " encoding=" & Chr(34) & "utf-8" & Chr(34) & "?>"
          '' <  add xml-schema if it's needed here >
    Print #1, "<test ProductCode=" & Chr(34) & rs!ProductCode & Chr(34) & " Category=" & Chr(34) & rs!Category & Chr(34) & " displayQuestions=" & Chr(34) & rs!displayQuestions & Chr(34) & " passMinimum=" & Chr(34) & rs!passMinimum & Chr(34) & ">"
    Print #1, "  <questions>"
    Print #1, "    <question text=" & Chr(34) & ReplaceHTML(rs!text1) & Chr(34) & " questionNumber=" & Chr(34) & rs!questionNumber & Chr(34) & ">"
    Print #1, "      <options>"
        
    Do While Not rs.EOF
        If rs!questionNumber = stQuestionNumber Then
            If rs!correct = True Then
                stCorrect = " correct=" & Chr(34) & LCase(rs!correct) & Chr(34)
            Else
                stCorrect = ""
            End If
            Print #1, "        <option type=" & Chr(34) & rs!type & Chr(34) & " text=" & Chr(34) & ReplaceHTML(rs!Text2) & Chr(34) & stCorrect & " />"
            rs.MoveNext
        Else
            Print #1, "      </options>"
            Print #1, "    </question>"
            Print #1, "    <question text=" & Chr(34) & ReplaceHTML(rs!text1) & Chr(34) & " questionNumber=" & Chr(34) & rs!questionNumber & Chr(34) & ">"
            Print #1, "      <options>"
            stQuestionNumber = rs!questionNumber
        End If
    Loop
    
    Print #1, "      </options>"
    Print #1, "    </question>"
    Print #1, "  </questions>"
    Print #1, "</test>"
    Close #1
    rs.Close
    Set rs = Nothing
    Set db = Nothing
    If DCount("ProductCode", "XMLExamOut", "ProductCode='" & stTableName & "'") = 0 Then
        stsql = "INSERT INTO XMLExamOUT ( ProductCode ) " & _
                "SELECT XMLExam.ProductCode " & _
                "FROM XMLExam " & _
                "WHERE [productcode]='" & [Forms]![frmmain].[cboProductCode] & "' "
        CurrentDb.Execute stsql
    End If
Exitsub:
    Me.lblExportXMLNote.Visible = False
End Sub
 

Users who are viewing this thread

Top Bottom