Solved

Macros For Excel

Posted on 2003-11-22
2
655 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

776 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