Solved

Has Data

Posted on 2014-03-23
3
268 Views
Last Modified: 2014-03-23
Can I find out if a form HasData before I try to open it ?

I have tried this

If Me.Forms!AppointmentsAll.HasData = -1 Then
    DoCmd.OpenForm "AppointmentsAll"
End If

It doesn't work

Derek
0
Comment
Question by:DatabaseDek
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 48

Accepted Solution

by:
Dale Fye (Access MVP) earned 500 total points
ID: 39948538
No, but you can find out whether the table or query that the form is based upon has data.

If fnHasData(TableName) Then Docmd.OpenForm "AppointmentsAll"

Change 'TableName' in the above to the name of the table or query that the form is based upon.  Then add this function to your code.
Public Function fnHasData(TableName as string) as boolean

    Dim strSQL as string
    Dim rs as DAO.Recordset

    strSQL = "SELECT Top1 * FROM [" & TableName & "]"
    set rs = currentdb.openrecordset(strsql,, dbfailonerror)

    fnHasData = iif(rs.eof, false, true)

ProcExit:
    OnError Resume next
    rs.close
    set rs = nothing
    Exit Function

ProcError:
    fnHasData = false
    Debug.print "fnHasData", err.number, err.description
    msgbox err.number & vbcrlf & err.description, , "fnHasData error"
    Resume ProcExit

End function

Open in new window

Or, you could simply use DLOOKUP() to see whether the forms recordsource has any records:
If Isnull(DLOOKUP("FieldName", "TableName")) = False Then 
    docmd.openform "AppointmentsAll"
endif

Open in new window

0
 

Author Closing Comment

by:DatabaseDek
ID: 39948547
Thank you!!

Derek
0
 
LVL 48

Expert Comment

by:Dale Fye (Access MVP)
ID: 39948553
You could also use the Form_Open event to determine whether there are any records in the form.

Private Sub Form_Open(Cancel As Integer)

    Cancel = (Me.RecordsetClone.RecordCount = 0)
   
End Sub

However, if you use this technique, you will need to handle the error that occurs when you cancel the form_Open event.  This would have to be done in the routine that calls the form, so it is better to simply test before hand.
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question