I just want to know how to code a validation for example a user wants to add/update a record but it has the same department for example in the database it has a department A but then the user wants to add another department with the same name how can I tell the user that the department exists?
code for ADD
code for update
code for ADD
Code:
If cmdadd.Caption = "Add" Then
cmdedit.Enabled = False
cmddelete.Enabled = False
cbdep.Enabled = True
txtcode.Enabled = True
txtname.Enabled = True
cmdadd.Caption = "Save"
cmdclose.Caption = "Cancel"
ElseIf cmdadd.Caption = "Save" Then
If txtcode.Text = "" Or txtname.Text = "" Or cbdep.Text = "" Then
MsgBox "Please fill up the fields", vbInformation, "Case study!"
Else
OpenCon
Set rs = New ADODB.Recordset
rs.Open "Select * from [tblposition]", con, adOpenStatic, adLockOptimistic
rs.AddNew
rs![positioncode] = txtcode.Text
rs![positionname] = txtname.Text
rs![deptcode] = cbdep.Text
rs.Update
rs.Close
con.Close
Form1.Hide
MsgBox "Record saved!", vbInformation, "Case study!"
txtcode.Text = ""
txtname.Text = ""
cbdep.Text = ""
cmdadd.Caption = "Add"
cmdclose.Caption = "Close"
Call Form_Load
End If
End If
Code:
If cmdedit.Caption = "Edit" Then
txtcode.Enabled = True
txtname.Enabled = True
cbdep.Enabled = True
cmdadd.Enabled = False
cbdep.Enabled = True
cmddelete.Enabled = True
cmdedit.Caption = "Update"
cmdclose.Caption = "Cancel"
ElseIf cmdedit.Caption = "Update" Then
If txtname.Text = "" Or txtcode.Text = "" Or cbdep.Text = "" Then
MsgBox "Please fill up the fields!", vbInformation, "Case study"
Else
OpenCon
Set rs = New ADODB.Recordset
rs.Open "SELECT * FROM [tblposition]", con, adOpenStatic, adLockOptimistic
rs![positioncode] = txtcode.Text
rs![positionname] = txtname.Text
rs![deptcode] = cbdep.Text
rs.Update
rs.Close
con.Close
ListView1.ListItems.Clear
MsgBox "Record updated!", vbInformation, "Case study"
Call Form_Load
cmdedit.Caption = "Edit"
End If
End If
End Sub