Insert each date record for current month in Access table using VBA

后端 未结 4 1024
无人共我
无人共我 2020-12-12 05:19

I am new with VBA programming. I have designed one form in which there is a button. After clicking this button I want to insert record in my table having one date column. Th

4条回答
  •  醉话见心
    2020-12-12 06:22

    Please, see below code and read comments:

    Option Explicit
    
    Sub Command0_Click()
    Dim startDate As Date
    Dim endDate As Date
    Dim curDate As Date
    
    'get first day from current date
    startDate = GetFirstDayInMonth(Date)
    'get last day from startDate
    endDate = GetLastDayInMonth(startDate)
    'loop through the dates
    For curDate = startDate To endDate
        'here call the procedure to insert data
    Next
    
    End Sub
    
    'function to return first date in the month
    Function GetFirstDayInMonth(dCurDate As Date) As Date
    
        GetFirstDayInMonth = DateSerial(Year(dCurDate), Month(dCurDate), 1)
    
    End Function
    
    'return last date in the month
    Function GetLastDayInMonth(dCurDate As Date) As Date
    
        GetLastDayInMonth = DateSerial(Year(dCurDate), Month(dCurDate) + 1, 1 - 1)
    
    End Function
    

提交回复
热议问题