Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 44
Default Excel & SQL server

Hi All
I need a sample code to connect, run a query in sql server and display
results in excel with these informations:
-worksheetname is "SHEET1"
-sql server name is "MYSQL"
-database name is "TEST"
-table name is "TABLE1"
-userid is "USER1"
-password is"PASS"
-my script file located in my computer D:\test\QUERY1.TXT(can be done in
query analyzer)
Best regards
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 523
Default Excel & SQL server

First, go to tools/references and tick Microsoft ActiveX Data Objects 2.7 (or
whatever the latest version is)

The use this:

Sub demo()

Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Dim s As String

Set con = New ADODB.Connection
con.Open "Driver={SQL Server};Server=MYSQL;Database=TEST;Uid=USER1;Pwd=P ASS;"

s = "Your SQL statement here"

Set rs = con.Execute(s, , 1)

Range("a1").CopyFromRecordset rs

rs.Close

con.Close

End Sub



"bijan" wrote:

Hi All
I need a sample code to connect, run a query in sql server and display
results in excel with these informations:
-worksheetname is "SHEET1"
-sql server name is "MYSQL"
-database name is "TEST"
-table name is "TABLE1"
-userid is "USER1"
-password is"PASS"
-my script file located in my computer D:\test\QUERY1.TXT(can be done in
query analyzer)
Best regards

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 44
Default Excel & SQL server

Thank you so much Sam,it's working perfectly
I want to know is there another way to keep my query statments in sql server
and just pass parameters from excel to it or not?
Thanks :)

"Sam Wilson" wrote:

First, go to tools/references and tick Microsoft ActiveX Data Objects 2.7 (or
whatever the latest version is)

The use this:

Sub demo()

Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Dim s As String

Set con = New ADODB.Connection
con.Open "Driver={SQL Server};Server=MYSQL;Database=TEST;Uid=USER1;Pwd=P ASS;"

s = "Your SQL statement here"

Set rs = con.Execute(s, , 1)

Range("a1").CopyFromRecordset rs

rs.Close

con.Close

End Sub



"bijan" wrote:

Hi All
I need a sample code to connect, run a query in sql server and display
results in excel with these informations:
-worksheetname is "SHEET1"
-sql server name is "MYSQL"
-database name is "TEST"
-table name is "TABLE1"
-userid is "USER1"
-password is"PASS"
-my script file located in my computer D:\test\QUERY1.TXT(can be done in
query analyzer)
Best regards

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 523
Default Excel & SQL server

Bijan,

I've never used it with a query statement from SQL server, but I would
imagine it is possible. In the line

"set rs = con.execute(s,,1)"

s was the sql query, but it can also be a QueryDef object - I've never used
them but that could be the way to do it.

Sam

"bijan" wrote:

Thank you so much Sam,it's working perfectly
I want to know is there another way to keep my query statments in sql server
and just pass parameters from excel to it or not?
Thanks :)

"Sam Wilson" wrote:

First, go to tools/references and tick Microsoft ActiveX Data Objects 2.7 (or
whatever the latest version is)

The use this:

Sub demo()

Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Dim s As String

Set con = New ADODB.Connection
con.Open "Driver={SQL Server};Server=MYSQL;Database=TEST;Uid=USER1;Pwd=P ASS;"

s = "Your SQL statement here"

Set rs = con.Execute(s, , 1)

Range("a1").CopyFromRecordset rs

rs.Close

con.Close

End Sub



"bijan" wrote:

Hi All
I need a sample code to connect, run a query in sql server and display
results in excel with these informations:
-worksheetname is "SHEET1"
-sql server name is "MYSQL"
-database name is "TEST"
-table name is "TABLE1"
-userid is "USER1"
-password is"PASS"
-my script file located in my computer D:\test\QUERY1.TXT(can be done in
query analyzer)
Best regards

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
can not locate the internet server or proxy server Tufail Excel Programming 0 September 30th 07 08:13 AM
SQL Server -- Bulk Insert from Excel to SQL Server Madhan Excel Discussion (Misc queries) 0 December 12th 06 03:08 PM
Excel 2003 Error Cannot Locate the Internet Server or Proxy Server Seabee Excel Discussion (Misc queries) 0 November 20th 05 12:03 AM
problem updating link from Novell server to windows server #REF er Ellen Excel Discussion (Misc queries) 0 May 10th 05 09:18 PM
SQL server - Excel Markonni Excel Programming 1 March 7th 05 12:32 AM


All times are GMT +1. The time now is 01:32 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"