ผมให้มันกวดสอบ ID กะ Date เมื่อมีอยู่แล้ว จะให้มันฟ้อง message แต่ที่นี้ มันไม่กวดสอบ มันข้ามไปเลย งง มากๆๆ ครับ ช่วยที่
Code (VB.NET)
Public Function isExist(ByVal barcode As String, ByVal d As String) As Boolean
Dim dt As New DataTable
Dim da As New OleDbDataAdapter(" SELECT tbl_DailyRecord.*, tbl_DailyRecord.Dates FROM tbl_DailyRecord WHERE tbl_DailyRecord.Dates > #" & d & " # AND tbl_DailyRecord.Dates < #" & d & " # AND tbl_DailyRecord.LabourID = '" & CInt(barcode) & "' ", Conn)
da.Fill(dt)
If (dt.Rows.Count > 0) Then
Return True
Else
Return False
End If
End Function
Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
If txtLabourID.Text = "" Then
MessageBox.Show("Please Check Barcode And Scan Again", "Scan Barcode", MessageBoxButtons.OK, MessageBoxIcon.Error)
txtBarcode.Clear()
txtBarcode.Focus()
Exit Sub
End If
Dim barcode As String = txtLabourID.Text
If txtBarcode.Text = "" Then
MessageBox.Show("Please Scan Barcode", "Scan Barcode", MessageBoxButtons.OK, MessageBoxIcon.Error)
txtBarcode.Clear()
txtBarcode.Focus()
Else
If isExist(barcode, dtpDateTime.Text) Then
MessageBox.Show("You were already CheckIn to the System", "Already CheckIn", MessageBoxButtons.OK, MessageBoxIcon.Stop)
txtBarcode.Clear()
txtLabourID.Clear()
txtFullnameEng.Clear()
txtFullnameLao.Clear()
txtAge.Clear()
txtTimeIn.Clear()
txtBarcode.Focus()
'picLabour.Image = "No_Photo.bmp"
If Not (picLabour.Image Is Nothing) Then
picLabour.Image.Dispose()
picLabour.Image = Nothing
End If
Else
' MessageBox.Show("Do You Want To Save ?", "Save", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
Try
sql = "insert into tbl_DailyRecord(LabourID,Nameeng,Dates,TimeIn,NSR,Species,Place,Description,Status)"
sql &= "values('" & txtLabourID.Text.Trim & "','" & txtFullnameEng.Text.Trim & "','" & dtpDateTime.Value.Date & "','" & dtpDateTime.Value & "',"
sql &= "" & CInt(cboNSR.SelectedValue) & "," & CInt(cboSpecies.SelectedValue) & ","
sql &= "" & CInt(cboPlace.SelectedValue) & "," & CInt(cboDescription.SelectedValue) & ","
sql &= "'I')"
Com = New OleDbCommand
With Com
.Connection = Conn
.CommandText = sql
.ExecuteNonQuery()
End With
'MessageBox.Show("Successful..!", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtBarcode.Clear()
txtLabourID.Clear()
txtFullnameEng.Clear()
txtFullnameLao.Clear()
txtAge.Clear()
txtTimeIn.Clear()
txtBarcode.Focus()
'picLabour.Image = "No_Photo.bmp"
If Not (picLabour.Image Is Nothing) Then
picLabour.Image.Dispose()
picLabour.Image = Nothing
End If
Catch ex As Exception
MessageBox.Show("Error. Because " & ex.Message, "Result", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End Try
End If
End If
End Sub
Dim da As New OleDbDataAdapter(" SELECT tbl_DailyRecord.*, tbl_DailyRecord.Dates FROM tbl_DailyRecord WHERE tbl_DailyRecord.Dates > #" & Convert.ToDateTime(d) & " # AND tbl_DailyRecord.Dates < #" & Convert.ToDateTime(d) & " # AND tbl_DailyRecord.LabourID = '" & CInt(barcode) & "' ", Conn)
Dim da As New OleDbDataAdapter(" SELECT * FROM tbl_DailyRecord WHERE tbl_DailyRecord.Dates > #" & Convert.ToDateTime(d).ToString("dd/MM/yyyy") & " # AND tbl_DailyRecord.Dates < #" & Convert.ToDateTime(d).ToString("dd/MM/yyyy") & " # AND tbl_DailyRecord.LabourID = '" & CInt(barcode) & "' ", Conn)
Public Function isExist(ByVal barcode As String, ByVal d As Date) As Boolean
barcode = barcode.Trim()
If Not IsNumeric(barcode) Then
MsgBox("BarCode Allow Number Only", MsgBoxStyle.Critical)
Return False
End If
Dim startDate As String = d.ToString("dd/MM/yyyy 00:00:00")
Dim endDate As String = d.ToString("dd/MM/yyyy 23:59:59")
Dim sql As String = String.Empty _
& " SELECT COUNT(*) FROM tbl_DailyRecord WHERE " _
& " ( tbl_DailyRecord.Dates Between #" & startDate & "# AND #" & endDate & "# )" _
& " AND tbl_DailyRecord.LabourID = " & CInt(barcode)
Dim com As New OleDbCommand(sql, Conn)
com.CommandTimeout = 0
Dim rowCount As Integer = 0
Try
If Conn.State <> ConnectionState.Open Then
Conn.Open()
End If
rowCount = CInt(com.ExecuteScalar())
Catch ex As Exception
Dim errorMessage As String = sql & vbCrLf _
& New String("=", 30) & vbCrLf _
& ex.Message & vbCrLf _
& New String("=", 30) & vbCrLf _
& ex.StackTrace
MsgBox(errorMessage, MsgBoxStyle.Critical)
End Try
Conn.Close()
com.Dispose()
Return rowCount > 0
End Function
เวลาเอาไปใช้ Code (VB.NET)
If isExist(barcode, dtpDateTime.Value) Then
'-------------------
End If