B
BruceM
I have the following code in a standard module:
Public Function CurrentEvents(frm As Form)
' Inserts current record number and total number of records
' (strCurrent, strTotal, strOf are defined in Declarations
' section of this module)
strCurrent = frm.CurrentRecord
frm.RecordsetClone.MoveLast
strTotal = frm.Recordset.RecordCount
strOf = strCurrent & " of " & strTotal
' Enable navigation buttons as needed
frm.cmdFirst.Enabled = Not frm.CurrentRecord = 1
frm.cmdPrevious.Enabled = Not frm.CurrentRecord = 1
frm.cmdNext.Enabled = (frm.CurrentRecord = 1 And
frm.Recordset.RecordCount > 1) _
Or frm.CurrentRecord < frm.Recordset.RecordCount
frm.cmdLast.Enabled = Not (frm.cmdNext.Enabled = False)
End Function
I call the function from the Current event of a form or subform. cmdFirst,
etc. are custom navigation buttons. I combine strOf with something specific
to the data displayed on the form (1 of 86 Employees, 4 of 8 Departments,
etc.), which is why I usually call it from the form's Current event rather
than placing the function name directly into the line for Current event on
the property sheet. Also, there is often something else in the Current
event.
As an aside, I don't know what to call it when I place a function name
directly into the space next to, say, a command button's Click event on the
button's property sheet, as opposed to using VBA to call the function from
within the Click event. It doesn't matter all that much, I suppose, but I
would like to know how to refer to the practice.
Anyhow, one of my forms contains two subforms, both of which contain the
same command buttons as the main form, and both of which call the
CurrentEvents function described above. Subform1 invariably contains at
least one record, but Subform2 may not contain any records (I know the
records are contained in the table to which the subform is bound and not in
the subform itself, and I ask that you accept the shorthand way of
expressing the situation). If both subforms in the record to which I am
navigating contain at least one record apiece everything is fine, but when
one of the subforms contains no records I receive Error 3021, No Current
Record in the CurrentEvents function.
For now I have used the function's error handling to bypass the error, but I
wonder if there is a better way. I have tried this after the strOf line:
If strTotal = 0 Then
Cancel = True
End If
I have also tried it with If strTotal = "0" and If frm.Recordset.RecordCount
= 0, but I get a "Variable not defined" compile error at Cancel. I tried
adding Cancel as Integer in the parentheses in the function name: Public
Function CurrentEvents(frm as Form, Cancel as Integer), but then I receive
an "Argument not optional" compile error (or type mismatch if Cancel as
Integer precedes frm as Form), but in any case it is a flawed approach, most
likely because there are large gaps in my knowledge of VBA. I would rather
address the cause of the error rather than ignoring it in the error
handling, unless ignoring it is a sound approach.
Public Function CurrentEvents(frm As Form)
' Inserts current record number and total number of records
' (strCurrent, strTotal, strOf are defined in Declarations
' section of this module)
strCurrent = frm.CurrentRecord
frm.RecordsetClone.MoveLast
strTotal = frm.Recordset.RecordCount
strOf = strCurrent & " of " & strTotal
' Enable navigation buttons as needed
frm.cmdFirst.Enabled = Not frm.CurrentRecord = 1
frm.cmdPrevious.Enabled = Not frm.CurrentRecord = 1
frm.cmdNext.Enabled = (frm.CurrentRecord = 1 And
frm.Recordset.RecordCount > 1) _
Or frm.CurrentRecord < frm.Recordset.RecordCount
frm.cmdLast.Enabled = Not (frm.cmdNext.Enabled = False)
End Function
I call the function from the Current event of a form or subform. cmdFirst,
etc. are custom navigation buttons. I combine strOf with something specific
to the data displayed on the form (1 of 86 Employees, 4 of 8 Departments,
etc.), which is why I usually call it from the form's Current event rather
than placing the function name directly into the line for Current event on
the property sheet. Also, there is often something else in the Current
event.
As an aside, I don't know what to call it when I place a function name
directly into the space next to, say, a command button's Click event on the
button's property sheet, as opposed to using VBA to call the function from
within the Click event. It doesn't matter all that much, I suppose, but I
would like to know how to refer to the practice.
Anyhow, one of my forms contains two subforms, both of which contain the
same command buttons as the main form, and both of which call the
CurrentEvents function described above. Subform1 invariably contains at
least one record, but Subform2 may not contain any records (I know the
records are contained in the table to which the subform is bound and not in
the subform itself, and I ask that you accept the shorthand way of
expressing the situation). If both subforms in the record to which I am
navigating contain at least one record apiece everything is fine, but when
one of the subforms contains no records I receive Error 3021, No Current
Record in the CurrentEvents function.
For now I have used the function's error handling to bypass the error, but I
wonder if there is a better way. I have tried this after the strOf line:
If strTotal = 0 Then
Cancel = True
End If
I have also tried it with If strTotal = "0" and If frm.Recordset.RecordCount
= 0, but I get a "Variable not defined" compile error at Cancel. I tried
adding Cancel as Integer in the parentheses in the function name: Public
Function CurrentEvents(frm as Form, Cancel as Integer), but then I receive
an "Argument not optional" compile error (or type mismatch if Cancel as
Integer precedes frm as Form), but in any case it is a flawed approach, most
likely because there are large gaps in my knowledge of VBA. I would rather
address the cause of the error rather than ignoring it in the error
handling, unless ignoring it is a sound approach.