Results 1 to 4 of 4
  1. #1
    SierraJuliet's Avatar
    SierraJuliet is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Dec 2017
    Location
    Earth
    Posts
    211

    Increment Next Item in List


    The following code is for incrementing calendar months with the click of a button. What would be the best way to increment through the months and increment into the next year? What I have increments one month left and right from the current month.

    Code:
    Private Sub btnNextMonth_Click()
    On Error GoTo btnNextMonth_Click_Err
        Me![txtSelectMonth] = Month(Date) + 1
        'Me![txtSelectYear] = Year(Date)
    btnNextMonth_Click_Exit:
        Exit Sub
    btnNextMonth_Click_Err:
        Beep
        MsgBox Err.Description, vbOKOnly, ""
        Resume btnNextMonth_Click_Exit
    End Sub
    
    
    Private Sub btnPreviousMonth_Click()
    On Error GoTo btnPreviousMonth_Click_Err
        Me![txtSelectMonth] = Month(Date) - 1
        'Me![txtSelectYear] = Year(Date)
    btnPreviousMonth_Click_Exit:
        Exit Sub
    btnPreviousMonth_Click_Err:
        Beep
        MsgBox Err.Description, vbOKOnly, ""
        Resume btnPreviousMonth_Click_Exit
    End Sub

  2. #2
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I have two combo boxes - one for the month and one for the year. Maybe you will be able to adapt my code for your use.

    So two combo boxes and two buttons - an increment button and a decrement button.

    The code (I wrote this a long time ago...)

    Code:
    Private Sub MonthDec_Click()
       Dim intMonth As Integer, strDate As String
       strDate = Me.cboMonth & " 1, 1999"
       intMonth = Month(strDate)
       intMonth = intMonth - 1
    
       If intMonth = 0 Then
          intMonth = 12
          Me.cboYear = Me.cboYear - 1
       End If
    
       'the year doesn't matter because only the month is displayed
       Me!cboMonth = Format(DateSerial(1995, intMonth, 1), "mmmm")
       SetDates
    End Sub
    
    Private Sub MonthInc_Click()
       Dim intMonth As Integer, strDate As String
       strDate = Me.cboMonth & " 1, 1999"
       intMonth = Month(strDate)
       intMonth = intMonth + 1
    
       If intMonth = 13 Then
          intMonth = 1
          Me.cboYear = Me.cboYear + 1
       End If
    
       'the year doesn't matter because only the month is displayed
       Me!cboMonth = Format(DateSerial(1995, intMonth, 1), "mmmm")
    
    End Sub
    
    '-------------------------------------------------------------------
    
    Private Sub YearInc_Click()
       Me.cboYear = Me.cboYear + 1
       Me.Requery
       Me.Refresh
    
    End Sub
    
    Private Sub YearDec_Click()
       Me.cboYear = Me.cboYear - 1
       Me.Requery
       Me.Refresh
    
    End Sub

    Sorry, I don't know enough about your design to modify the code for you use.......

  3. #3
    SierraJuliet's Avatar
    SierraJuliet is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Dec 2017
    Location
    Earth
    Posts
    211
    This code worked perfectly with a previous button and a next button on the form. The month combo box changes months and the year textbox automatically adjusts when toggling between December and January.

    Code:
    Private Sub btnPreviousMonth_Click()
    On Error GoTo btnPreviousMonth_Click_Err
        Dim January, February, March, April, May, June, July, August, September, October, November, December
        January = 1
        February = 2
        March = 3
        April = 4
        May = 5
        June = 6
        July = 7
        August = 8
        September = 9
        October = 10
        November = 11
        December = 12
        If Me![txtSelectMonth] = 2 - 1 Then
            Me![txtSelectMonth] = 12
            Me![txtSelectYear] = Me![txtSelectYear] - 1
        Else
            Me![txtSelectMonth] = Me![txtSelectMonth] - 1
        End If
    btnPreviousMonth_Click_Exit:
        Exit Sub
    btnPreviousMonth_Click_Err:
        Beep
        MsgBox Err.Description, vbOKOnly, ""
        Resume btnPreviousMonth_Click_Exit
    End Sub
    
    
    Private Sub btnNextMonth_Click()
    On Error GoTo btnNextMonth_Click_Err
        Dim January, February, March, April, May, June, July, August, September, October, November, December
        January = 1
        February = 2
        March = 3
        April = 4
        May = 5
        June = 6
        July = 7
        August = 8
        September = 9
        October = 10
        November = 11
        December = 12
        If Me![txtSelectMonth] = 11 + 1 Then
            Me![txtSelectMonth] = 1
            Me![txtSelectYear] = Me![txtSelectYear] + 1
        Else
            Me![txtSelectMonth] = Me![txtSelectMonth] + 1
        End If
    btnNextMonth_Click_Exit:
        Exit Sub
    btnNextMonth_Click_Err:
        Beep
        MsgBox Err.Description, vbOKOnly, ""
        Resume btnNextMonth_Click_Exit
    End Sub

  4. #4
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Happy you solved your problem...

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

Similar Threads

  1. Insert Item in a List if there is no
    By SamyEcheverria in forum Forms
    Replies: 2
    Last Post: 01-12-2019, 06:40 PM
  2. Add item to value list in list box - error
    By slochhaas in forum Forms
    Replies: 7
    Last Post: 01-31-2017, 03:22 PM
  3. Replies: 2
    Last Post: 04-05-2012, 08:39 PM
  4. Dropdown List: Use Each Item Once?
    By Heavy Doody in forum Access
    Replies: 1
    Last Post: 05-02-2011, 07:44 AM
  5. Replies: 5
    Last Post: 11-12-2010, 12:10 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