PC Review


Reply
Thread Tools Rate Thread

Pass Form Field to Public Function

 
 
Jack Leach
Guest
Posts: n/a
 
      17th Sep 2009
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)

 
Reply With Quote
 
 
 
 
Allen Browne
Guest
Posts: n/a
 
      17th Sep 2009
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.

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.


"Jack Leach" <dymondjack at hot mail dot com> wrote in message
news63E6D8B-D71D-4AA8-9334-(E-Mail Removed)...
> 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)
>

 
Reply With Quote
 
Banana
Guest
Posts: n/a
 
      17th Sep 2009
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")

Jack Leach wrote:
> 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.
>

 
Reply With Quote
 
Jack Leach
Guest
Posts: n/a
 
      17th Sep 2009
?TypeName(Me.Fieldname)
AccessField


--
Jack Leach
www.tristatemachine.com

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



"Banana" wrote:

> 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")
>
> Jack Leach wrote:
> > 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.
> >

>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Use Public Function UserName to populate Form field and bind it to table DIDS Microsoft Access Form Coding 4 28th May 2011 03:00 PM
pass parameter from form field to sql function =?Utf-8?B?RGF6emxl?= Microsoft Access Reports 1 13th Nov 2006 10:22 PM
How to pass a query field result to a form field =?Utf-8?B?VGRhd2c=?= Microsoft Access Queries 1 11th Nov 2005 06:56 AM
How to pass a value from a Public function to a report? =?Utf-8?B?Q2hhcmxlcw==?= Microsoft Access Reports 1 20th Jul 2005 01:15 AM
pass a calculated form field to a subform field? =?Utf-8?B?QnJvb2s=?= Microsoft Access Forms 22 28th Apr 2005 05:56 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:59 PM.