473,473 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Export Query To XML

Can Access programatically export a query to XML?

Thanks,

Steve
Nov 13 '05 #1
2 7059
I am not particularly knowledgeable, but with a little cheating using two
knowledge base articles, I did it. - Mike Gramelspacher

'------------------------------
Sub ExportXMLFromADO()
'------------------------------
' l. Open SQL in ADO Recordset
' 2. Create new DOM
' 3. Save recordset to DOM
' 4. create another new DOM
' 5. Load .xsl (from KB article) into DOM
' 6. Transform DOM from attribute-centric to element-centric DOM using
..xsl
' 7. Save DOM as XML file

' requires Microsoft ActiveX Data Objects 2.5 Library of higher
' to persist XML directly to a DOM. See: Persisting XML Directly into
' DOM Causes Run-Time Error '438', kb264869

Dim szConnect As String
Dim SQL As String

szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\" & _
"Documents and Settings\Mike Gramelspacher\My Documents\My
Database\AnzeigerTest2K.mdb"
SQL = "TestSearchFor"

Dim oRS As ADODB.Recordset
Dim oCN As ADODB.Connection

Set oCN = New ADODB.Connection
Set oRS = New ADODB.Recordset

With oCN
.CursorLocation = adUseClient
.ConnectionString = szConnect
.ConnectionTimeout = 5
.Open szConnect
End With

oRS.Open SQL, oCN

Dim xmlDoc As DOMDocument
Set xmlDoc = New DOMDocument

' To specify a specific version, use a declaration like the following, with
the appropriate version in the ProgID:
' Dim xmlDoc As MSXML2.DOMDocument40
' Set xmlDoc = New MSXML2.DOMDocument40

xmlDoc.async = False
oRS.Save xmlDoc, adPersistXML

If xmlDoc.parseError.errorCode <> 0 Then
MsgBox "Errors During Load" & vbCrLf & xmlDoc.parseError.errorCode &
xmlDoc.parseError.reason
Else
'MsgBox xmlDoc.XML
' See: You cannot import attribute-centric XML in Access, KB285329
' Microsoft Access supports only element-centric XML. XML that is
' persisted from ADO recordsets is created in attribute-centric XML.
' File ADOXMLToAccess.xsl is from this article. It transforms
' attribute-centris XML to element-centric XML.

Dim DOMOut As DOMDocument
Dim domStylesheet As DOMDocument

Set domStylesheet = New DOMDocument
domStylesheet.Load "C:\Documents and Settings\Mike Gramelspacher\My
Documents\My Database\Access2KFiles\ADOXMLToAccess.xsl"

'Apply the transform
If Not domStylesheet Is Nothing Then
Set DOMOut = New DOMDocument
xmlDoc.transformNodeToObject domStylesheet, DOMOut

'Save the output
DOMOut.Save "C:\Documents and Settings\Mike Gramelspacher\My
Documents\Anzeiger.xml"

'Import the saved document into Access
Application.ImportXML "C:\Documents and Settings\Mike
Gramelspacher\My Documents\Anzeiger.xml"

End If
End If

oRS.Close
oCN.Close
Set oRS = Nothing
Set oCN = Nothing
End Sub

"PC Datasheet" <no****@nospam.spam> wrote in message
news:Hn*****************@newsread3.news.atl.earthl ink.net...
Can Access programatically export a query to XML?

Thanks,

Steve

Nov 13 '05 #2
Thanks, Mike, appreciate the help!
"Mike Gramelspacher" <gr******@psci.net> wrote in message
news:dd**********@pscinews.psci.net...
I am not particularly knowledgeable, but with a little cheating using two
knowledge base articles, I did it. - Mike Gramelspacher

'------------------------------
Sub ExportXMLFromADO()
'------------------------------
' l. Open SQL in ADO Recordset
' 2. Create new DOM
' 3. Save recordset to DOM
' 4. create another new DOM
' 5. Load .xsl (from KB article) into DOM
' 6. Transform DOM from attribute-centric to element-centric DOM using
.xsl
' 7. Save DOM as XML file

' requires Microsoft ActiveX Data Objects 2.5 Library of higher
' to persist XML directly to a DOM. See: Persisting XML Directly into
' DOM Causes Run-Time Error '438', kb264869

Dim szConnect As String
Dim SQL As String

szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\" & _
"Documents and Settings\Mike Gramelspacher\My Documents\My
Database\AnzeigerTest2K.mdb"
SQL = "TestSearchFor"

Dim oRS As ADODB.Recordset
Dim oCN As ADODB.Connection

Set oCN = New ADODB.Connection
Set oRS = New ADODB.Recordset

With oCN
.CursorLocation = adUseClient
.ConnectionString = szConnect
.ConnectionTimeout = 5
.Open szConnect
End With

oRS.Open SQL, oCN

Dim xmlDoc As DOMDocument
Set xmlDoc = New DOMDocument

' To specify a specific version, use a declaration like the following,
with the appropriate version in the ProgID:
' Dim xmlDoc As MSXML2.DOMDocument40
' Set xmlDoc = New MSXML2.DOMDocument40

xmlDoc.async = False
oRS.Save xmlDoc, adPersistXML

If xmlDoc.parseError.errorCode <> 0 Then
MsgBox "Errors During Load" & vbCrLf & xmlDoc.parseError.errorCode
& xmlDoc.parseError.reason
Else
'MsgBox xmlDoc.XML
' See: You cannot import attribute-centric XML in Access, KB285329
' Microsoft Access supports only element-centric XML. XML that is
' persisted from ADO recordsets is created in attribute-centric
XML.
' File ADOXMLToAccess.xsl is from this article. It transforms
' attribute-centris XML to element-centric XML.

Dim DOMOut As DOMDocument
Dim domStylesheet As DOMDocument

Set domStylesheet = New DOMDocument
domStylesheet.Load "C:\Documents and Settings\Mike Gramelspacher\My
Documents\My Database\Access2KFiles\ADOXMLToAccess.xsl"

'Apply the transform
If Not domStylesheet Is Nothing Then
Set DOMOut = New DOMDocument
xmlDoc.transformNodeToObject domStylesheet, DOMOut

'Save the output
DOMOut.Save "C:\Documents and Settings\Mike Gramelspacher\My
Documents\Anzeiger.xml"

'Import the saved document into Access
Application.ImportXML "C:\Documents and Settings\Mike
Gramelspacher\My Documents\Anzeiger.xml"

End If
End If

oRS.Close
oCN.Close
Set oRS = Nothing
Set oCN = Nothing
End Sub

"PC Datasheet" <no****@nospam.spam> wrote in message
news:Hn*****************@newsread3.news.atl.earthl ink.net...
Can Access programatically export a query to XML?

Thanks,

Steve


Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Tim Eliot | last post by:
Just wondering if anyone has hit the following issue and how you might have sorted it out. I am using the command: DoCmd.TransferText acExportMerge, , stDataSource, stFileName, True after...
2
by: GAVO-UK | last post by:
Using Ms Access 2003 I am trying to export a query to a delimited txt file containing some CURRENCY fields, which I want to export without the currently symbol and with five decimals. I have...
0
by: mathewbutler | last post by:
Access 2000 SR-1 I've spent some time investigating this, and researching both here and on the MS support site. Briefly; Problem Description: ------------------------------- I have the...
3
by: azzi2000 | last post by:
This should be rather simple. I have a query using different link tables and 2 parameters. The query works perfect. However I need to export or save the result in an Access table in order to...
5
by: Simon | last post by:
Dear reader, With the export command you can export a query to Excel. By activate this command a form pop's up with the following text:
1
by: smaczylo | last post by:
Hello, I've recently been asked to work with Microsoft Access, and while I feel quite comfortable with Excel, I'm at a complete loss with databases. If someone could help me with this issue I'm...
9
by: NEWSGROUPS | last post by:
I have data in a table in an Access 2000 database that needs to be exported to a formatted text file. For instance, the first field is an account number that is formatted in the table as text and...
3
by: Wayne | last post by:
I'm trying to automate the export of a query to a text file using code. If I export the query manually I get the exact result that I want i.e. If I select the query then choose File/Export from...
1
by: rhbourne | last post by:
Hello, I'm doing something wrong here, and it's driving me nuts. A very strange thing is happening with a query that populates an XML export function. 1. I have a query and its XML export...
8
by: iheartvba | last post by:
Hi I am using Access 2007 and am trying to export a query to a fixed length text file. I tried using the following code to export the text file: DoCmd.TransferText acExportFixed, , "qryFFRDeFile",...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.