Connecting Tech Pros Worldwide Help | Site Map

Display Message if No Data Exist through Query / Form

Newbie
 
Join Date: Aug 2007
Posts: 31
#1: Aug 25 '07
I have created a form, which has 'Query' datasource.

I want a message if query does not show any record, as occured in report of 'On No Data'
missinglinq's Avatar
Moderator
 
Join Date: Nov 2006
Location: Richmond, Virginia USA
Posts: 2,992
#2: Aug 25 '07

re: Display Message if No Data Exist through Query / Form


Use DCount() to check for records returned by query:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Open(Cancel As Integer)
  2. Dim Msg As String
  3. Dim intX As Integer
  4.  
  5. intX = DCount("[PrimaryKeyField]", "YourQueryName")
  6. If intX < 1 Then
  7. Msg = "No Records are Returned by the Query"
  8. MsgBox Msg, vbOKOnly + vbInformation
  9. DoCmd.Close acForm, "YourFormName"
  10. End If
  11. End Sub
  12.  
Linq ;0)>
Jim Doherty's Avatar
Moderator
 
Join Date: Aug 2007
Location: Derbyshire,England
Posts: 639
#3: Aug 25 '07

re: Display Message if No Data Exist through Query / Form


Quote:

Originally Posted by Sanjaylml

I have created a form, which has 'Query' datasource.

I want a message if query does not show any record, as occured in report of 'On No Data'

Not sure whether you want it for form or report so heres both

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Open(Cancel As Integer)
  2. On Error Resume Next
  3.     If Me.RecordsetClone.RecordCount = 0 Then
  4.     MsgBox "No records to support form display", vbExclamation, "System Message"
  5.     DoCmd.CancelEvent
  6.     Exit Sub
  7.     End If
  8. Exit Sub
and one for the reports on nodata event

Expand|Select|Wrap|Line Numbers
  1. Private Sub Report_NoData(Cancel As Integer)
  2. On Error Resume Next
  3.     Cancel = True
  4. MsgBox "No records to support report", vbExclamation, "System Message"
  5. End Sub
Reply