我正在完成这项任务,将获得当天晚些时候的总数。数据库后期有3列,它是lateAM
,latePM
和totallate
。在lateAM
如果用户在上午7:31参加,他会迟到1分钟。在latePM
,如果他在下午1:05参加,他会迟到5分钟。然后我需要添加lateAM
和latePM
以获得6分钟的totallate
。
这是获取lateAM
Dim timelate As TimeSpan
Dim time As Date = #7:30:00 AM#
Dim zerotime As TimeSpan = TimeOfDay - TimeOfDay
If TimeOfDay > #7:30:00 AM# Then
timelate = TimeOfDay - time
Else
timelate = zerotime 'output 0:00:00
End If
Try
Dim cnn As New OleDbConnection(conString)
query = "Insert into tbldtr(EmpID,empname,timeinAM,lateam) values ('" & txtInputID.Text & "', '" & txtEmpName.Text & "','" & TimeOfDay & "','" & timelate.ToString & "')"
cmd = New OleDbCommand(query, cnn)
cnn.Open()
cmd.ExecuteNonQuery()
MessageBox.Show("You have successfully Time in AM")
cnn.Close()
Catch ex As Exception
MsgBox("ERROR: " & ErrorToString(), MsgBoxStyle.Critical)
End Try
这是获取latePM
并添加lateAM
和latePM
Dim timelate As TimeSpan
Dim time As Date = #1:00:00 PM#
Dim zerotime As TimeSpan = TimeOfDay - TimeOfDay
If TimeOfDay > #1:00:00 PM# Then
timelate = TimeOfDay - time
Else
timelate = zerotime
End If
Try
Dim cnn As New OleDbConnection(conString)
query = "update tbldtr set timeinPM = '" & TimeOfDay & "',latepm = '" & timelate.ToString & "', totallate = lateam + latepm"
cmd = New OleDbCommand(query, cnn)
cnn.Open()
cmd.ExecuteNonQuery()
MessageBox.Show("You have successfully Time in PM")
cnn.Close()
Catch ex As Exception
MsgBox("ERROR: " & ErrorToString(), MsgBoxStyle.Critical)
End Try
我的问题是我无法添加lateAM
和latePM
。
我正在使用vb和msaccess作为数据库。我的lateAM
和latePM
列的数据类型为shorttext
。
答案 0 :(得分:1)
您应始终将日期/时间存储为日期,以便更改字段的数据类型。
然后:
query = "Insert into tbldtr(EmpID,empname,timeinAM,lateam) values ('" & txtInputID.Text & "', '" & txtEmpName.Text & "',#" & TimeOfDay.ToString("yyyy'/'MM'/'dd") & "#,#" & timelate.ToString("yyyy'/'MM'/'dd") & "#)"
修改强>
要仅存储时间,请使用以下格式:
query = "Insert into tbldtr(EmpID,empname,timeinAM,lateam) values ('" & txtInputID.Text & "', '" & txtEmpName.Text & "',#" & TimeOfDay.ToString("HH':'mm':'ss") & "#,#" & timelate.ToString("HH':'mm':'ss") & "#)"