Results 1 to 7 of 7
  1. #1
    Levi92 is offline Novice
    Windows 10 Access 2016
    Join Date
    Sep 2018
    Posts
    11

    Assignin dynamic value to a text field via VBA

    Hello!

    I am currently building a form in Access which includes a text field "Age" which, based on the date of birth, calculates the correct age. Unfortunately the DateDiff function does not work 100% of the time, which is


    why I cannot rely on using the Expression Builder via theControl Source for the "Age" text field (wish Access had Excel's Yearfrac function...)

    Anyway, so I wrote the code that accurately calculates the age:

    Code:
    Private Sub txtDayofBirth_Click()
    
    Dim Birthdate As Date
    Dim Age As Byte
    
    
    Birthdate = [Day of Birth]
    
    
    Age = DateDiff("yyyy", Birthdate, Date)
    
    
    If Month(Birthdate) >= Month(Date) Then
        
        If Month(Birthdate) > Month(Date) Then Age = Age - 1
        If Month(Birthdate) = Month(Date) Then
            
           If Day(Birthdate) > Day(Date) Then Age = Age - 1
        
        End If
    End If
    
    
    Me.txtAge.Value = Age
    
    
    End Sub
    The issue I encounter is that, when I trigger this event, the age of the currently selected person is assigned to all other employees in my list. That is probably due to the code I highlighted red.

    Is there any way I can I can solve this?

    Edit:

    Here is a picture of a sample table illustrating the issue:


    Click image for larger version. 

Name:	birthday.jpg 
Views:	21 
Size:	18.8 KB 
ID:	35611

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Create a public function in a standard module that takes the birth date as an input and returns age. Then have this as the control source of the textbox

    =FunctionName([day of birth])

    if needed:

    http://www.baldyweb.com/Function.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Levi92 is offline Novice
    Windows 10 Access 2016
    Join Date
    Sep 2018
    Posts
    11
    Thanks very much! Works like a charm. This is also the very first time I created a Function. Always wondered what the difference was between a Sub and a Function, I think I am getting an idea now. Thanks!

  4. #4
    Levi92 is offline Novice
    Windows 10 Access 2016
    Join Date
    Sep 2018
    Posts
    11
    Click image for larger version. 

Name:	typeerror.jpg 
Views:	19 
Size:	13.9 KB 
ID:	35614

    Ok, there is one minor problem that I can't seem to solve. I get a "#Type!" error if I leave the "Day of Birth" field empty. This error also shows up in the row below the latest record (see picture).

    Here is my code:

    Code:
    Function ReturnAge(Birthdate As Date) As Byte
    
    Birthdate = [Form_Employees].txtDayofBirth.Value
    
    
    If IsEmpty(Birthdate) Then ReturnAge = "": Exit Function
    
    
    ReturnAge = DateDiff("yyyy", Birthdate, Date)
    
    
    If Month(Birthdate) >= Month(Date) Then
        
        If Month(Birthdate) > Month(Date) Then ReturnAge = ReturnAge - 1
        If Month(Birthdate) = Month(Date) Then
            
           If Day(Birthdate) > Day(Date) Then ReturnAge = ReturnAge - 1
        
        End If
    End If
    
    
    End Function
    What do I have to change?

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Declare the input variable as Variant rather than Date, and test for it being Null instead of empty. More here on sub vs function if you're interested.

    http://www.baldyweb.com/SubFunction.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    Levi92 is offline Novice
    Windows 10 Access 2016
    Join Date
    Sep 2018
    Posts
    11
    Works like a charm! Thank you!

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Dynamic Text Box
    By dhicks in forum Forms
    Replies: 6
    Last Post: 11-30-2017, 09:15 AM
  2. Replies: 4
    Last Post: 10-17-2015, 09:40 PM
  3. dynamic field names (sort of)
    By mountainclimber in forum Access
    Replies: 12
    Last Post: 08-13-2015, 08:05 AM
  4. Dynamic Text Box ControlSource from multiple tables
    By cjtezak in forum Programming
    Replies: 2
    Last Post: 06-20-2012, 10:07 AM
  5. Dynamic field names per record
    By snofrandy in forum Queries
    Replies: 1
    Last Post: 05-30-2012, 02:50 PM

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