Results 1 to 14 of 14
  1. #1
    uncletreetrunk is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Jul 2012
    Posts
    72

    One Form, Two Queries?

    Hi all,

    I am relatively new to access and I was wondering if I could run two queries off of one form. i.e. based on user selected fields run query 1 or query 2

    I am running two queries because query 2 has a table relationship, but not all the values have partners so the maximum number of results is far less running query 2.

    Or should I just run two separate forms?

    Thanks!

  2. #2
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    You could create an IF statement in VBA that will run Query1 or Query2, depending on your selection, i.e.
    Code:
    If Me.strSelection=1 Then
       DoCmd.OpenQuery "Query1", acViewNormal, acEdit
    Else
       DoCmd.OpenQuery "Query2", acViewNormal, acEdit
    End If

  3. #3
    RayMilhon is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,085
    The short answer is yes. However, it really depends on exactly what you're trying to do. For example I have a Reporting Database that links to SQL Server Data. There are about a dozen Reports in this Database. I have a form that uses a combo box to list all of the reports and then depending on which report is selected the criteria elements are displayed within the form the user enters their criteria and there are 2 command buttons 1 to display the report in print preview and 1 to send the report directly to the printer without displaying on the screen.

  4. #4
    uncletreetrunk is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Jul 2012
    Posts
    72
    Hey JoeM,

    I have a button with VBA command similar to that but when I run it I get Error 3071. "too complex to be evaluated" Is there a limit to how many fields you can pass?

  5. #5
    uncletreetrunk is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Jul 2012
    Posts
    72
    Hey RayMilhon,

    Good to hear! haha I want to have a user run report and if they check a box or enter a specification number run a different query

  6. #6
    RayMilhon is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,085
    so what you're looking for is a single report that could use either of 2 queries. Correct?

  7. #7
    uncletreetrunk is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Jul 2012
    Posts
    72
    yes that is correct! but it seems when I run the second query it's "too complicated" is there a way to get around it or a limit to how many fields I can pass to a query?

  8. #8
    RayMilhon is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,085
    A method I've used in the past.

    In the load Event of the report First check to be sure that the Form is loaded.
    Code:
       
    
    If not isformopen("yourformname") Then
             MsgBox "The Form Your Form Name is not open please open the form and re-run the report",vbokonly,"Missing Form Error"        
             cancel = true
       Else
            If isnull(forms!yourformname!criteriafield)  Then
                  me.recordsouce = "Query1"
            Else
                  me.recordsource = "query2"
            End if
       End if
    In a module unclude the following function.

    Code:
    Function isformopen(strfname as string) as integer
       'This function returns true if a form is open:
       'This function from the Access 2007 Bible
    
       dim i as integer
       'Assume False;
       isformopen = false
       For i = 0 to forms.count -1
            If Forms(i).name = strfname Then
               isformopen = true
               exit function
            end if
      next
    
    End function

  9. #9
    RayMilhon is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,085
    Left off 1 end if in the first code section.

  10. #10
    uncletreetrunk is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Jul 2012
    Posts
    72
    mmm I think you're okay on your "end if"s
    I'll give the code a shot and let you know thanks so much!

  11. #11
    uncletreetrunk is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Jul 2012
    Posts
    72
    Whoops what I really wanted was 1 form that could run either query, but the next question would be to print 1 report from both queries. So thank you for your esp haha

  12. #12
    RayMilhon is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,085
    Same principle applies the diffenece being that you would have a command button on the form and put very similar code in the on click event of the command button.

  13. #13
    uncletreetrunk is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Jul 2012
    Posts
    72
    Hm... alright I'll give it a shot! haha thanks for all the help Ray!

  14. #14
    RayMilhon is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,085
    Anytime. My way of passing on all the help I've gotten over the years.

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

Similar Threads

  1. Running Queries from a form
    By HaYuM in forum Queries
    Replies: 1
    Last Post: 08-23-2013, 03:12 AM
  2. multiple queries in form
    By geraldk in forum Forms
    Replies: 5
    Last Post: 12-26-2011, 02:22 PM
  3. Form queries. Multiple checkboxes.
    By radink in forum Queries
    Replies: 4
    Last Post: 04-27-2011, 07:34 AM
  4. form with different queries
    By bbeernaert in forum Queries
    Replies: 2
    Last Post: 08-26-2010, 03:11 AM
  5. Queries to Form Problem
    By PnerraD in forum Queries
    Replies: 1
    Last Post: 06-10-2006, 09:13 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