Solved

Has Data

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

810 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