Writing a routine to create sequential records

后端 未结 3 855
南旧
南旧 2021-01-16 21:11

I would like to write a routine which will allow me to take dated events (records) in a table which span accross a set time frame and in the cases where no event took place

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-16 21:22

    Here you go.

    Sub FillBlanks()
        Dim rsEvents As Recordset
        Dim EventDate As Date
        Dim Fld1 As String
        Dim Fld2 As String
        Dim Fld3 As String
        Dim SQL As String
    
        Set rsEvents = CurrentDb.OpenRecordset("SELECT * FROM tblevents ORDER BY EventDate")
        'Save the current date & info
        EventDate = rsEvents("EventDate")
        Fld1 = rsEvents("Field1")
        Fld2 = rsEvents("Field2")
        Fld3 = rsEvents("Field3")
        rsEvents.MoveNext
        On Error Resume Next
        Do
            ' Loop through each blank date
            Do While EventDate < rsEvents("EventDate") - 1 'for all dates up to, but not including the next date
                EventDate = EventDate + 1 'advance date by 1 day
                rsEvents.AddNew
                rsEvents("EventDate") = EventDate
                rsEvents("Field1") = Fld1
                rsEvents("Field2") = Fld2
                rsEvents("Field3") = Fld3
                rsEvents.Update
            Loop
            ' get new current date & info
            EventDate = rsEvents("EventDate")
            Fld1 = rsEvents("Field1")
            Fld2 = rsEvents("Field2")
            Fld3 = rsEvents("Field3")
            rsEvents.MoveNext
            ' new records are placed on the end of the recordset,
            ' so if we hit on older date, we know it's a recent insert and quit
        Loop Until rsEvents.EOF Or EventDate > rsEvents("EventDate")
    End Sub
    

提交回复
热议问题