Results 1 to 9 of 9
  1. #1
    mdavid is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2017
    Posts
    160

    set combo box date default value

    Hi, I have the following sub:
    Code:
    Private Sub Form_Load()
     Dim Days As Integer
     
     For Days = 0 To 40
      Me.DaysLoanedCmb.AddItem Format(DateAdd("d", Days, Now), "dd/mm/yyyy")
     Next Days
     Me.DaysLoanedCmb.DefaultValue = Format(DateAdd("d", 14, Now), "dd/mm/yyyy")
    End Sub
    My combo box gets populated ok with dates from Now to Now + 40 days.
    I'd like to add a DefaultValue so that form opens with date Now + 14 days appears in the combo box, but the above code produces a blank combo box!
    What am I doing wrong?

    Thanks
    David

  2. #2
    Minty is offline VIP
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Try the following;

    Code:
     Dim Days As Integer
     
        For Days = 0 To 40
            Me.DaysLoanedCmb.AddItem Format(DateAdd("d", Days, Date), "dd/mm/yyyy")
        Next Days
        
        Me.DaysLoanedCmb = Date + 14
    I suspect your combo is unbound and a default value only applies to a new record on a bound control.
    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
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    opens with date Now + 14 days appears in the combo box, but the above code produces a blank combo box
    that is because now includes a time element (date=15/05/19, now-15/05/19 11:53am) - and your combo uses full dates.

  4. #4
    mdavid is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2017
    Posts
    160
    Thanks for your help, how do I create a bound control when I've entered the values through the Form_Load sub?

    Ajax, can I specify a DefaultValue using the above Sub, if so how?

    Thanks David

  5. #5
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    check post #2

  6. #6
    mdavid is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2017
    Posts
    160
    Thanks Ajax, sorry missed that, thought the code was mine and the text Mintys comment - Thanks Minty

  7. #7
    mdavid is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2017
    Posts
    160
    Thanks Ajax, sorry missed that, thought the code was mine and the text Mintys comment.

    Tried your code Minty - still loading with a blank combo box

  8. #8
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    you could just set the default value in the control properties

    Date()+14

  9. #9
    Minty is offline VIP
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    I tested the function and it worked on my test version...

    Post up the code as you have it.

    And Ajax's method would equally work.
    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 ↓↓

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

Similar Threads

  1. Replies: 1
    Last Post: 02-27-2017, 07:30 PM
  2. Default value on an unbound combo box
    By lsmcal1984 in forum Forms
    Replies: 1
    Last Post: 10-10-2013, 06:40 AM
  3. Replies: 2
    Last Post: 05-05-2013, 09:07 PM
  4. Refresh default value for combo box
    By dmunsch in forum Programming
    Replies: 14
    Last Post: 03-18-2013, 12:35 PM
  5. Default Date in Date Fields
    By rickn in forum Access
    Replies: 5
    Last Post: 04-14-2010, 01:30 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