可以综合一个sql访问,另外在循环中多次update记录集影响速度,通过调试,在ide环境下,你的代码需要58秒,我修改后的代码需要16秒,我的代码如下:
Private Sub Command1_Click()
Dim d1 As Date, d2 As Date, k As Long, i As Integer, a As String
Call OpenConn
d1 = Time
If rs1.State = 1 Then rs1.Close
sql = "select 工号,日期,Count(时间) AS sjj from Tb_kq_kqjl_temp group by 工号,日期 order by 工号,日期"
rs1.Open sql, cn, 3, 3
While Not rs1.EOF
If Weekday(rs1!日期) = 8 Then wkd = "星期日" Else wkd = Null
'''标注星期日出勤
If rs2.State = 1 Then rs2.Close
sql = "select * from Tb_kq_kqjl_temp where 工号='" & rs1!工号 & "' and 日期='" & rs1!日期 & "' order by 时间1"
rs2.Open sql, cn, 3, 3
i = 1
zt = ""
While Not rs2.EOF
rs2!b = "时间" & i
rs2!休息日 = wkd
If rs1!sjj = 2 Then
'属于正常出勤
If rs2!ID = 459916 Or rs2!ID = 459917 Then
a = a
End If
If i = 1 Then
yc = "正常上班"
Else
yc = "正常下班"
End If
If Format(rs2!时间, "hh:mm:ss") > Format("8:15:00", "hh:mm:ss") And rs2!时间 < "08:45:00" And i = 1 Then yc = "迟到"
If Format(rs2!时间, "hh:mm:ss") < Format("16:45:00", "hh:mm:ss") And rs2!时间 > "16:15:00" And i = 2 Then yc = "早退"
If Format(rs2!时间, "hh:mm:ss") > Format("8:45:00", "hh:mm:ss") And i = 1 Then yc = "上班异常"
If Format(rs2!时间, "hh:mm:ss") < Format("16:15:00", "hh:mm:ss") And i = 2 Then yc = "下班异常"
If yc = "" Then
zt = zt
Else
zt = zt & "/" & yc
End If
If i = 1 Then
kssj = rs2!时间
Else
If Len(zt) > 0 Then zt = Right(zt, Len(zt) - 1)
jssj = rs2!时间
cqsj = Round(Val(DateDiff("n", kssj, jssj)) / 60, 2) - 0.5
''出勤时间
rs2!状态 = zt
rs2!工作时间 = cqsj
rs2.MovePrevious
rs2!状态 = zt
rs2!工作时间 = cqsj
rs2.MoveNext
End If
Else
rs2!状态 = "异常"
rs2!工作时间 = 0
End If
rs2.MoveNext
i = i + 1
Wend
rs2.UpdateBatch
rs1.MoveNext
Wend
Call CloseConn
d2 = Time
k = DateDiff("s", d1, d2)
'计算耗时
MsgBox k
'显示耗时,本代码需要16秒,你的需要58秒,提高3.6倍,也就这样了,还有一种方式,只需打开一次记录集,不知道会不会加快速度
End Sub
[
本帖最后由 lowxiong 于 2013-10-15 16:39 编辑 ]