Excel VBA needs to run SQL query

Posted on 2005-05-11
Last Modified: 2010-05-01
I have an Excel spreadsheet which gets its data from an external data source (a SQL database).  The SQL query used pulls all data from a table which is within a date range.  At the moment this date range is assigned by manually updating the query.

Once the data is on the spreadsheet, I have a macro which updates the sheet, and refreshes the database query thus presenting up-to-date information.

I'd like to use VBA to prompt the user for a date range, so that they don't get a chance to edit the whole query.  The dates they enter would be inserted into the SQL query and then run against the database.

Any ideas?


Question by:SquareOneResources
    LVL 8

    Accepted Solution

    You could use a standard input box then modify your query with the users input. You might also check the users input after it is entered.

    dim BgnDte, EndDte

    BgnDte = Application.InputBox(prompt:="Enter Beginning Date:", Title:="Beginning Date", Left:=300, Top:=50, Type:=2)

    EndDte = Application.InputBox(prompt:="Enter Ending date:", Title:="Ending Date", Left:=300, Top:=50, Type:=2)

    LVL 8

    Expert Comment

    A grade of "C"?

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now