Connecting Tech Pros Worldwide Forums | Help | Site Map

Setting a variable default value?

Newbie
 
Join Date: Nov 2008
Posts: 21
#1: Nov 17 '08
Would like any suggestions please on how to achieve this - I'm using MS Access 2003.

At present for a simple data entry situation using a form, the user entering the data enters their intials into a field on every record to enable us to identify who made that entry.

Is there a way the user can enter their name just once at the beginning of the session and for it to then be automatically entered in each record they add until the database is next closed?

I suppose it's like setting a variable default field somehow....?

missinglinq's Avatar
Moderator
 
Join Date: Nov 2006
Location: Richmond, Virginia USA
Posts: 3,000
#2: Nov 17 '08

re: Setting a variable default value?


In your form, you can use the AfterUpdate event of the control holding your data to set the DefaultValue for the field. From that time forward, until you either manually change the data or close your form, the data will be entered automatically in each new record. The syntax varies slightly, depending on the datatype of the data. For your use at this time you'd want to use the Text version, but I'll give you all versions for future reference:

For Text fields
Expand|Select|Wrap|Line Numbers
  1. Private Sub YourTextControlName_AfterUpdate()
  2. If Not IsNull(Me.YourTextControlName.Value) Then
  3.   YourTextControlName.DefaultValue = """" & Me.YourTextControlName.Value & """"
  4. End If
  5. End Sub
For Date fields
Expand|Select|Wrap|Line Numbers
  1. Private Sub YourDateControlName_AfterUpdate()
  2. If Not IsNull(Me.YourDateControlName.Value) Then
  3.   YourDateControlName.DefaultValue ="#" &  Me.YourDateControlName & "#"
  4. End If
  5. End Sub
For Numeric fields
Expand|Select|Wrap|Line Numbers
  1. Private Sub YourNumericControlName_AfterUpdate()
  2. If Not IsNull(Me.YourNumericControlName.Value) Then
  3.   YourNumericControlName.DefaultValue = Me.YourNumericControlName.Value
  4. End If
  5. End Sub
Welcome to Bytes!

Linq ;0)>
Newbie
 
Join Date: Nov 2008
Posts: 21
#3: Nov 19 '08

re: Setting a variable default value?


Many thanks for your reply.
Unfortunately I'm either very thick or very new (or both!)
Could you clarify how you've used the names in your script?
ie: my table name is Asurvivortable
Form name is Asurvivorformshort
The field concerned is locationnow
If you could put it into words of one syllable for me that would be a great help....
Thanks.
Kev
missinglinq's Avatar
Moderator
 
Join Date: Nov 2006
Location: Richmond, Virginia USA
Posts: 3,000
#4: Nov 19 '08

re: Setting a variable default value?


Assuming that

LocationNow

is the field you want to "bring forward" and is a text field

Expand|Select|Wrap|Line Numbers
  1. Private Sub LocationNow_AfterUpdate()
  2.   If Not IsNull(Me.LocationNow.Value) Then
  3.     LocationNow.DefaultValue = """" & Me.LocationNow.Value & """"
  4.   End If
  5. End Sub
  6.  
Linq ;0)>
Newbie
 
Join Date: Nov 2008
Posts: 21
#5: Nov 20 '08

re: Setting a variable default value?


Many thanks.
It works brilliantly!
Kev
Reply


Similar Microsoft Access / VBA bytes