Page 2 of 2 FirstFirst 12
Results 16 to 28 of 28
  1. #16
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922

    Quote Originally Posted by NOTLguy View Post
    So far, I have had no luck with Paul's solution and perhaps I am using it incorrectly. I was assuming that Datefield is a field of date/time type where the default value is Date().
    This field in Paul's example is just an Integer field (long if you want) in every record with the default value of Year(Date()). It is *not* a DateTime field.

  2. #17
    NOTLguy is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Sep 2010
    Posts
    60
    Quote Originally Posted by RuralGuy View Post
    This field in Paul's example is just an Integer field (long if you want) in every record with the default value of Year(Date()). It is *not* a DateTime field.
    Thanks for getting back to me. I have changed the DateField to a long integer and stored the default value as Year(Date()) which is now '2010' if I look at the datasheet view of the next record to be created.

    I have placed the code in the On Click Event Procedure of my Add New Record Control as follows:

    Private Sub Command44_Click()
    On Error GoTo Err_Command44_Click

    DoCmd.GoToRecord , , acNewRec

    Me.Client__ = Nz(DMax(Me.Client__, Clients, "me.DateField =" & Year(Date)), 0) + 1

    Exit_Command44_Click:
    Exit Sub

    Err_Command44_Click:
    MsgBox Err.Description
    Resume Exit_Command44_Click

    End Sub

    You will notice that the Year(date()) does not look right as Access will not let me add the extra )'s and returns an error "Invalid use of Null" when executed. Any thoughts?

    Thanks,
    Bill

  3. #18
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    You do *not* want the me. in that criteria argument!
    Code:
    Me.Client__ = Nz(DMax(Me.Client__, Clients, "me.DateField =" & Year(Date)), 0) + 1
    You do ynderstand that the Me.Client__ control will end up with just a number in it, right? When you display it or search for it you will need to concatenate the two fields into the string you want.
    [DateField] & "-" & Format(Client__, "0000") or whatever you want.

  4. #19
    NOTLguy is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Sep 2010
    Posts
    60
    Quote Originally Posted by RuralGuy View Post
    You do *not* want the me. in that criteria argument!
    Code:
    Me.Client__ = Nz(DMax(Me.Client__, Clients, "me.DateField =" & Year(Date)), 0) + 1
    You do ynderstand that the Me.Client__ control will end up with just a number in it, right? When you display it or search for it you will need to concatenate the two fields into the string you want.
    [DateField] & "-" & Format(Client__, "0000") or whatever you want.
    Thanks for your reply. I tried the code with the "me." removed from the DateField and it still comes up with an error "Invalid use of null". As well, I am still unable to add the extra ) required. My cope comes out looking like this:

    Before leaving editing the code:

    Me.Client__ = Nz(DMax(Me.Client__, Clients, "DateField =" & Year(Date())), 0) + 1

    After leaving the line, Access removes the () after Date

    Me.Client__ = Nz(DMax(Me.Client__, Clients, "DateField =" & Year(Date)), 0) + 1

    This results in the "Invalid use of null" error.

    ???

    Regards,
    Bill

  5. #20
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Any chance you have named some procedure DATE() in your ap somewhere? Do a find on "Date(' from any VBA module and select "Current Project".

  6. #21
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    It may be time to post your zipped up db so we can play with it.

  7. #22
    NOTLguy is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Sep 2010
    Posts
    60
    Quote Originally Posted by RuralGuy View Post
    It may be time to post your zipped up db so we can play with it.
    Would something like this work in the On-Click event of the Add New Record button?

    Private Sub Command44_Click()
    On Error GoTo Err_Command44_Click

    Dim ClienNum As Integer
    Dim ClienYear As Integer
    DoCmd.GoToRecord , acClients, acLast
    ClienNum = Me.DateField
    ClienYear = Left(Me.Client__, 4)
    DoCmd.GoToRecord , , acNewRec
    If ClienYear = Year(Date) Then
    Me.Client__ = ClienYear & "-" & ClienNum + 1
    Else
    Me.DateField = 1
    Me.Client__ = Year(Date) & "-" & Me.DateField
    End If



    Exit_Command44_Click:
    Exit Sub
    Err_Command44_Click:
    MsgBox Err.Description
    Resume Exit_Command44_Click

    End Sub

  8. #23
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Why not put something in the Current Event of the Form that checks for Me.NewRecord?

  9. #24
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Pardon me for jumping in. A couple of thoughts on what appears to be your most recent effort:

    Me.Client__ = Nz(DMax(Me.Client__, Clients, "DateField =" & Year(Date)), 0) + 1

    Does this work?

    Me.[Client__] = Nz(DMax("[Client__]", "Clients", "DateField =" & Year(Date)), 0) + 1

    I'm not a fan of the trailing underscores; you may need to bracket that field name, but the quotes are necessary in the DMax, and as RG mentioned you don't want Me in the field argument. As RG also mentioned, a sample db would be a great help.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #25
    NOTLguy is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Sep 2010
    Posts
    60
    Quote Originally Posted by pbaldy View Post
    Pardon me for jumping in. A couple of thoughts on what appears to be your most recent effort:

    Me.Client__ = Nz(DMax(Me.Client__, Clients, "DateField =" & Year(Date)), 0) + 1

    Does this work?

    Me.[Client__] = Nz(DMax("[Client__]", "Clients", "DateField =" & Year(Date)), 0) + 1

    I'm not a fan of the trailing underscores; you may need to bracket that field name, but the quotes are necessary in the DMax, and as RG mentioned you don't want Me in the field argument. As RG also mentioned, a sample db would be a great help.
    Thanks for your reply.

    I tried this line of code with the field name bracketed and it produced:

    "1 - " in the [Client__] field.

    Prior to going to the new record, Datefield value was 37 and the largest value in the [Client__] field with the current year was 2010-37.

    Regards,
    Bill

  11. #26
    NOTLguy is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Sep 2010
    Posts
    60
    In using this code:

    Me.[Client__] = Nz(DMax("[Client__]", "Clients", "DateField =" & Year(Date)), 0) + 1

    I am confused as to what "DateField" actually is? Is this another field in the Current Table (Clients) that I have to create and store the value of the current year in?


    Regards,
    Bill

  12. #27
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    DateField should be the name of your field. I take it you can't post the db?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  13. #28
    NOTLguy is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Sep 2010
    Posts
    60
    Quote Originally Posted by pbaldy View Post
    DateField should be the name of your field. I take it you can't post the db?
    This Db is for a Not for Profit Palliative Care Group and the Clients records are confidential in nature. I cannot post the Db. I am a volunteer in the group with some limited Access background.

    I have solved the problem, although using a different approach. The field that stores the data for Client Number is a text field and the data looks like this:

    2010-36
    2010-37
    etc and increments by 1 as a new client is entered until the end of the year comes and we start over as:

    2011-01
    etc

    The following is the code I used in the On-Click eventy of the Add New Record Control:

    Private Sub Command44_Click()
    On Error GoTo Err_Command44_Click

    Dim ClienNum As Integer
    Dim ClienYear As Integer
    DoCmd.GoToRecord , , acLast
    ClienNum = Me.DateField ‘Sets ClienNum = to value of Datefield in the last record
    ClienYear = Left(Me.Client__, 4) ‘ Sets ClienYear – to first 4 characters in the Client__ Field
    DoCmd.GoToRecord , , acNewRec
    If ClienYear = Year(Date) Then ‘test for the year being the same as the last record
    Me.DateField = (ClienNum + 1) ‘adds 1 to value of DateField
    Me.Client__ = ClienYear & "-" & (ClienNum + 1)
    Else ‘ year has changed on Jan 1st and a new series of numbers must begin
    Me.DateField = 1
    Me.Client__ = Year(Date) & "-" & Me.DateField
    End If

    Exit_Command44_Click:
    Exit Sub

    Err_Command44_Click:
    MsgBox Err.Description
    Resume Exit_Command44_Click

    End Sub


    Many thanks to all of you who have helped me with this,
    Regards,
    Bill

Page 2 of 2 FirstFirst 12
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Auto fill a field from multiple lookup
    By shahemaan in forum Forms
    Replies: 4
    Last Post: 06-13-2014, 04:00 PM
  2. Auto-Fill
    By sophiecormier in forum Programming
    Replies: 3
    Last Post: 10-02-2010, 08:29 AM
  3. Auto-fill in datasheet
    By Terence in forum Database Design
    Replies: 2
    Last Post: 03-18-2010, 03:42 PM
  4. Auto Fill-In Capability?
    By bbarrene in forum Access
    Replies: 3
    Last Post: 01-15-2010, 08:35 AM
  5. Auto-fill field not saving to table
    By aaid in forum Forms
    Replies: 1
    Last Post: 12-18-2009, 05:34 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