Solved

Macros For Excel

Posted on 2003-11-22
2
648 Views
Last Modified: 2010-04-17
Well I need to to retrieve data from MS SQL Server Database tables and then make an update on the sheet and save it back to the tables in MS SQL Server Database using XML.
Well to make it clear I dont want to import or export data from  MS SQL Server Database.

It must work similar to ClientServer environment.

0
Comment
Question by:amiteshsinha
2 Comments
 
LVL 8

Accepted Solution

by:
Rog earned 500 total points
ID: 9803108
This can be done as VBA in Excel.

This will get you a connection to a sql data base.  You will have to change the Connection String to connect to another database.

(www.connectionstrings.com) will help you with other types of connection stirng


Public Sub DataToWorksheet()


'Have to set a reference to Microsoft ActiveX Data Objects

Dim cn As ADODB.Connection
Dim sCon As String
Dim aRs As ADODB.Recordset
Dim ssql As String

sCon = "Driver={SQL Server};Server=Aron1;Database=pubs;Uid=sa;Pwd=asdasd;"

Set cn = New ADODB.Connection
cn.Open sCon

ssql = "Select * from Table"

Set rs = New ADODB.Recordset

rs.Open ssql, cn


'Loops through recordset
Do While Not (rs.EOF Or rs.BOF)
   ActiveSheet.Range("A1") = rs("Field1Name")
   rs.MoveNext
Loop

End Sub

Public Sub DataBackIn()

Dim cn As ADODB.Connection
Dim sCon As String
Dim aRs As ADODB.Recordset
Dim ssql As String

sCon = "Driver={SQL Server};Server=Aron1;Database=pubs;Uid=sa;Pwd=asdasd;"

Set cn = New ADODB.Connection
cn.Open sCon

'This will update the table
ssql = "Update tableName set Field1 = " & ActiveSheet.Range("A1")
cn.Execute ssql

End Sub


I hope this is what you were looking for....

Rog
0
 

Author Comment

by:amiteshsinha
ID: 9803511
I need to know how we can use an XMl in an excel macro for saving data into MS SQL server database
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now