Results 1 to 4 of 4
  1. #1
    astath is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Apr 2020
    Posts
    30

    Set a value with VBA

    Dear All,




    I am currently using VBA in order to set a value of a field of a subform for each new entry. What I have found working is to use Units = Units.ItemData(0) under the Form_Current ()

    The problem with this scenario is that if I select a field of a previous line, units get reset. Is there a way to perform what needed only once for each new row?

    Thanks.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    no vba needed,
    set the text box property: DEFAULT VALUE=6

  3. #3
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    You're going to have Unit reset every time you move to a different Record, using the Form_Current event.

    To prevent this and only have it apply for New Records, which I'm assuming is your goal (your explanation is somewhat murky, at least to me...maybe just a lack of caffeine on my part) you need to check to see if it's a New Record before assigning the value...i.e.

    Code:
    If Me.NewRecord Then
       Units = Units.ItemData(0)
    End If

    ranman256: You can't do that...the value isn't fixed...it depends on the data in the Combobox/Listbox maned Units.ItemData(0).

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  4. #4
    astath is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Apr 2020
    Posts
    30
    Thank you both for your help and prompt responses. Missinglinq despite the murky explanation from my side, not only you understood what I wanted but you solution works perfectly! Many thanks!!

Please reply to this thread with any new information or opinions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums