+ Reply to Thread
Results 1 to 10 of 10

Pass Input Box VBA Variable to SQL Query

  1. #1
    Registered User
    Join Date
    07-15-2010
    Location
    Omaha, Nebraska
    MS-Off Ver
    Excel 2007
    Posts
    32

    Pass Input Box VBA Variable to SQL Query

    Hello,

    I need to prompt the user for a date and then send it to the SQL query I have running in my 2007 Excel VBA. I am using Windows Server 2000 if that makes a difference. I know I can use in input box to get the date from the user and assign it to a variable in VB. My issue is how do I then get it into my SQL script? The script works great now with a date hard coded in. It looks in the tables with 4 joins and gets all the data as I need it.

    I would also like to total the figures after I am done. I am struggling with that as I can do it with a macro but I want the report to be done with one push of the button for the user. All I am returning is 2 fields from SQL. An invoice number and a Tax. It can be anywhere from 25 to 125 entries each day. I am willing to total all 125 rows each day if I can't select what is there.

    Thanks in Advance for any help.

    Charlie
    Last edited by Zoomer36; 09-15-2011 at 11:33 AM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,440

    Re: Pass Input Box VBA Variable to SQL Query

    Might be useful to share the code you have ...
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    07-15-2010
    Location
    Omaha, Nebraska
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Pass Input Box VBA Variable to SQL Query

    Thanks. Here is my code.

    Please Login or Register  to view this content.
    Last edited by Zoomer36; 09-14-2011 at 03:26 PM.

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,440

    Re: Pass Input Box VBA Variable to SQL Query

    You need to add code tags as per the forum rules before anyone can provide an answer.

    Regards

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,440

    Re: Pass Input Box VBA Variable to SQL Query

    I can't test this but I'm guessing it would look something like:

    Please Login or Register  to view this content.


    Regards

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

    Re: Pass Input Box VBA Variable to SQL Query

    You'll need to format it first, I think:
    Please Login or Register  to view this content.
    Remember what the dormouse said
    Feed your head

  7. #7
    Registered User
    Join Date
    07-15-2010
    Location
    Omaha, Nebraska
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Pass Input Box VBA Variable to SQL Query

    Hi There!

    Thanks! It works like a champ! Really appreciate it.

    How about the totaling of the column? I want to put the total in cell C2. This is what I have now from recording a macro

    'Figure Totals here and put in cell C2
    .Range("C2").Select
    .Range("C2").Value = SUM(B2:B125)
    .Range("C3").Select

    The line where is says Value = SUM(B2:B125) gives me an error. It highlights the colon ":" between the two cell numbers.

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,440

    Re: Pass Input Box VBA Variable to SQL Query

    @RS: thought report_date was coming in as a string.

    Regards

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

    Re: Pass Input Box VBA Variable to SQL Query

    You are correct - overlooked that. In that case, I suspect:
    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    07-15-2010
    Location
    Omaha, Nebraska
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Pass Input Box VBA Variable to SQL Query

    You both are very good! It actually worked without any formatting at all. I put the formatting in just to be safe and it still works like a champ!

    Any hints on getting a total of the column?

    Thanks!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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