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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code not working

Status
Not open for further replies.

Ringers

Technical User
Feb 26, 2004
180
AU
I got this code from M$ and it doesn't work, can anyone tell me why.I am trying to import data from a SQL database using a M$ access front end. Then export it to a XML page.

I have created a form with a command button and inserted the code below, but it comes back with a compile error and higlights the try.

I have changed the user id and password and the data source to my database mercy.

Try
Dim cn As New System.Data.SqlClient.SqlConnection()
cn.ConnectionString = _
"data source=(mercy);initial catalog=pubs;" & _
"User ID=sa;Password=!sa789"
cn.Open()

Dim cmd As New System.Data.SqlClient.SqlCommand()
cmd.Connection = cn
cmd.CommandText = "SELECT * FROM F_Pat_Present FOR XML AUTO, XMLDATA"

Dim xmlr As System.XML.XmlReader
xmlr = cmd.ExecuteXmlReader()
xmlr.Read()
Do While xmlr.ReadState <> XML.ReadState.EndOfFile
System.Diagnostics.Debug.WriteLine (xmlr.ReadOuterXml())
Loop
MessageBox.Show ("Reached the end. Check the output window.")

Catch ex As Exception
MessageBox.Show (ex.Message)
End Try
 
If this code is in an Access db the TRY block may not be valid syntax for VBA. I think it will work in VB.NET but I don't think it will work within Access
 
I thought this had been explained in the other forums you posted this in. This is VB.NET code, it is not VBA.

You need ADO objects for retrieving the data from SQL server. For creating the XML file, one possibility is using the FileSystem object.



 
Ringers,
The post in the VB.NET forum requires your attention.

You didn't confirm what language/program you are using.
The code you are using is vb.net and is not complete.. It WILL NOT work in Access.

To export an object to XML from Access you can simply use "ExportXML Method". Have a look in the help file for more details.


________________________________________________________
Zameer Abdulla
Help to find Missing people
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top