-
Notifications
You must be signed in to change notification settings - Fork 1
/
frmAttendance.vb
executable file
·406 lines (323 loc) · 16.3 KB
/
frmAttendance.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
Imports System.Data.oledb
Public Class frmAttendance
Dim rdr As oledbDataReader = Nothing
Dim dtable As DataTable
Dim con As oledbConnection = Nothing
Dim adp As oledbDataAdapter
Dim ds As DataSet
Dim cmd As oledbCommand = Nothing
Dim dt As New DataTable
Dim cs As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\PayrollManagerDB.accdb;Persist Security Info=False;"
Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub Attendance_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
frmMainMenu.Show()
End Sub
Sub fillcombo()
Try
Dim CN As New oledbConnection(cs)
CN.Open()
adp = New oledbDataAdapter()
adp.SelectCommand = New oledbCommand("SELECT distinct (employeeid) FROM employeeregistration", CN)
ds = New DataSet("ds")
adp.Fill(ds)
dtable = ds.Tables(0)
EmployeeID.Items.Clear()
For Each drow As DataRow In dtable.Rows
EmployeeID.Items.Add(drow(0).ToString())
Next
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub Attendance_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
fillcombo()
DataGridView1.DataSource = GetData()
End Sub
Private Sub EmployeeID_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EmployeeID.SelectedIndexChanged
Try
Delete.Enabled = True
con = New oledbConnection(cs)
con.Open()
Dim ct As String = "select employeename,basicworkingtime from employeeregistration where employeeid=@find"
cmd = New oledbCommand(ct)
cmd.Connection = con
cmd.Parameters.Add(New oledbParameter("@find", oledbType.Varchar, 30, "employeeid"))
cmd.Parameters("@find").Value = Trim(EmployeeID.Text)
rdr = cmd.ExecuteReader()
If rdr.Read Then
EmployeeName.Text = Trim(rdr.GetString(0))
BasicWorkingTime.Text = Trim(rdr.GetString(1))
End If
If Not rdr Is Nothing Then
rdr.Close()
End If
If con.State = ConnectionState.Open Then
con.Close()
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Sub Reset()
WorkingDate.Text = Today
EmployeeID.Text = ""
EmployeeName.Text = ""
InTime.Text = Now
OutTime.Text = Now
Overtime.Text = ""
Status.Text = ""
BasicWorkingTime.Text = ""
Delete.Enabled = False
Update_Record.Enabled = False
txtOutTime.Visible = False
txtInTime.Visible = False
Save.Enabled = True
End Sub
Private Sub NewRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewRecord.Click
Reset()
End Sub
Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click
If Len(Trim(EmployeeID.Text)) = 0 Then
MessageBox.Show("Please select employee id", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
EmployeeID.Focus()
Exit Sub
End If
If Len(Trim(Status.Text)) = 0 Then
MessageBox.Show("Please select Status", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Status.Focus()
Exit Sub
End If
Try
con = New OleDbConnection(cs)
con.Open()
Dim ct As String = "select employeeid,workingdate from employeeattendance where employeeid='" & EmployeeID.Text & "' and workingdate= #" & WorkingDate.Text & "#"
cmd = New OleDbCommand(ct)
cmd.Connection = con
rdr = cmd.ExecuteReader()
If rdr.Read Then
MessageBox.Show("Employee today's attendance is already saved", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
EmployeeID.Focus()
If Not rdr Is Nothing Then
rdr.Close()
End If
Else
con = New OleDbConnection(cs)
con.Open()
Dim cb As String = "insert into employeeAttendance(Workingdate,employeeid,status,intime,outtime,overtime,basicworkingtime) VALUES (@d1,@d2,@d4,@d5,@d6,@d7,@d8)"
cmd = New OleDbCommand(cb)
cmd.Connection = con
cmd.Parameters.Add(New OleDbParameter("@d1", OleDbType.VarChar, 30, "Workingdate"))
cmd.Parameters.Add(New OleDbParameter("@d2", OleDbType.VarChar, 30, "employeeid"))
cmd.Parameters.Add(New OleDbParameter("@d4", OleDbType.VarChar, 10, "status"))
cmd.Parameters.Add(New OleDbParameter("@d5", OleDbType.VarChar, 30, "intime"))
cmd.Parameters.Add(New OleDbParameter("@d6", OleDbType.VarChar, 30, "outtime"))
cmd.Parameters.Add(New OleDbParameter("@d7", OleDbType.VarChar, 10, "overtime"))
cmd.Parameters.Add(New OleDbParameter("@d8", OleDbType.VarChar, 10, "basicworkingtime"))
cmd.Parameters("@d1").Value = WorkingDate.Text
cmd.Parameters("@d2").Value = EmployeeID.Text
cmd.Parameters("@d4").Value = Status.Text
If Status.Text = "P" Then
cmd.Parameters("@d5").Value = InTime.Text
cmd.Parameters("@d6").Value = OutTime.Text
ElseIf Status.Text = "A" Then
cmd.Parameters("@d5").Value = txtInTime.Text
cmd.Parameters("@d6").Value = txtOutTime.Text
End If
cmd.Parameters("@d7").Value = Overtime.Text
cmd.Parameters("@d8").Value = BasicWorkingTime.Text
cmd.ExecuteReader()
MessageBox.Show("Successfully saved", "Employee Attendance", MessageBoxButtons.OK, MessageBoxIcon.Information)
If con.State = ConnectionState.Open Then
con.Close()
End If
con.Close()
Save.Enabled = False
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub Submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Submit.Click
Try
If EmployeeID.Text = "" Then
MessageBox.Show("Please select employee id", "input error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
Update_Record.Enabled = True
Delete.Enabled = True
con = New oledbConnection(cs)
con.Open()
Dim ct As String = "select Employeename,EmployeeAttendance.Basicworkingtime,status,intime,outtime,overtime from EmployeeAttendance,EmployeeRegistration where EmployeeRegistration.EmployeeID=EmployeeAttendance.EmployeeID and EmployeeRegistration.employeeid=@find and WorkingDate=@find1"
cmd = New oledbCommand(ct)
cmd.Connection = con
cmd.Parameters.Add(New oledbParameter("@find", oledbType.Varchar, 30, "employeeid"))
cmd.Parameters("@find").Value = Trim(EmployeeID.Text)
cmd.Parameters.Add(New oledbParameter("@find1", oledbType.Varchar, 30, "workingdate"))
cmd.Parameters("@find1").Value = Trim(WorkingDate.Text)
rdr = cmd.ExecuteReader()
If rdr.Read Then
EmployeeName.Text = Trim(rdr.GetString(0))
BasicWorkingTime.Text = Trim(rdr.GetString(1))
Status.Text = Trim(rdr.GetString(2))
If Status.Text = "P" Then
InTime.Text = Trim(rdr.GetString(3))
OutTime.Text = Trim(rdr.GetString(4))
ElseIf Status.Text = "A" Then
txtOutTime.Visible = True
txtInTime.Visible = True
txtOutTime.Text = Trim(rdr.GetString(3))
txtInTime.Text = Trim(rdr.GetString(4))
End If
InTime.Text = Trim(rdr.GetString(3))
OutTime.Text = Trim(rdr.GetString(4))
Overtime.Text = Trim(rdr.GetString(5))
Else
MessageBox.Show("No record found", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
If Not rdr Is Nothing Then
rdr.Close()
End If
If con.State = ConnectionState.Open Then
con.Close()
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub Update_Record_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Update_Record.Click
Try
con = New OleDbConnection(cs)
con.Open()
Dim cb As String = "update employeeAttendance set status='" & Status.Text & "',intime=@d5,outtime=@d6,overtime='" & Overtime.Text & "',basicworkingtime='" & BasicWorkingTime.Text & "' where workingdate= #" & WorkingDate.Text & "# and Employeeid='" & EmployeeID.Text & "'"
cmd = New oledbCommand(cb)
cmd.Connection = con
cmd.Parameters.Add(New OleDbParameter("@d5", OleDbType.VarChar, 30, "intime"))
cmd.Parameters.Add(New oledbParameter("@d6", oledbType.Varchar, 30, "outtime"))
If Status.Text = "P" Then
cmd.Parameters("@d5").Value = InTime.Text
cmd.Parameters("@d6").Value = OutTime.Text
ElseIf Status.Text = "A" Then
cmd.Parameters("@d5").Value = txtOutTime.Text
cmd.Parameters("@d6").Value = txtInTime.Text
End If
If MessageBox.Show("Are you sure want to update the record?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
cmd.ExecuteReader()
MessageBox.Show("Successfully updated", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
Reset()
End If
If con.State = ConnectionState.Open Then
con.Close()
End If
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub Delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Delete.Click
Try
If EmployeeID.Text = "" Then
MessageBox.Show("Please select employee id", "Entry", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Exit Sub
End If
If EmployeeID.Items.Count > 0 Then
If MessageBox.Show("Do you really want to delete the records?" & vbCrLf & "You can not restore the records" & vbCrLf & "It will delete all the attendance records permanently" & vbCrLf & "related to selected employee", "Employee's Attendance", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
delete_records()
End If
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub delete_records()
Try
Dim RowsAffected As Integer = 0
con = New oledbConnection(cs)
con.Open()
Dim cq As String = "delete from employeeattendance where EmployeeID=@DELETE1;"
cmd = New OleDbCommand(cq)
cmd.Connection = con
cmd.Parameters.Add(New oledbParameter("@DELETE1", oledbType.Varchar, 30, "EmployeeID"))
cmd.Parameters("@DELETE1").Value = Trim(EmployeeID.Text)
RowsAffected = cmd.ExecuteNonQuery()
If RowsAffected > 0 Then
MessageBox.Show("Successfully deleted", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
Reset()
Else
MessageBox.Show("No record found", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Information)
Reset()
con.Close()
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private ReadOnly Property Connection() As OleDbConnection
Get
Dim ConnectionToFetch As New OleDbConnection(cs)
ConnectionToFetch.Open()
Return ConnectionToFetch
End Get
End Property
Public Function GetData() As DataView
Dim SelectQry = "SELECT (EmployeeName) as [Employee Name],(EmployeeID) as [Employee ID] FROM EmployeeRegistration order by employeename"
Dim SampleSource As New DataSet
Dim TableView As DataView
Try
Dim SampleCommand As New oledbCommand()
Dim SampleDataAdapter = New oledbDataAdapter()
SampleCommand.CommandText = SelectQry
SampleCommand.Connection = Connection
SampleDataAdapter.SelectCommand = SampleCommand
SampleDataAdapter.Fill(SampleSource)
TableView = SampleSource.Tables(0).DefaultView
Catch ex As Exception
Throw ex
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Return TableView
End Function
Private Sub Overtime_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Overtime.KeyPress
If (e.KeyChar < Chr(48) Or e.KeyChar > Chr(57)) And e.KeyChar <> Chr(8) Then
e.Handled = True
End If
End Sub
Private Sub Status_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Status.SelectedIndexChanged
If Status.Text = "P" Then
txtOutTime.Visible = False
txtInTime.Visible = False
InTime.Enabled = True
OutTime.Enabled = True
InTime.Text = Now
OutTime.Text = Now
Overtime.Text = ""
ElseIf Status.Text = "A" Then
txtOutTime.Visible = True
txtInTime.Visible = True
txtOutTime.Text = "00:00:00"
txtInTime.Text = "00:00:00"
Overtime.Text = "00:00:00"
End If
End Sub
Private Sub OutTime_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OutTime.ValueChanged
Dim ts As TimeSpan
TimeSpan.TryParse(BasicWorkingTime.Text, ts)
Dim duration As TimeSpan = OutTime.Value - InTime.Value
Overtime.Text = Convert.ToString(duration - ts)
End Sub
Private Sub InTime_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles InTime.ValueChanged
Dim ts As TimeSpan
TimeSpan.TryParse(BasicWorkingTime.Text, ts)
Dim duration As TimeSpan = OutTime.Value - InTime.Value
Overtime.Text = Convert.ToString(duration - ts)
End Sub
Private Sub DataGridView1_RowHeaderMouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.RowHeaderMouseClick
Try
Dim dr As DataGridViewRow = DataGridView1.SelectedRows(0)
Me.EmployeeName.Text = dr.Cells(0).Value.ToString()
Me.EmployeeID.Text = dr.Cells(1).Value.ToString()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
End Class