DAO.Recordset AddNew

时间:2017-02-02 04:19:50

标签: forms ms-access access-vba

我想知道你是否可以以及如何继续将记录或循环记录添加到表中,直到我达到字段的某个日期1/1/2121?我将我的代码更改为sub,我可以称之为FilltbluBoughtVacation 2013,2021

但我认为这会有效并且我会收到错误' 3134'插入到语句和调试中的语法错误将我带到CurrentDb.Execute strSql。

Public Sub FilltbluBoughtVacation(StartYear As Integer, EndYear As Integer)

Dim BoughtYear As Date
Dim CurrentYear As Integer

For CurrentYear = StartYear To EndYear
    BoughtYear = CDate("01/01/" & CurrentYear)
    InsertBoughtVacation BoughtYear

Next CurrentYear
End Sub

Public Sub InsertBoughtVacation(BoughtVacDate As Date)
Dim strSql As String
strSql = "Insert into [tbluBoughtVacation] ([BoughtVacDate]) values (#" & Format([BoughtVacDate], "mm/dd/yyyy") & "# ,)"
Debug.Print strSql
CurrentDb.Execute strSql

End Sub

1 个答案:

答案 0 :(得分:0)

对于两个函数来说太少了,在循环中调用SQL太慢了。因此:

Public Sub FilltbluBoughtVacation(StartYear As Integer, EndYear As Integer)

    Dim rs AS DAO.Recordset

    Dim Sql As String
    Dim CurrentYear As Integer

    Sql = "Select Top 1 EmployeeID, BoughtYear From tbluBoughtVacation"
    Set rs = CurrentDb.OpenRecordset(Sql)

    For CurrentYear = StartYear To EndYear
        rs.AddNew
            rs("EmployeeID").Value = Me!txtEmployeeID.Value 
            rs("BoughtYear").Value = DateSerial(CurrentYear, 1, 1)
        rs.Update
    Next
    rs.Close

    Set rs = Nothing

End Sub