Results 1 to 5 of 5
  1. #1
    emanon is offline Novice
    Windows 10 Access 2013 32bit
    Join Date
    Apr 2020
    Posts
    8

    Onclick event for button, build a query string and run it


    I have a MS Access form where it will ask users to select or type in words that will be used in the where clause for the query. When the button is clicked, it will build the string and use that to run the query. This will be a read only and to view the subset results from a link table in SQL Server. Nothing more. I tried it with DoCmd.RunSQL but that is only for action type like update, delete, etc.
    On click button event, it will have similar query. strSQL = "Select top 10 * Where ID = ''" + textbox1.value + "'' from linked_Table_Name;"
    This did not work. DoCmd.RunSQL strSQL

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    you dont need to build an SQL, just run the query using the same sql:
    query Q1:
    Select top 10 * Where ID =forms!fMyForm!txtBox1 from linked_Table_Name

    your code:
    docmd.openquery "Q1"


  3. #3
    emanon is offline Novice
    Windows 10 Office 365
    Join Date
    Apr 2020
    Posts
    8
    This is in the click event. I get Microsoft Access can't find object 'Select top 10 * from RPT_Compliance_Dashboard_Details;' I am not sure what I am doing wrong. Once I get the basic query to work, then I can add the where clause and do that part.

    Dim strSQL As String
    strSQL = "Select top 10 * from RPT_Compliance_Dashboard_Details;"

    DoCmd.OpenQuery strSQL

  4. #4
    emanon is offline Novice
    Windows 10 Office 365
    Join Date
    Apr 2020
    Posts
    8
    I got it to work. I created an empty saved query then did the below stuff in on click event.

    Dim strSQL As String

    strSQL = "Select top 10 * from RPT_Compliance_Dashboard_Details;"


    Dim qdf As DAO.QueryDef
    Set qdf = CurrentDb.QueryDefs("qryResult")
    qdf.SQL = strSQL
    DoCmd.OpenQuery ("qryResult")
    Set qdf = Nothing

  5. #5
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    or just docmd.openquery

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

Similar Threads

  1. Dynamically Build Query String
    By Juan4412 in forum Programming
    Replies: 6
    Last Post: 05-24-2017, 10:11 AM
  2. Replies: 11
    Last Post: 05-14-2017, 08:43 AM
  3. Button OnClick to trigger Query results
    By rhubarb in forum Programming
    Replies: 9
    Last Post: 05-18-2014, 10:38 AM
  4. Replies: 6
    Last Post: 10-11-2012, 02:19 PM
  5. Replies: 1
    Last Post: 10-06-2011, 02:28 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