Solved

Has Data

Posted on 2014-03-23
3
262 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
  • 2
3 Comments
 
LVL 47

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 47

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

759 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now