Pass Form Field to Public Function

J

Jack Leach

Hello, thanks in advance.

I'm making an attempt to globalize a function, and looking for the best
method, if anyone has any thoughts.

My intent is to pass a field of the Form's recordset (rather than a control)
to a public function for update.

fldCrDate, fldCrBy, fldModDate, fldModBy... these are what I am trying to
pass to function SetTimeStamps(DateField, UserField)

I've done this enough times with Controls, but I thought it would be a
little cleaner to not put these controls on the form as hidden just for this
purpose.

If I declare the arguments as Field, I get a Type Mismatch. I switched to
Variant and the code didn't fail at runtime, but nothing was updated.

'=======
Public Function SetTimeStamps( _
ByRef DateField As Variant, _
ByRef UserField As Variant _
) As Boolean

DateField = Now()

If Len(Nz(gUSER, "")) <> 0 Then
UserField = gUSER
End If

End Function
'=======


and in the form's BeforeUpdate:

'=======
If Me.NewRec Then
SetTimeStamps Me.fldCrDate, Me.fldCrBy
End If

SetTimeStamps Me.fldModDate, Me.fldModBy
'=======



Should I instead try to pass the form's Recordset to the function? Or am I
stuck using hidden controls.

--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)
 
A

Allen Browne

To refer to the field rather than the control, you will need to ensure the
field name and control name are different.

Then try passing an object of type AccessField. While I haven't tried it
that should work.

My preferred approach is to use the same field name across all forms (even
if you have to alias it in a query), and then pass the Form to the function,
e.g.:
Call MyFunction(Me)
The receiving function is declared:
Public MyFunction(frm As Form)
and you can then do anything with frm that you could have done with Me.
 
B

Banana

What if you tried being more explicit, using "DAO.Field"?

Failing that, run in the immediate windows:

?Typename(Me.fldCrDate)

To determine the actual type and use that instead.

Finally, if this still doesn't work, use the recordset property
(available in Access 2000 and later)

Me.Recordset.Fields("fldCrDate")
 

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