Link to home
Start Free TrialLog in
Avatar of shambalad
shambaladFlag for United States of America

asked on

Using a function to assign a default value for a field in table definition

When I define a date type field i am able to set a default value using the functions Date() or Now(). Are these unique exceptions, or is there a way I could assign other functions to load a default field default value? I have a public function the retrieves a Session ID (i.e. 'Public GetSessionID() As Long). I use this value to identify who, what and when a record was modified. I'd like to assign a default value of 'GetSessionID()' to the field SessionID, but I get an 'Unknown Function' error when I try to save it. I suspect I could accomplish this in Access 2010 with the new macros that are available there, but I'm currently at Access 2007.
Is there some way I can accomplish this at the table definition level in Access 2007?
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

I suggest not using the hard code Default value properties at the table or form level. Instead, at the Form level ... use the BeforeInsert event to set 'initial values' (aka default values), like so:

Private Sub Form_BeforeInsert(Cancel As Integer).
  ' set your control values here
   Me.txtSomeTextBox = <SomeValue>
End Sub

This gives you a lot more flexibility. Also, it keeps the New Record position on a Form looking 'clean' - no default values hanging in some controls, making it look like there is already a record.  The BI event triggers when any Bound control is edited.

mx
you can, assign the default value using a form.
the control in the form must be bound to that field,
you can then use, in the current event of the form an expression

me.txtSessionID=GetSessionID()
Avatar of shambalad

ASKER

I'm aware of the VBA options. I was hoping for something that would just automatically take care of itself, especially in instances such as when I'm doing an SQL Append. I'm guessing the short answer is 'No'.
SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well, as you have discovered ... you cannot call a User Defined Function from the Default Value property. However, you *can* ... call that UDF from a Query ... specifically an Append Query as you noted.

mx
"I'd like to assign a default value of 'GetSessionID()' to the field SessionID,"

You *can* call that Function from a query. So, if that is what you are wanting, you are good to go.  For example:

INSERT INTO Table2 ( FIELD1, FIELD2, FIELD3, FIELD4 )
SELECT Table1.FIELD1, Table1.FIELD2, Table1.FIELD3, GetSessionID() AS MyFld
FROM Table1;

mx


ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks guys. I've used UDFs in queries before; can be quite handy. I appreciate your taking the time to look at this question.
Todd
I always appreciate your help. Thanks again.