DCount improper use of null

  • Thread starter Thread starter SoggyCashew
  • Start date Start date
S

SoggyCashew

Hello I have a function that is giving me an error "Inproper use of null" I
am running this to verify there is data in a query named qryQearView and if
there isnt then it exits function and sets focus to a text box. What am I
doing wrong?

Private Function ShowReport(ByVal RName As String)
Dim Emp As String
Dim datYr As String

Emp = Forms!frmCalendar!cboUser.Column(2)
datYr = Forms!frmCalendar!CalYear

If DCount("*", "qryYearView", "") = 0 Then
MsgBox "There is no attendance data for " & Emp & " in the year " &
datYr & Chr(13) & _
"" & Chr(13) & _
"The calendar can only be viewed if data is available!", vbCritical, "No
Data Error"
Exit Function
End If

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 Branch" & 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 Branch" & 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
 
If nothing's selected in the combo box, Forms!frmCalendar!cboUser.Column(2)
will return Null. If nothing's in Forms!frmCalendar!CalYear, it will return
Null as well. String variables cannot hold Null values: the only data type
that can is the Variant.

While not related to the problem you're encountering, ListIndex is a numeric
property, not a text one, so your If statements should be

If Me.cboDepartment.ListIndex = -1 Then

and

If Me.cboUser.ListIndex = -1 Then

To check whether anything's been selected, though, all you need do is see
whether the combo is Null:

If IsNull(Me.cboDepartment) Then

and

If IsNull(Me.cboUser) Then

I'm not sure why you've got the zero-length string in your DCount statement.

DCount("*", "qryYearView")

is all you need if there's no condition to be checked.
 

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

Back
Top