how to call my Query from VB code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mabsalam
    New Member
    • Aug 2014
    • 7

    how to call my Query from VB code

    I have developed a database that keep the records of my client's hardwares and softwares. I created a form with a button against a textbox that will filter my records if a particular company name in typed. How should I call my sql query to display all the information related to any company name typed?
  • Seth Schrock
    Recognized Expert Specialist
    • Dec 2010
    • 2965

    #2
    First you would have your query reference the textbox as the criteria for the field you want filtered. You do this by entering the following in the criteria box for the field you want filtered:
    Code:
    Forms!YourFormName!TextboxName
    I would then recommend creating a form based on your query and open the form. This allows you to do more things with events, etc. The VBA code to open a form is
    Code:
    DoCmd.OpenForm "YourFormNameHere"

    Comment

    • mabsalam
      New Member
      • Aug 2014
      • 7

      #3
      @Seth Schrock don't understand this. Let me explain further the problem I am facing. My database has four forms, COMPANY,HARDWAR E,SOFTWARE and REPORT FILTER. My Query (Query1) displays the records from both hardware and software because there is referential integrity between the two. My Query go thus
      Code:
      SELECT HARDWARE.[COMPANY NAME], HARDWARE.DEVICE_ID, HARDWARE.[DEVICE TYPE], HARDWARE.[USER NAME], HARDWARE.PROCESSOR, HARDWARE.RAM, HARDWARE.[HARD DISK], HARDWARE.[DATE PURCHASED], HARDWARE.[LAST SERVICE DATE], HARDWARE.[IDENTIFIED PROBLEM], HARDWARE.STATUS, SOFTWARE.[SOFTWARE NAME], SOFTWARE.[DATE INSTALLED], SOFTWARE.[EXPIRING DATE], SOFTWARE.NETWORK
      FROM HARDWARE INNER JOIN SOFTWARE ON HARDWARE.DEVICE_ID = SOFTWARE.DEVICE_ID;
      REPORT FILTER FORM consists of a button and textBox1. I want the button to trigger the Query1 event if clicked and display the record based on the company name supplied in textBox1.
      Last edited by NeoPa; Aug 14 '14, 11:17 PM. Reason: Added [CODE] tags.

      Comment

      • Seth Schrock
        Recognized Expert Specialist
        • Dec 2010
        • 2965

        #4
        So to get your filter, you would add the following before the semi-colon at the end of your query:
        Code:
        WHERE HARDWARE.[COMPANY NAME] = Forms![Report Filter]!TextBox1

        Comment

        • mabsalam
          New Member
          • Aug 2014
          • 7

          #5
          thanks @Seth Schrock this work like charm.

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32663

            #6
            A more standard way would be to add the filtering to the .Filter property of your form and set .FilterOn to True. This doesn't involve complicating the underlying query.

            Comment

            Working...