MS Access 2010: Visual Basic: Get the number of rows from an open query window

Hello

I would like to code in MS Access 2010: Visual Basic a function that close all open query windows that have no rows.

Where can I find in MS Access 2010: Visual Basic the list with the open query window ?

How do I test if there are rows in the query window ?

Sincerely
      
Ivo Raemy
Ivo RaemyAnalyst ProgrammerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gustav BrockCIOCommented:
You can close all forms like this:
Public Function CloseFormsAllBut(Optional ByVal intNoClose As Integer = 1) As Boolean

    Dim booSuccess  As Boolean
    Dim intForm     As Integer
    Dim intForms    As Integer
    
    On Error GoTo Err_CloseFormsAllBut
    
    intForms = Forms.Count
    If intForms > intNoClose Then
        ' Close all forms in reverse order.
        For intForm = intForms - 1 To intNoClose
            CloseForm Forms(intForm).Name
        Next
    End If
    If Forms.Count > intNoClose Then
        ' Not all forms could be closed.
        DoCmd.Beep
    Else
        booSuccess = True
    End If
    
    CloseFormsAllBut = booSuccess

Exit_CloseFormsAllBut:
    Exit Function
    
Err_CloseFormsAllBut:
    Call ErrorMox
    Resume Exit_CloseFormsAllBut


End Function

Open in new window

To check if a form has records:

If Forms(n).RecordsetClone.RecordCount = 0 Then
     ' No records.
Endif

where n is a form index as used above in the function, or:

If Forms("NameOfForm").RecordsetClone.RecordCount = 0 Then
     ' No records.
Endif

if you wish to use the name of the form.

/gustav
Ivo RaemyAnalyst ProgrammerAuthor Commented:
Hello Gustav

This works for form. However, I open the queries with

OpenQuery queriesName, acViewNormal, acReadOnly

The Queries are not in the list Forms.

I would do the test:

 
For each query in Querys
  IF query .RecordCount = 0 THEN
      query .CloseQuery()
 END IF
Next



Any Idea?
Gustav BrockCIOCommented:
I don't think you can do this. You can loop the QueryDefs collection and:

    Set rs = QueryDefs(n).OpenRecordset()

but that will open any of these not already opened.

You can also do:

    QueryDefs(n).Close

but that doesn't fail if the query was closed, so that would close all queries, not counting records.

/gustav
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Jeffrey CoachmanMIS LiasonCommented:
Or, you could not even open the query if it has no records...
Something roughly like this:
(If the query only has more than 0 records, open it )
IF Dcount("*","YourQuery")>0 then
   Docmd.OpenQuery "YourQuery"
end if

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jeffrey CoachmanMIS LiasonCommented:
I mean, why even open it, ...if you need to go through a lot of trouble (or code) to close it anyway?
Jeffrey CoachmanMIS LiasonCommented:
Glad I could help...
Enjoy your weekend.
;-)

Jeff
Ivo RaemyAnalyst ProgrammerAuthor Commented:
Thank you very much. That was a great solution.

Ivo
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.