Results 1 to 3 of 3
  1. #1
    karambos is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Dec 2013
    Posts
    4

    how to prompt for input then populate SQL query

    I have a file that I bring into Access. Then I execute an SQL query on it and output the results. I've got it working in the following manner:



    Code:
    Function Proc_daymacro()
    On Error GoTo Proc_daymacro_Err
        DoCmd.SetWarnings False
        DoCmd.DeleteObject acTable, "3days"
        DoCmd.TransferText acImportDelim, "3days Import Specification", "3days", "3days.csv", True, ""
        DoCmd.OpenQuery "All3", acViewNormal, acReadOnly
        DoCmd.TransferText acExportFixed, "3daysExportSpecification", "All3", "x3rd_day_17003.txt", True, ""
        DoCmd.SetWarnings True
    
    Proc_daymacro_Exit:
        Exit Function
    Proc_daymacro_Err:
        MsgBox Error$
        Resume Proc_daymacro_Exit
    End Function
    and the SQL query, All3, looks like this:
    Code:
    SELECT Serial
    FROM 3days
    WHERE date>=#12/2/2013#
    GROUP BY Serial
    HAVING count(*)=3;
    Having got the above working, I would like to prompt the user for the date specified in the SQL query and dynamically insert it.

    In An attempt to just get it working with a fixed date, this is the best I could come up with and it doesn't work:

    Code:
    Function Proc_daymacro2()
    On Error GoTo Proc_daymacro_Err
       
          
        Dim strSQL As String
        strSQL = "SELECT Serial " & _
                "FROM 3days " & _
                "WHERE Date >= #12/2/2013# " & _
                "GROUP BY Serial " & _
                "HAVING count(*)=3"
           
        DoCmd.SetWarnings False
        DoCmd.DeleteObject acTable, "3days"
        DoCmd.TransferText acImportDelim, "3days Import Specification", "3days", "3days.csv", True, ""
        DoCmd.RunSQL strSQL
        DoCmd.TransferText acExportFixed, "3daysExportSpecification", "All3", "x3rd_day_17003.txt", True, ""
        DoCmd.SetWarnings True
    
    Proc_daymacro_Exit:
        Exit Function
    Proc_daymacro_Err:
        MsgBox Error$
        Resume Proc_daymacro_Exit
    End Function
    I just found out that "DoCmd.RunSQL" can only be used if you are doing an INSERT, DELETE etc because it doesn't return a result. However, I cannot find out how to make it work.

    I am grateful for any input

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,633
    Use a dynamic parameterized query object.

    The parameter input can be a popup prompt or reference to a control on a form.

    I never use popup prompt because can't validate user input.

    The All3 query sql would be:

    SELECT Serial FROM 3days WHERE Date >= [enter date value] GROUP BY Serial HAVING Count(*)=3

    or

    SELECT Serial FROM 3days WHERE Date >= [Forms]![formname]![controlname] GROUP BY Serial HAVING Count(*)=3

    Then go back to the OpenQuery code.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    "Date" is a poor name for a field because it is a reserved word in Access ("Date" is a command and a function). If you must use "Date", enclose it in brackets - [Date]..

    "Help" states : filename - A string expression that's the full name, including the path, of the text file you want to import from, export to, or link to. You don't have a path.

    If importing a CSV file multiple times AND the structure will always be the same, deleting the table, then recreating the table is inefficient.
    It would be better to delete the records in the table"3days", then import the CSV file.

    Creating a query (strSQL) in code allows you to use the recordset in code, usually by stepping through the record one record at a time.
    To use the query you have now, you would need to create a saved query or creat a temporary query, then use code to modify the SQL of the saved query. You could then use the query in the DoCmd.TransferText command.

    See Help "Query Def" for examples....


    Edit: June beat me again.....

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

Similar Threads

  1. VBA and DOM to populate innerHTML on an input tag
    By callmeandy in forum Programming
    Replies: 5
    Last Post: 06-19-2013, 05:32 PM
  2. Using Like to prompt input of multiple values
    By eskybel in forum Queries
    Replies: 6
    Last Post: 01-16-2013, 03:45 PM
  3. Replies: 0
    Last Post: 03-03-2012, 03:43 PM
  4. Accept user input to populate new record
    By bbrazeau in forum Forms
    Replies: 1
    Last Post: 01-03-2012, 05:45 PM
  5. Find duplicates query to populate input form
    By kctalent in forum Queries
    Replies: 6
    Last Post: 08-22-2011, 03:12 PM

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