I hope I am not posting in conflict with what Marshall has saying. I hope
further that I am not leading you astray. I will say that it really is not
much more complicated that declaring a form variable. I have done this in
two ways:
Public Function FunctionName(frm as Form)
' Code
End Function
When I call the function:
FunctionName(Form)
The other way is:
Public Function FunctionName()
Dim frm as Form
' Code
End Function
Either way, Access "knows" that the form from which you are calling is the
form to which the code applies. In your case it could be:
Function InsStatus(frm as Form)
If IsNull(frm.txbExpSoonest) Then 'No Expiration Date
If frm.chkInForce.Value = False Then 'No Date, No Checkbox: No
Insurance
InsStatus = "Absent"
Else 'No Date, Yes Checkbox: Need Date
etc.
Call it thus:
InsStatus(Form)
or:
Function InsStatus()
Dim frm As Form
If IsNull(frm.txbExpSoonest) Then 'No Expiration Date
If frm.chkInForce.Value = False Then 'No Date, No Checkbox: No
Insurance
InsStatus = "Absent"
Else 'No Date, Yes Checkbox: Need Date
etc.
Call it thus:
InsStatus()
That said, when I do this sort of thing it is to loop through controls or do
something at the form level such as determine the number of records and place
the value in an unbound text box txtCount:
frm.txtCount = frm.Recordset.RecordCount
In this small example each form where you want to use this code needs a text
box named txtCount, but it is a generic text box rather than one bound to a
specific field. If two forms are similar enough that they have a lot of the
same control names they may be similar enough that you would do better to
load the recordset at run time and use one form for two different purposes.
I don't understand just what you are trying to do, so these thoughts are very
general, but when code in a standard module references a lot of specific
controls it may be an indication that you are building something that could
be difficult to maintain.
Again, I want to stress that this is not intended to be in opposition to what
Marshall has written. He has far more experience and skill than I. What I
know today about Access is due in part to his replies to my questions and to
his discussions in many threads I have watched silently. I am just trying to
add what I can about the use of form variables.
I should have said "standard module". The function needs to be in a standard
module rather than being married to any one form.
But there must be more to it than declaring a form variable and using it in
place of the Me keyword. How do I pass the name of the calling form to the
function? I've been trying all afternoon. Correct syntax cannot be guessed,
yet I have very poor luck trying to systematically look it up in a book or
online.
Here's what I've got now, which doesn't work:
Function InsStatus(strCallForm As String) As String
Dim frm As Form
Set frm = Forms(strCallForm)
If IsNull(frm.txbExpSoonest) Then 'No Expiration Date
If frm.chkInForce.Value = False Then 'No Date, No Checkbox: No
Insurance
InsStatus = "Absent"
Else 'No Date, Yes Checkbox: Need Date
etc...
My Control Source for the text box is =InsStatus("MyFormName")
Access barks back that it can't find any such form.
The "Question" in my prior post was in context of suggestion made earlier by
Marshall that my text box was not being automatically recalculated because
Access had no way of knowing that the calculation depended on control values
that had been changed. In other words, if I understand, he was saying that
if I mentioned those control names as arguments in my function call, Access
would then decide to re-run the function anytime those controls were edited.
Whether this is true or not I don't know, but my approach of using a
function in the After Update events of the various controls to force the form
to recalculate does at least work.
Thanks,
Pew
Regarding your second point, I'm not sure why it would need to be a public
procedure in a class module rather than a standard module. Assuming the
[quoted text clipped - 67 lines]
control's AfterUpdate event to requery the text box with the
function call.
--
Message posted via AccessMonster.com
.