Results 1 to 2 of 2
  1. #1
    accessprogramer is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    1

    Pass a VB variable to SQL command

    I have the following code and what I would like to do is find the oldest purchase date and delete a the number of shares entered in the InputBox. This code works to delete all of DIA, put I can't get it to pass the VB StockTicker value into the SQL code...

    Dim Message, Title, Default, StockTicker, Shares
    Title = "Sell Stocks"
    Default = ""
    StockTicker = InputBox("Which stock ticker name would you like to SELL?")


    StockTicker = UCase(StockTicker)
    If StockTicker = Empty Then Exit Sub

    Shares = InputBox("How many shares would you like to SELL?")

    If MsgBox("You have chosen to SELL " & Shares & " shares of " & StockTicker & ".", vbQuestion + vbYesNo) = vbYes Then DoCmd.RunSQL "DELETE * FROM [TBL-Purchases] WHERE [TBL-Purchases].[Stock_ID] = 'DIA'" Else: Exit Sub


    Eventually, I want to find the oldest purchase date and delete the number of shares entered as 'Shares' in my InputBox...

    DELETE *
    FROM [TBL-Purchases]
    WHERE [TBL-Purchases].[Stock_ID]=StockTicker & FIRST([TBL-Purchases].[Purchase Date]), FIRST([TBL-Purchases].[Number of Shares])
    GROUP BY [TBL-Purchases].[Stock_ID];

  2. #2
    NTC is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2009
    Posts
    2,392
    I don't consider your code syntax to be viable for Access - and so won't attempt to approach it as posted.

    1. find the oldest purchase date
    ** set up a Aggregate query using the Min option

    2. and delete a the number of shares entered in the InputBox
    ** set up an Update query - referenced upon the first aggregate query

    Then trigger the Update query as appropriate within the user interface sequence of events.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 10-31-2013, 08:10 PM
  2. Replies: 15
    Last Post: 04-21-2011, 02:50 PM
  3. Pass variable name to another forms
    By Thomman in forum Access
    Replies: 3
    Last Post: 04-07-2011, 10:01 AM
  4. VBA Pass-through Query with Variable
    By smaumau in forum Programming
    Replies: 0
    Last Post: 12-06-2010, 09:10 AM
  5. Replies: 5
    Last Post: 04-05-2010, 11:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums