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

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

提交回复
热议问题