Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default interaction with excel and ms SQL (retrieval of records from sql intoa sheet)

Hi,

I used to use this vba script in excel to get data from a mySQL
Database.

================================================== =====

Sub databaseophalen()

'-------------------------------------------------------------------------
' Connection variables
Dim Conn As New ADODB.Connection
Dim server_name As String
Dim database_name As String
Dim user_id As String
Dim password As String

' Table action variables
Dim sqlstr As String ' SQL to perform various actions
Dim rs As ADODB.Recordset

'----------------------------------------------------------------------
' Establish connection to the database
server_name = "nameOfTheServer" ' Enter your server name here - if
running from a local computer use 127.0.0.1
database_name = "nameOfTheDatabase" ' Enter your database name here
user_id = "userID" ' enter your user ID here
password = "Password" ' Enter your password here

Set Conn = New ADODB.Connection
Conn.Open "DRIVER={MySQL ODBC 3.51 Driver}" _
& ";SERVER=" & server_name _
& ";DATABASE=" & database_name _
& ";UID=" & user_id _
& ";PWD=" & password _
& ";OPTION=3" ' Option 16427 = Convert LongLong to Int: This just
helps makes sure that large numeric results get properly interpreted

'-------------------------------------------------------------------------

Set rs = New ADODB.Recordset
sqlstr = "select * from database"
rs.Open sqlstr, Conn, adOpenStatic
With Worksheets("Current_previous_settings").Cells(2, 1) ' Enter your
sheet name and range here
..ClearContents
..CopyFromRecordset rs
End With

'-----------------------------------------------------------------------
' Close connections
On Error Resume Next
rs.Close
Set rs = Nothing
Conn.Close
Set Conn = Nothing
On Error GoTo 0

MsgBox ("done")

End Sub


================================================== =====



Now I need to interact with a MS SQL database, but I would like to do
that in a similar way, so that the results are outputted in a cell
(actually it start at that cell and fills all cells below till
finished).

Can someone help me out how to connect to MS SQL with VBA / EXCEL. I
can't seem to find a tutorial.
Regards,

Matthijs
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default interaction with excel and ms SQL (retrieval of records from sql into a sheet)

You need a different connection string. See
http://www.carlprothman.net/Default....erForSQLServer

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

wrote in message
...
Hi,

I used to use this vba script in excel to get data from a mySQL
Database.

================================================== =====

Sub databaseophalen()

'-------------------------------------------------------------------------
' Connection variables
Dim Conn As New ADODB.Connection
Dim server_name As String
Dim database_name As String
Dim user_id As String
Dim password As String

' Table action variables
Dim sqlstr As String ' SQL to perform various actions
Dim rs As ADODB.Recordset

'----------------------------------------------------------------------
' Establish connection to the database
server_name = "nameOfTheServer" ' Enter your server name here - if
running from a local computer use 127.0.0.1
database_name = "nameOfTheDatabase" ' Enter your database name here
user_id = "userID" ' enter your user ID here
password = "Password" ' Enter your password here

Set Conn = New ADODB.Connection
Conn.Open "DRIVER={MySQL ODBC 3.51 Driver}" _
& ";SERVER=" & server_name _
& ";DATABASE=" & database_name _
& ";UID=" & user_id _
& ";PWD=" & password _
& ";OPTION=3" ' Option 16427 = Convert LongLong to Int: This just
helps makes sure that large numeric results get properly interpreted

'-------------------------------------------------------------------------

Set rs = New ADODB.Recordset
sqlstr = "select * from database"
rs.Open sqlstr, Conn, adOpenStatic
With Worksheets("Current_previous_settings").Cells(2, 1) ' Enter your
sheet name and range here
.ClearContents
.CopyFromRecordset rs
End With

'-----------------------------------------------------------------------
' Close connections
On Error Resume Next
rs.Close
Set rs = Nothing
Conn.Close
Set Conn = Nothing
On Error GoTo 0

MsgBox ("done")

End Sub


================================================== =====



Now I need to interact with a MS SQL database, but I would like to do
that in a similar way, so that the results are outputted in a cell
(actually it start at that cell and fills all cells below till
finished).

Can someone help me out how to connect to MS SQL with VBA / EXCEL. I
can't seem to find a tutorial.
Regards,

Matthijs



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default interaction with excel and ms SQL (retrieval of records from sqlinto a sheet)

You need a different connection string. Seehttp://www.carlprothman.net/Default.aspx?tabid=87#OLEDBProviderForSQL...

Hi Bob,

thanks. I'll look into it!
regards,

Matthijs
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
Split text file into Excel sheet and separate the final results intoa new sheet Luciano Paulino da Silva Excel Worksheet Functions 8 April 18th 09 02:00 AM
user interaction to select a sheet mark Excel Programming 5 May 29th 07 03:11 AM
Using vb to paste records from 1 sheet, to related records in another chrisnichols87 Excel Programming 0 January 10th 07 10:59 AM
Copy Records from Access Database table(more than 5 lakh records in this table) to Excel Sheet divya Excel Programming 1 October 26th 06 12:12 PM
Retrieval of details in excel sheet chits Excel Programming 0 September 4th 05 11:09 AM


All times are GMT +1. The time now is 06:39 PM.

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

About Us

"It's about Microsoft Excel"