+ Reply to Thread
Results 1 to 4 of 4

Use .txt file instead of SQL Query

  1. #1
    Registered User
    Join Date
    06-12-2008
    Posts
    11

    Use .txt file instead of SQL Query

    I've created a basic macro that pulls data from SQL sever into excel.

    The connection string and and query work fine but I was wondering if it is possible to replace the query embedded within the macro to call a .txt or .sql file and use the text within the file as the query?

    so instead of .Open "SELECT * FROM Authors" I would have a link to the text file and use the text contained within it as the query

    Any help or pointers in the right direction will be much appreciated.
    Last edited by mark_jam3s; 09-17-2010 at 10:06 AM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Use .txt file instead of SQL Query

    Look at doing a Data > Import External Data > Import Data and set the popup dialog to Text Files. Then continue with the wizard. Be sure to use the Properties at the end to set the refresh options if you want the text file data to update itself from time to time.

    This process can be recorded into a macro to examine later for code you can tweak.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Use .txt file instead of SQL Query

    I have done this in the past using a form with a couple of textboxes for the connection and SQL strings, then I have Load and Save buttons to read write the SQL strings to text files like so:
    Please Login or Register  to view this content.
    Remember what the dormouse said
    Feed your head

  4. #4
    Registered User
    Join Date
    06-12-2008
    Posts
    11

    Re: Use .txt file instead of SQL Query

    Thank you both for your replies.

    I ended-up using the following:


    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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