เอาโค้ดมาดูนะครับ
ว่ามีตรงไหน ทำงานซ้ำซ้อน หรือเปล่า หรือการอ้างอิงต่างๆ อาจจะขัดแย้งกัน
ต้องดูโค้ดครับ
นี่คือโค้ดทั้งหมดที่อยู่บน frmInput จากไฟล์ที่ผมอัพโหลดให้ท่านครับ รบกวนช่วยดูให้หน่อยครับ มีตรงไหน ขัดแย้งบ้าง เท่าที่ดูด้วยตนเอง ก็ไม่น่ามีนะครับ
Option Compare Database
Option Explicit
Private str As String
Private Sub cmdInsertIMG_Click()
Dim iFileName As String
iFileName = iFileDialog
If iFileName & "" <> "" Then
Me.txtImagePath = Mid(iFileName, InStrRev(iFileName, "\"))
End If
End Sub
Private Sub cmdClear_Click()
Me.txtNationalID1 = ""
Me.txtNamePrefixThai1 = ""
Me.txtFirstNameThai1 = ""
Me.txtLastNameThai1 = ""
Me.txtNickName1 = ""
Me.txtNamePrefixEng1 = ""
Me.txtFirstNameEng1 = ""
Me.txtLastNameEng1 = ""
Me.txtBirthDate = ""
Me.txtBloodGroup = ""
Me.txtAddessNo1 = ""
Me.txtVillageNo1 = ""
Me.txtRoad1 = ""
Me.txtSubDistrict1 = ""
Me.txtDistrict1 = ""
Me.txtProvince1 = ""
Me.txtPostcode1 = ""
Me.txtTelMobile1 = ""
Me.txtImagePath = ""
Me.txtRaceID = ""
Me.txtNationalityID = ""
Me.txtReligion = ""
Me.txtEdu1 = ""
Me.txtSchool1 = ""
Me.txtEdu2 = ""
Me.txtSchool2 = ""
Me.txtDLType = ""
Me.txtDLExpDate = ""
Me.txtNationalID2 = ""
Me.txtNamePrefixThai2 = ""
Me.txtFirstNameThai2 = ""
Me.txtLastNameThai2 = ""
Me.txtNamePrefixEng2 = ""
Me.txtFirstNameEng2 = ""
Me.txtLastNameEng2 = ""
Me.txtNickName2 = ""
Me.txtAddessNo2 = ""
Me.txtVillageNo2 = ""
Me.txtRoad2 = ""
Me.txtSubDistrict2 = ""
Me.txtDistrict2 = ""
Me.txtProvince2 = ""
Me.txtPostcode2 = ""
Me.txtTelMobile2 = ""
Me.txtCompanyID = ""
Me.txtPlantID = ""
Me.txtDepartmentID = ""
Me.txtSectionID = ""
Me.txtSubSectionID = ""
Me.txtShiftID = ""
Me.txtSubShiftID = ""
Me.txtJobAreaID = ""
Me.txtWorkDetail = ""
Me.txtLabourType = ""
Me.txtContractType = ""
Me.txtFulltime = ""
Me.txtPayID = ""
Me.txtCompanyHiringDate = ""
Me.txtJobAreaEntryDate = ""
End Sub
Private Sub cmdSave_Click()
Dim ctrl As Control
Dim mSave As Boolean
Dim DontDuplicate As String
Dim str1 As String
str = Empty
'---------------------------------------------------------------------------------------------
' Copy Part 2 = Part 1 Me.txtNationalID2 = Me.txtNationalID1
Me.txtNamePrefixThai2 = Me.txtNamePrefixThai1
Me.txtFirstNameThai2 = Me.txtFirstNameThai1
Me.txtLastNameThai2 = Me.txtLastNameThai1
Me.txtNamePrefixEng2 = Me.txtNamePrefixEng1
Me.txtFirstNameEng2 = Me.txtFirstNameEng1
Me.txtLastNameEng2 = Me.txtLastNameEng1
Me.txtNickName2 = Me.txtNickName1
Me.txtAddessNo2 = Me.txtAddessNo1
Me.txtVillageNo2 = Me.txtVillageNo1
Me.txtRoad2 = Me.txtRoad1
Me.txtSubDistrict2 = Me.txtSubDistrict1
Me.txtDistrict2 = Me.txtDistrict1
Me.txtProvince2 = Me.txtProvince1
Me.txtPostcode2 = Me.txtPostcode1
Me.txtTelMobile2 = Me.txtTelMobile1
'---------------------------------------------------------------------------------------------
' Check IsNullOrEmptyFor Each ctrl In Me.Controls
If TypeOf ctrl Is TextBox Then
If isnullorEmptyTbx(ctrl) Then
ctrl.BackColor = RGB(119, 192, 212)
ctrl.BorderColor = RGB(157, 187, 97)
str = str & ctrl.Tag & vbNewLine
Else
ctrl.BackColor = vbWhite
ctrl.BorderColor = RGB(192, 192, 192)
End If
End If
Next ctrl
For Each ctrl In Me.Controls
If TypeOf ctrl Is ComboBox Then
If isnullorEmptyCbx(ctrl) Then
ctrl.BackColor = RGB(119, 192, 212)
ctrl.BorderColor = RGB(157, 187, 97)
str = str & ctrl.Tag & vbNewLine
Else
ctrl.BackColor = vbWhite
ctrl.BorderColor = RGB(192, 192, 192)
End If
End If
Next ctrl
If IsNull(str) Or str = "" Then
DontDuplicate = Me.txtNationalID1.Value
str1 = "[NationalID]=" & "'" & DontDuplicate & "'"
If Me.txtNationalID1 = DLookup("[NationalID]", "tblContractor", str1) Then
MsgBox "A record " & txtNationalID1 & " is duplicate. " & "Please check record.", vbInformation
Me.Undo
Exit Sub
Else
'---------------------------------------------------------------------------------------------
' Insert into table If MsgBox("Are you sure you want to save?", vbQuestion + vbYesNo, "Save Confirmation") = vbYes Then
mSave = True
On Error Resume Next
DoCmd.SetWarnings False
DoCmd.RunSQL "INSERT INTO tblContractor([NationalID],[NamePrefixThai],[FirstNameThai],[LastNameThai],[NamePrefixEng],[FirstNameEng],[LastNameEng],[NickName],[BirthDate],[BloodGroup],[AddessNo],[VillageNo],[Road],[SubDistrict],[District],[Province],[Postcode],[TelMobile],[ImagePath],[RaceID],[NationalityID],[Religion],[Edu1],[School1],[Edu2],[School2],[DLType],[DLExpDate])" & _
"Values ('" & Me.txtNationalID1 & "', '" & Me.txtNamePrefixThai1 & "', '" & Me.txtFirstNameThai1 & "', '" & Me.txtLastNameThai1 & "', '" & Me.txtNamePrefixEng1 & "', '" & Me.txtFirstNameEng1 & "', '" & Me.txtLastNameEng1 & "', '" & Me.txtNickName1 & "', '" & Me.txtBirthDate & "', '" & _
Me.txtBloodGroup & "', '" & Me.txtAddessNo1 & "', '" & Me.txtVillageNo1 & "', '" & Me.txtRoad1 & "', '" & Me.txtSubDistrict1 & "', '" & Me.txtDistrict1 & "', '" & Me.txtProvince1 & "', '" & Me.txtPostcode1 & "', '" & Me.txtTelMobile1 & "', '" & Me.txtImagePath & "', '" & Me.txtRaceID & "', '" & _
Me.txtNationalityID & "', '" & Me.txtReligion & "', '" & Me.txtEdu1 & "', '" & Me.txtSchool1 & "', '" & Me.txtEdu2 & "', '" & Me.txtSchool2 & "', '" & Me.txtDLType & "', '" & Me.txtDLExpDate & "') ;"
DoCmd.RunSQL "INSERT INTO tblWork([NationalID],[NamePrefixThai],[FirstNameThai],[LastNameThai],[NamePrefixEng],[FirstNameEng],[LastNameEng],[NickName],[AddessNo],[VillageNo],[Road],[SubDistrict],[District],[Province],[Postcode],[TelMobile],[CompanyID],[PlantID]," & _
"[DepartmentID],[SectionID],[SubSectionID],[ShiftID],[SubShiftID],[JobAreaID],[WorkDetail],[LabourType],[ContractType],[Fulltime],[PayID],[CompanyHiringDate],[JobAreaEntryDate])" & _
"Values ('" & Me.txtNationalID2 & "', '" & Me.txtNamePrefixThai2 & "', '" & Me.txtFirstNameThai2 & "', '" & Me.txtLastNameThai2 & "', '" & Me.txtNamePrefixEng2 & "', '" & Me.txtFirstNameEng2 & "', '" & Me.txtLastNameEng2 & "', '" & Me.txtNickName2 & "', '" & Me.txtAddessNo2 & "', '" & _
Me.txtVillageNo2 & "', '" & Me.txtRoad2 & "', '" & Me.txtSubDistrict2 & "', '" & Me.txtDistrict2 & "', '" & Me.txtProvince2 & "', '" & Me.txtPostcode2 & "', '" & Me.txtTelMobile2 & "', '" & Me.txtCompanyID & "', '" & Me.txtPlantID & "', '" & Me.txtDepartmentID & "', '" & _
Me.txtSectionID & "', '" & Me.txtSubSectionID & "', '" & Me.txtShiftID & "', '" & Me.txtSubShiftID & "', '" & Me.txtJobAreaID & "', '" & Me.txtWorkDetail & "', '" & Me.txtLabourType & "', '" & Me.txtContractType & "', '" & Me.txtFulltime & "', '" & Me.txtPayID & "', '" & Me.txtCompanyHiringDate & "', '" & Me.txtJobAreaEntryDate & "');"
MsgBox ("Your record has been successfully saved!")
'---------------------------------------------------------------------------------------------
' Clear Textbox Me.txtNationalID1 = ""
Me.txtNamePrefixThai1 = ""
Me.txtFirstNameThai1 = ""
Me.txtLastNameThai1 = ""
Me.txtNickName1 = ""
Me.txtNamePrefixEng1 = ""
Me.txtFirstNameEng1 = ""
Me.txtLastNameEng1 = ""
Me.txtBirthDate = ""
Me.txtBloodGroup = ""
Me.txtAddessNo1 = ""
Me.txtVillageNo1 = ""
Me.txtRoad1 = ""
Me.txtSubDistrict1 = ""
Me.txtDistrict1 = ""
Me.txtProvince1 = ""
Me.txtPostcode1 = ""
Me.txtTelMobile1 = ""
Me.txtImagePath = ""
Me.txtRaceID = ""
Me.txtNationalityID = ""
Me.txtReligion = ""
Me.txtEdu1 = ""
Me.txtSchool1 = ""
Me.txtEdu2 = ""
Me.txtSchool2 = ""
Me.txtDLType = ""
Me.txtDLExpDate = ""
Me.txtNationalID2 = ""
Me.txtNamePrefixThai2 = ""
Me.txtFirstNameThai2 = ""
Me.txtLastNameThai2 = ""
Me.txtNamePrefixEng2 = ""
Me.txtFirstNameEng2 = ""
Me.txtLastNameEng2 = ""
Me.txtNickName2 = ""
Me.txtAddessNo2 = ""
Me.txtVillageNo2 = ""
Me.txtRoad2 = ""
Me.txtSubDistrict2 = ""
Me.txtDistrict2 = ""
Me.txtProvince2 = ""
Me.txtPostcode2 = ""
Me.txtTelMobile2 = ""
Me.txtCompanyID = ""
Me.txtPlantID = ""
Me.txtDepartmentID = ""
Me.txtSectionID = ""
Me.txtSubSectionID = ""
Me.txtShiftID = ""
Me.txtSubShiftID = ""
Me.txtJobAreaID = ""
Me.txtWorkDetail = ""
Me.txtLabourType = ""
Me.txtContractType = ""
Me.txtFulltime = ""
Me.txtPayID = ""
Me.txtCompanyHiringDate = ""
Me.txtJobAreaEntryDate = ""
Me.Requery
Else
mSave = False
Me.Undo
Exit Sub
End If
End If
Else
MsgBox "Please enter data for all the required fields", vbInformation + vbOKOnly, "Form is not fullfilled completly"
Exit Sub
End If
End Sub
Private Sub Form_Current()
' txtHide Me.txtNationalID1.SetFocus
End Sub
Private Sub Form_Error(DataErr As Integer, Response As Integer)
If DataErr = 2113 Then
MsgBox "Your input is incorrect. Please scan your card again", , "Input Error"
Response = acDataErrContinue
Me.Undo
End If
End Sub
'Private Sub txtDLType_AfterUpdate()
' txtHide
'End SubPrivate Sub txtPlantID_Change()
Me.txtDepartmentID.Requery
End Sub
Private Sub txtDepartmentID_Change()
Me.txtSectionID.Requery
End Sub
'Private Sub txtReligion_KeyDown(KeyCode As Integer, Shift As Integer)
'Select Case KeyCode
' Case vbKeyReturn, vbKeyTab, vbKeyUp, vbKeyDown
' KeyCode = KeyCode 'Accept these keys
' Case Else
' KeyCode = 0 'Block all other keys
'End Select
'End SubPrivate Sub txtSectionID_Change()
Me.txtSubSectionID.Requery
End Sub
Private Sub txtSubSectionID_Change()
Me.txtShiftID.Requery
End Sub
Private Sub txtShiftID_Change()
Me.txtSubShiftID.Requery
End Sub
Private Sub txtSubShiftID_Change()
Me.txtJobAreaID.Requery
End Sub
'Private Sub txtHide()
' Me.txtDLExpDate.Visible = Not (IsNull(Me.txtDLType) Or Me.txtDLType = 0 Or Me.txtDLType = "No" Or Me.txtDLType = "N/A")
'End Sub