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

后端 未结 4 994
无人共我
无人共我 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:10

    You can create a table containing every possible day number

    [DayOfMonth]
    
    dd
    --
     1
     2
     3
    ...
    30
    31
    

    and then just use a query like this to generate one row for each day in the current month

    SELECT DateSerial(Year(Date()), Month(Date()), dd) AS IDate
    FROM DayOfMonth
    WHERE Month(DateSerial(Year(Date()), Month(Date()), dd)) = Month(Date())
    

    To use it as an INSERT query would be

    INSERT INTO tbl_ShipOrders (IDate)
    SELECT DateSerial(Year(Date()), Month(Date()), dd) AS IDate
    FROM DayOfMonth
    WHERE Month(DateSerial(Year(Date()), Month(Date()), dd)) = Month(Date())
    
    0 讨论(0)
  • 2020-12-12 06:13

    Here's a fancy query that will return a month's dates:

    SELECT DISTINCT 
        10 * Abs([Deca].[id] Mod 10) + Abs([Uno].[id] Mod 10) AS Id, 
        DateAdd("d",[Id], DateSerial(Year(DateOfMonth), Month(DateOfMonth), 1)) AS [Date]
    FROM 
        msysobjects AS Uno, 
        msysobjects AS Deca
    WHERE 
        (10*Abs([Deca].[id] Mod 10) + Abs([Uno].[id] Mod 10)) < Day(DateSerial(Year(DateOfMonth), Month(DateOfMonth)+1, 0))
    

    That said, I would write a loop adding records to a recordset using VBA, not the slow SQL call of yours.

    0 讨论(0)
  • 2020-12-12 06:21

    The following expression will give you an integer, the number of days in the current month:

    Day(DateSerial(Year(Date()), Month(Date())+1, 0))
    

    This is the zeroth day of the following month, which is the last day of the current month. This expression will still work if moving from December to January.

    Store this in a variable, say lastDay then use a loop, For x = 1 To lastDay to perform the inserts.

    Within the loop, this expression

    DateSerial(Year(Date()), Month(Date()), x)
    

    will give you the dates 1/5/2016, 2/5/2016,.., 31/5/2016.

    You should also surround the dates with date delimiters # when inserting. Combine this with ISO formatting of the date yyyy-mm-dd (so that months won't be interpreted as days):

    VALUES (#" & Format(dtValue, "yyyy-mm-dd") & "#)"
    

    where dtValue is the date you've just formed using the previous DateSerial expression.

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题