Setting a variable default value?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KEV999
    New Member
    • Nov 2008
    • 21

    Setting a variable default value?

    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
    Recognized Expert Specialist
    • Nov 2006
    • 3533

    #2
    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
    Code:
    Private Sub YourTextControlName_AfterUpdate()
    If Not IsNull(Me.YourTextControlName.Value) Then
      YourTextControlName.DefaultValue = """" & Me.YourTextControlName.Value & """"
    End If
    End Sub
    For Date fields
    Code:
    Private Sub YourDateControlName_AfterUpdate()
    If Not IsNull(Me.YourDateControlName.Value) Then
      YourDateControlName.DefaultValue ="#" &  Me.YourDateControlName & "#"
    End If
    End Sub
    For Numeric fields
    Code:
    Private Sub YourNumericControlName_AfterUpdate()
    If Not IsNull(Me.YourNumericControlName.Value) Then
      YourNumericControlName.DefaultValue = Me.YourNumericControlName.Value
    End If
    End Sub
    Welcome to Bytes!

    Linq ;0)>

    Comment

    • KEV999
      New Member
      • Nov 2008
      • 21

      #3
      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 Asurvivorformsh ort
      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

      Comment

      • missinglinq
        Recognized Expert Specialist
        • Nov 2006
        • 3533

        #4
        Assuming that

        LocationNow

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

        Code:
        Private Sub LocationNow_AfterUpdate()
          If Not IsNull(Me.LocationNow.Value) Then
            LocationNow.DefaultValue = """" & Me.LocationNow.Value & """"
          End If
        End Sub
        Linq ;0)>

        Comment

        • KEV999
          New Member
          • Nov 2008
          • 21

          #5
          Many thanks.
          It works brilliantly!
          Kev

          Comment

          Working...