O
OssieMac
This question is really one of a curiosity nature because I have a
workaround. However, I would just like to know why something does not work.
I have a continuous form where the user enters a value in the Form Header
and is presented with an on screen report.
The user then has one button to Print the report and another to Export the
data to Excel.
The Record Source for the form is a table named [Report Receipts In].
So that the user is not presented with data from a previous query, I delete
all the records from the table with the following code in the Forms On Load
event:-
CurrentDb.Execute "Delete * from [Report Receipts In]"
The table is re-created with the following code which runs a Make Table
Query after the user enters the required parameter.
Dim stDocName As String
'Remove the forms record source so the Query can use the table
Me.RecordSource = ""
stDocName = "Receipts In"
DoCmd.SetWarnings False
DoCmd.OpenQuery stDocName, acNormal, acEdit
DoCmd.SetWarnings True
'Reattach the forms record source
Me.RecordSource = "Report Receipts In"
Me.Requery
Me.Recalc
All the above works fine. However, the form header has a Text Box with the
following formula as the Control Source to count the number of records
returned:-
=DCount("*","[Report Receipts In]")
Now my question is if no records are returned, why don’t I get a zero in the
Text Box with the above formula and in VBA why doesn’t the following code
work:-
If Me.Number_Records = 0 Then 'Error on this line
MsgBox "No data to export."
Exit Sub
End If
Error details of the code are:-
Runtime error 2427
You entered an expression that has no value.
I have tried various combinations of the formula in the Text Box and in the
VBA code by nesting it with the Nz function to return Zero and Zero length
string and it still doesn’t work.
It all works fine if records are returned and a value greater than Zero is
returned in the field.
The following VBA code does work and hence I use it for the test in lieu of
the above. However, as the Dcount function is the same as the Text Box
Control source, it has simply heightened my curiosity.
If DCount("*", "[Report Receipts In]") = 0 Then
MsgBox "No data to export."
Exit Sub
End If
As always, all replies will be greatly appreciated.
workaround. However, I would just like to know why something does not work.
I have a continuous form where the user enters a value in the Form Header
and is presented with an on screen report.
The user then has one button to Print the report and another to Export the
data to Excel.
The Record Source for the form is a table named [Report Receipts In].
So that the user is not presented with data from a previous query, I delete
all the records from the table with the following code in the Forms On Load
event:-
CurrentDb.Execute "Delete * from [Report Receipts In]"
The table is re-created with the following code which runs a Make Table
Query after the user enters the required parameter.
Dim stDocName As String
'Remove the forms record source so the Query can use the table
Me.RecordSource = ""
stDocName = "Receipts In"
DoCmd.SetWarnings False
DoCmd.OpenQuery stDocName, acNormal, acEdit
DoCmd.SetWarnings True
'Reattach the forms record source
Me.RecordSource = "Report Receipts In"
Me.Requery
Me.Recalc
All the above works fine. However, the form header has a Text Box with the
following formula as the Control Source to count the number of records
returned:-
=DCount("*","[Report Receipts In]")
Now my question is if no records are returned, why don’t I get a zero in the
Text Box with the above formula and in VBA why doesn’t the following code
work:-
If Me.Number_Records = 0 Then 'Error on this line
MsgBox "No data to export."
Exit Sub
End If
Error details of the code are:-
Runtime error 2427
You entered an expression that has no value.
I have tried various combinations of the formula in the Text Box and in the
VBA code by nesting it with the Nz function to return Zero and Zero length
string and it still doesn’t work.
It all works fine if records are returned and a value greater than Zero is
returned in the field.
The following VBA code does work and hence I use it for the test in lieu of
the above. However, as the Dcount function is the same as the Text Box
Control source, it has simply heightened my curiosity.
If DCount("*", "[Report Receipts In]") = 0 Then
MsgBox "No data to export."
Exit Sub
End If
As always, all replies will be greatly appreciated.