Results 1 to 6 of 6
  1. #1
    Chrtalgo is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2018
    Posts
    8

    Calculated field prevents new record

    Hi! I have this code in Private Sub Form_current():




    Code:
        If IsNull(Me.BirthDate) Then        Dim noYears As String
            noYears = "-"
            Me.Age.Value = noYears
        Else
            Dim intYears As Integer
            intYears = Year(Now) - Year(Me.BirthDate)
        
            If DateSerial(Year(Now), Month(Me.BirthDate), Day(Me.BirthDate)) > Now Then
                intYears = intYears - 1
            End If
            Me.Age.Value = intYears
            
        End If
    This is to calculate the current age of the records. However, this seems to restrict me from using DoCmd.GoToRecord , "", acNewRec

    When I click a button to create a new record I get Run-time error '2424': The expression you entered has a field, control, or property that Membersarchive can't find.

    When debugging it highlights "intYears = Year(Now) - Year(Me.BirthDate)" in the code.


    I don't get this as the first IF code should bypass the calculation and set the age to "-"? This works if I delete the birthdate from existing record, but it won't allow me to create a rew record.

    me.age is not pulled from a query or table, it is calculated within the form itself.


    Any ideas?

  2. #2
    Minty is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Try renaming your text box to txtBirthDate to make sure you are referencing the control not the field.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    Micron is online now Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,788
    Looks like you are storing a calculated age, which is something you should not do anyway.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    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
    First of, you really need to place all DIM statements at the top of your event.

    That said, what Datatype is the Age control tied to?

    First you have

    Me.Age.Value = noYears

    where noYears is defined as a String. Then you have

    Me.Age.Value = intYears

    where intYears is defined as an Integer! The obvious assumption would be that Age is defined as an Integer, in which case

    Code:
    noYears = "-"
    Me.Age.Value = noYears

    is going to pop an error, possibly the one you're getting! The Access Gnomes aren't always accurate/clear with their error messages.

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

    All posts/responses based on Access 2003/2007

  5. #5
    SierraJuliet's Avatar
    SierraJuliet is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Dec 2017
    Location
    Earth
    Posts
    211
    Just some ideas.

    Dim noDOB As Integer

    Dim tAge As Integer

    Dim tYears As Integer

    'show a zero for null birth date value
    noDOB = 0

    'age of person in years old
    tAge = (Year(Now) - Year(Me![txtBirthDate]))

    'excludes current year from years old; however, this will result in an inaccurate years of age.
    tYears = (tAge - 1)

    If IsNull(Me![txtBirthDate]) Then

    Me![txtAge] = noDOB

    Else
    'Not sure why this next line needs to be here. Simply append the tYears to [txtAge] if [txtBirthDate] is not null
    If DateSerial(Year([txtBirthDate]), Month([txtBirthDate]), Day([txtBirthDate])) < Now Then

    Me![txtAge] = tYears

    End If
    End If

  6. #6
    SierraJuliet's Avatar
    SierraJuliet is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Dec 2017
    Location
    Earth
    Posts
    211
    Now this is what I would do if you are doing what I think you are. You can change As Integer to As String for both noDOB and tAge if you really want the "-" but ensure Me![txtAge] field is set to short text instead of number. They have to be the same data type or you will receive a mismatch error.

    Dim noDOB As Integer

    Dim tAge As Integer

    noDOB = 0

    tAge = (Year(Now) - Year(Me![txtBirthDate]))

    If IsNull(Me![txtBirthDate]) Then

    Me![txtAge] = noDOB

    Else

    Me![txtAge] = tAge

    End If
    End If

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

Similar Threads

  1. Replies: 1
    Last Post: 05-22-2017, 05:59 AM
  2. Replies: 1
    Last Post: 11-15-2014, 12:43 PM
  3. Replies: 3
    Last Post: 07-20-2012, 11:41 AM
  4. Query Prevents formatting of field
    By Chelcone in forum Queries
    Replies: 3
    Last Post: 04-20-2012, 11:43 AM
  5. Replies: 3
    Last Post: 03-25-2012, 12:40 AM

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