Consulting

Results 1 to 3 of 3

Thread: getting data from SQL Server to Excel and vice versa

  1. #1

    Exclamation getting data from SQL Server to Excel and vice versa

    Hi,

    I need to get data from MS SQL Server to Excel and vice versa.

    Could you please send me the code for that.

    Thanks for your help.

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Tutor mohanvijay's Avatar
    Joined
    Aug 2010
    Location
    MADURAI
    Posts
    268
    Location
    Try this (Using ADO reference)

    refer this website more connection strings http://www.connectionstrings.com

    [vba]

    Dim db_sl As New ADODB.Connection
    Dim rs_sl As New ADODB.Recordset
    Dim con_str As String, qry_sl As String
    con_str = "Provider=SQLNCLI;Server=YourServerAddress;Database=YourDataBase;Uid=YourUs ername;Pwd=YourPassword;"
    rs_sl = "SELECT * FROM tablename" 'Your Query
    db_sl.Open con_str
    rs_sl.Open qry_sl, db_sl, adOpenStatic, adLockReadOnly
    Range("a1").CopyFromRecordset rs_sl 'Get data into Excel
    rs_sl.Close
    Set rs_sl = Nothing
    db_sl.Close
    Set db_sl = Nothing

    [/vba]
    Last edited by Aussiebear; 08-24-2011 at 03:53 PM. Reason: Changed to correct tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •