Results 1 to 10 of 10

VBA: Pulling MS SQL data to text file

Threaded View

  1. #1
    Forum Contributor VAer's Avatar
    Join Date
    09-10-2016
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    830

    VBA: Pulling MS SQL data to text file

    I would like to post this thread (kinda different topic) regarding a reply in previous thread: https://www.excelforum.com/excel-pro...-database.html

    How to use VBA to pull data into text file? I was able to use below code to pull data from MS SQL to excel sheet, however, sometimes there are multiple millions of records (or I don't want to make excel file size too large, just use a text file as data source), how can I modify the code and pull data into text file (using below folder path)?

    From the text file, how can I use VBA (SELECT *** From Text_File Where ColumnName = ***) to pull data from text file to excel sheet?

    Set Cn = CreateObject("ADODB.Connection")
        Cn.Open ConnectionString
        
        
        Set TestRs = CreateObject("ADODB.Recordset")
        TestQuery = "Select * From Table_name" 
        TestRs.Open TestQuery, Cn
    
        i = 0
        For Each TestField In TestRs.Fields
             WB.Sheets("Home").Cells(1, 1).Offset(, i) = TestField.Name
             i = i + 1
        Next
        WB.Sheets("Home").Cells(2, 1).CopyFromRecordset TestRs
        
        
        Set TestRs = Nothing
    WBName = ThisWorkbook.Name
    WBFullName = ThisWorkbook.FullName
    FolderPath = Replace(WBFullName, WBName, "")
    Last edited by VAer; 11-06-2024 at 04:23 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Pulling data from source file into main file
    By cmb80 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 03-16-2023, 03:28 PM
  2. Pulling data from source file into main file
    By cmb80 in forum Excel General
    Replies: 1
    Last Post: 03-15-2023, 10:44 AM
  3. Replies: 1
    Last Post: 09-10-2021, 02:32 AM
  4. Extracting/pulling data from text file into excel sheet
    By guest2013 in forum Excel General
    Replies: 0
    Last Post: 01-26-2015, 11:31 AM
  5. [SOLVED] Pulling data from a text file, splitting by fixed column width
    By ferynd in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-15-2013, 08:21 PM
  6. Pulling text from a separate .cvs file to populate a cell.
    By ihatekale in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-08-2013, 05:43 PM
  7. [SOLVED] pulling three data strings from a text file - at the same time
    By El Conquistador in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-08-2013, 09:27 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1