Solved

Has Data

Posted on 2014-03-23
3
267 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

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…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

730 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