Validate if data exists

S

SoggyCashew

Hello, I have a button on my form and in this buttons OnClick event I have
=ShowReport("L_iCalendar12") which opens the report L_iCalendar12 but only if
there is data selected in the function "ShowReport". All this works but I
needed to add a validation. I need to also check if there is data in a query
named qryYearView and if ther isnt then I need a mssgbox telling me and then
set focus to cboUser. I have no clue how to do this can someone help? Thanks!

Here is what I have:

Private Function ShowReport(ByVal RName As String)

If Me.cboDepartment.ListIndex = "-1" Then
MsgBox "You must make sure yor criteria's" & Chr(13) & _
"are selected in before proceeding." & Chr(13) & _
"" & Chr(13) & _
" - Please select a Department" & Chr(13) & _
" - Please select an Employee", vbCritical, "Selection Error"
Me.cboDepartment.SetFocus
Exit Function
End If

If Me.cboUser.ListIndex = "-1" Then
MsgBox "You must make sure yor criteria's" & Chr(13) & _
"are selected in before proceeding." & Chr(13) & _
"" & Chr(13) & _
" - Please select a Department" & Chr(13) & _
" - Please select an Employee", vbCritical, "Selection Error"
Me.cboUser.SetFocus
Exit Function
Else
DoCmd.OpenReport RName, acPreview
End If
End Function
 
A

Arvin Meyer [MVP]

You do know that reports have a HasNoData property that will pretty much do
this for you. Open the report in design view, then open the property sheet
and go to the event tab and open a code window for On No Data. Then paste
this in:

Private Sub Report_NoData(Cancel As Integer)
MsgBox "No data to print", vbOKOnly, "NO DATA"
Cancel = True
End Sub
 
S

SoggyCashew

I had already tried using that. This is why I wanted to check the query for
data before running anything. I figured it out by adding the code below.
Thanks!

If DCount("*", "qryYearView", "") = 0 Then
MsgBox "Code Added to stop form open if no data"
Exit Function
End If

--
Thanks,
Chad


Arvin Meyer said:
You do know that reports have a HasNoData property that will pretty much do
this for you. Open the report in design view, then open the property sheet
and go to the event tab and open a code window for On No Data. Then paste
this in:

Private Sub Report_NoData(Cancel As Integer)
MsgBox "No data to print", vbOKOnly, "NO DATA"
Cancel = True
End Sub
 
G

Guest

SoggyCashew said:
Hello, I have a button on my form and in this buttons OnClick event I have
=ShowReport("L_iCalendar12") which opens the report L_iCalendar12 but only
if
there is data selected in the function "ShowReport". All this works but I
needed to add a validation. I need to also check if there is data in a
query
named qryYearView and if ther isnt then I need a mssgbox telling me and
then
set focus to cboUser. I have no clue how to do this can someone help?
Thanks!

Here is what I have:

Private Function ShowReport(ByVal RName As String)

If Me.cboDepartment.ListIndex = "-1" Then
MsgBox "You must make sure yor criteria's" & Chr(13) & _
"are selected in before proceeding." & Chr(13) & _
"" & Chr(13) & _
" - Please select a Department" & Chr(13) & _
" - Please select an Employee", vbCritical, "Selection
Error"
Me.cboDepartment.SetFocus
Exit Function
End If

If Me.cboUser.ListIndex = "-1" Then
MsgBox "You must make sure yor criteria's" & Chr(13) & _
"are selected in before proceeding." & Chr(13) & _
"" & Chr(13) & _
" - Please select a Department" & Chr(13) & _
" - Please select an Employee", vbCritical, "Selection
Error"
Me.cboUser.SetFocus
Exit Function
Else
DoCmd.OpenReport RName, acPreview
End If
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top