【问题标题】:VBA error matching date from table and date from query when date has leading zero eg 01/04 or 02/04当日期前导零时,VBA 错误匹配表中的日期和查询中的日期,例如 01/04 或 02/04
【发布时间】:2023-04-03 05:55:01
【问题描述】:

我在 MS Access 中有一个名为 qryRMCountStudentsBySessionWP 的查询。结果如下所示。

我有一些 VBA 代码如下所示。基本上,如果 Timetable_Date、Timetable_Session 和 Location 与名为 Invigilation 的表中记录的 Exam_Date、Exam_Session 和 Exam_Location 匹配,则对该记录的开始时间进行更改。

这在 2020 年 3 月 30 日和 2020 年 3 月 31 日非常有效,但不适用于 4 月的任何日期。我认为这是因为前导零,但我只是不知道如何解决它。有什么想法吗?

Private Sub wordProcessing()

    Dim dbs                As Database
    Dim name               As String
    Dim SQL                As String
    Dim rstAllSessions     As Recordset

    Set dbs = CurrentDb

         SQL = "SELECT Timetable_Date, Timetable_Session, Location, CountOfStudent_Ref FROM qryRMCountStudentsBySessionWP;"


    Set rstAllSessions = dbs.OpenRecordset(SQL)

    rstAllSessions.MoveFirst

    Do Until rstAllSessions.EOF  ' for each sesson update invigilation table


         If rstAllSessions.Fields(3) >= 10 Then

            If rstAllSessions.Fields(1) = "A" Then
                dbs.Execute "UPDATE Invigilation SET Start_Time = TimeValue('08:30:00'), Notes = 'WP Exam with 10 or more students' WHERE Invigilation.Exam_Date = #" & rstAllSessions!Timetable_Date & "# AND Invigilation.Exam_Location = " & "'" & rstAllSessions!Location & "'" & "AND  Invigilation.Exam_Session = " & "'" & rstAllSessions!Timetable_Session & "'" & ";", dbFailOnError
            Else
                dbs.Execute "UPDATE Invigilation SET Start_Time = TimeValue('13:00:00'), Notes = 'WP Exam with 10 or more students' WHERE Invigilation.Exam_Date = #" & rstAllSessions!Timetable_Date & "# AND Invigilation.Exam_Location = " & "'" & rstAllSessions!Location & "'" & "AND  Invigilation.Exam_Session = " & "'" & rstAllSessions!Timetable_Session & "'" & ";", dbFailOnError
            End If

         ElseIf rstAllSessions.Fields(3) >= 5 Then

            If rstAllSessions.Fields(1) = "A" Then
                dbs.Execute "UPDATE Invigilation SET Start_Time = TimeValue('08:35:00'), Notes = 'WP Exam with 5 or more students' WHERE Invigilation.Exam_Date = #" & rstAllSessions!Timetable_Date & "# AND Invigilation.Exam_Location = " & "'" & rstAllSessions!Location & "'" & "AND  Invigilation.Exam_Session = " & "'" & rstAllSessions!Timetable_Session & "'" & ";", dbFailOnError
            Else
                dbs.Execute "UPDATE Invigilation SET Start_Time = TimeValue('13:05:00'), Notes = 'WP Exam with 5 or more students' WHERE Invigilation.Exam_Date = #" & rstAllSessions!Timetable_Date & "# AND Invigilation.Exam_Location = " & "'" & rstAllSessions!Location & "'" & "AND  Invigilation.Exam_Session = " & "'" & rstAllSessions!Timetable_Session & "'" & ";", dbFailOnError
            End If

        ElseIf rstAllSessions.Fields(3) > 1 Then

            If rstAllSessions.Fields(1) = "A" Then
                dbs.Execute "UPDATE Invigilation SET Start_Time = TimeValue('08:40:00'), Notes = 'WP Exam with >1 and <5 students' WHERE Invigilation.Exam_Date = #" & rstAllSessions!Timetable_Date & "# AND Invigilation.Exam_Location = " & "'" & rstAllSessions!Location & "'" & "AND  Invigilation.Exam_Session = " & "'" & rstAllSessions!Timetable_Session & "'" & ";", dbFailOnError
            Else
                dbs.Execute "UPDATE Invigilation SET Start_Time = TimeValue('13:10:00'), Notes = 'WP Exam with >1 and <5 students' WHERE Invigilation.Exam_Date = #" & rstAllSessions!Timetable_Date & "# AND Invigilation.Exam_Location = " & "'" & rstAllSessions!Location & "'" & "AND  Invigilation.Exam_Session = " & "'" & rstAllSessions!Timetable_Session & "'" & ";", dbFailOnError
            End If

        Else

           If rstAllSessions.Fields(1) = "A" Then
                dbs.Execute "UPDATE Invigilation SET Start_Time = TimeValue('08:45:00'), Notes = 'WP Exam with 1 student' WHERE Invigilation.Exam_Date = #" & rstAllSessions!Timetable_Date & "# AND Invigilation.Exam_Location = " & "'" & rstAllSessions!Location & "'" & "AND  Invigilation.Exam_Session = " & "'" & rstAllSessions!Timetable_Session & "'" & ";", dbFailOnError
            Else
                dbs.Execute "UPDATE Invigilation SET Start_Time = TimeValue('13:15:00'), Notes = 'WP Exam with 1 student' WHERE Invigilation.Exam_Date = #" & rstAllSessions!Timetable_Date & "# AND Invigilation.Exam_Location = " & "'" & rstAllSessions!Location & "'" & "AND  Invigilation.Exam_Session = " & "'" & rstAllSessions!Timetable_Session & "'" & ";", dbFailOnError
            End If

        End If

       '  doesn't do dates with leading 0s?????

       rstAllSessions.MoveNext

    Loop

    rstAllSessions.Close
    dbs.Close


End Sub

【问题讨论】:

    标签: sql vba date ms-access


    【解决方案1】:

    日期值是数字,因此没有前导零。但是您必须在连接时为日期值传递格式正确的字符串表达式

    "UPDATE Invigilation SET Start_Time = TimeSerial(13,15,0), Notes = 'WP Exam with 1 student' WHERE Invigilation.Exam_Date = #" & Format(rstAllSessions!Timetable_Date, "yyyy\/mm\/dd") & "# AND Invigilation.Exam_Location = '" & rstAllSessions!Location & "' AND  Invigilation.Exam_Session = '" & rstAllSessions!Timetable_Session & "';"
    

    【讨论】:

    • 谢谢,谢谢!这让我发疯了好多年!
    猜你喜欢
    • 2015-10-03
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    • 2017-07-25
    • 2019-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多