แสดงกระทู้

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - PNR

หน้า: 1 ... 20 21 22 [23] 24 25 26 ... 32
397
ข้อ 1 แก้เป็น
โค๊ด: [Select]
Private Sub SearchCombo()
    Dim sql As String
    sql = "SELECT * FROM tblWorkforceY2020 WHERE (((tblWorkforceY2020.DepartmentID)=[forms]![frmWorkforce]![cbSearch1]) AND  ((tblWorkforceY2020.SectionID)=[forms]![frmWorkforce]![cbSearch2]));"
    Forms!frmWorkforce.RecordSource = sql
    Forms!frmWorkforce.Requery
End Sub

หรือแบบนี้
โค๊ด: [Select]
Dim sql As String
     sql = "SELECT * FROM qryWorkforceY2020 WHERE [DepartmentID]=" & Me.cbSearch1 & " AND [SectionID]=" & Me.cbSearch2 & ""

    Forms!frmWorkforce.RecordSource = sql
    Forms!frmWorkforce.Requery

ข้อ 1 แนะนำให้ใช้แบบนี้
โค๊ด: [Select]
Private Sub SearchCombo()
    Dim sql As String
    sql = "SELECT * FROM qryWorkforceY2020 WHERE [DepartmentID] Like '" & Nz(Me.cbSearch1, "*") & "' AND [SectionID] Like '" & Nz(Me.cbSearch2, "*") & "' AND [ShiftID] Like '" & Nz(Me.cbSearch3, "*") & "'"
    Forms!frmWorkforce.RecordSource = sql
    Forms!frmWorkforce.Requery
End Sub
Private Sub cbSearch1_AfterUpdate()
    SearchCombo
End Sub
Private Sub cbSearch2_AfterUpdate()
    SearchCombo
End Sub
Private Sub cbSearch3_AfterUpdate()
  SearchCombo
End Sub

ข้อที่ 2 แก้เป็นแบบนี้
โค๊ด: [Select]
DoCmd.OpenReport "rptCard1", acViewPreview, , "[Fulltime]='Fulltime' AND [CreateID] = " & Forms!frmCreateCard!txtCreateID
โพสต์นี้ได้รับคำขอบคุณจาก: Tatchawin

398
Dim sql As Integer ต้องแก้เป็น  Dim sql As String นะครับ

Private Sub SearchCombo()
Dim SQL As String
SQL = "SELECT * FROM tblWorkforceY2020 WHERE [DepartmentID] = " & Me.cbSearch1
    Forms!frmWorkforce.RecordSource = SQL
    Forms!frmWorkforce.Requery
End Sub


โพสต์นี้ได้รับคำขอบคุณจาก: Tatchawin

399
ห้อง MS Access / : คำสั่ง Answer เกิด error
« เมื่อ: 15 มี.ค. 63 , 08:22:34 »
เพิ่มเป็น

Private Sub Command166_Click()
            On Error Resume Next
            Dim strSQL,Answer As String
            Answer = MsgBox("คุณแน่ใจเหรอว่าต้องการลบตารางคนมามอบตัวทิ้ง" & vbCrLf & "ตรวจสอบให้แน่ใจ เอาคืนไม่ได้" & vbCrLf & "กด yes = ยืนยันการลบ" & vbCrLf & "กด No = ยกเลิกการลบ", vbYesNo + vbDefaultButton2, "!!!!ลบตารางคนมามอบตัว!!!!")
            If Answer = vbYes Then

            DoCmd.SetWarnings False
            strSQL = "Delete * from m1_Total_student;"
            DoCmd.RunSQL strSQL
 
            MsgBox "คุณได้ลบตารางคนมามอบตัวทิ้งเรียบร้อยแล้ว" & vbCrLf & "กรุณาตรวจสอบข้อมูล", , "ผลการลบตารางคนมามอบตัว"
            DoCmd.Close
            DoCmd.OpenForm "frm_main_menu"
            DoCmd.MoveSize 300, 400, 18500, 11000
            DoCmd.SetWarnings True
            Else
            MsgBox "คุณได้ยกเลิกการลบตารางคนมามอบตัวทิ้ง", , "ยกเลิก"
            End If
End Sub

Error เกิดจาก Answer ยังไม่ได้กำหนดให้เป็นตัวแปร เลยครับ แต่มีการนำมาเรียกใช้แล้ว เลยเกิดปัญหาดังกล่าว
โพสต์นี้ได้รับคำขอบคุณจาก: pirin

400
ลองทำการ compact-repair ดูก่อนว่าหายไหม

หรือลองดู แบบนี้ว่าแก้ได้ไหม



ถ้ายังไม่ได้อีก ให้ Backup File ต้นฉบับไว้ Copy มาซัก 1 อัน
เปิดไฟล์ใหม่ขึ้นมาสร้าง Module แล้วใส่ Codeนี้เข้าไป
โค๊ด: [Select]
Sub FixBadAOIndex(BadDBPath As String)
    ' <BadDBPath> is the path to the corrupt database.
    Dim dbBad As DAO.Database
    Dim tdf As DAO.TableDef
    Dim ix As DAO.Index

    Set dbBad = DBEngine.OpenDatabase(BadDBPath)
    dbBad.Execute "DELETE FROM MSysAccessObjects " & _
        "WHERE ([ID] Is Null) OR ([Data] Is Null)", _
        dbFailOnError
    Set tdf = dbBad.TableDefs("MSysAccessObjects")
    Set ix = tdf.CreateIndex("AOIndex")
    With ix
        .Fields.Append .CreateField("ID")
        .Primary = True
    End With
    tdf.Indexes.Append ix
    Set tdf = Nothing
    dbBad.Close
    Set dbBad = Nothing
End Sub

ต่อไปกด Ctrl+G แล้วใส่ FixBadAOIndex("ตำแหน่งไฟล์ที่เสียหายตั้งอยู่")
เช่น FixBadAOIndex("C:\MyPath\MyFile.mdb")
หรือส่งไฟล์เจ้าปัญหา มาทาง Inbox ผมแก้ไขให้
โพสต์นี้ได้รับคำขอบคุณจาก: sjs, Eakaluk Sansila

401
ห้อง MS Access / : Loop เปลี่ยนสีแถบ Navigation
« เมื่อ: 13 มี.ค. 63 , 08:42:32 »
แบบนี้ไหมครับ



Sub Formstyle(frm As Form)
For Each ctl In frm.Controls
         If ctl.ControlType = acNavigationButton Or acNavigationControl And ctl.Tag = "b1" Then
                      ctl.BackColor = 4210943
         End If
    Next ctl
    'ctl = Nothing
End Sub

โพสต์นี้ได้รับคำขอบคุณจาก: Kxess

402
ห้อง MS Access / : Loop เปลี่ยนสีแถบ Navigation
« เมื่อ: 12 มี.ค. 63 , 11:47:36 »
ทำปุ่มพื้นหลัง ให้เป็นสีที่ต้องการใช่ไหมคับ

โค๊ด: [Select]
Sub Formstyle(frm As Form)
Dim ctl     As Control
    For Each ctl In frm.Controls
        If ctl.ControlType = acNavigationButton And ctl.Tag = "b1" Then
                      ctl.BackColor = 11193702
             End If
    Next ctl
End Sub

แต่ถ้าอยากให้เปิดขึ้นมาแล้ว แสดงสี ถ้า tag = B1 ทั้งแบบที่กด Tab อยู่ และ ที่ยังไม่กดก็เพิ่มเป็น
โค๊ด: [Select]
Sub Formstyle(frm As Form)
Dim ctl     As Control
    For Each ctl In frm.Controls
        If ctl.ControlType = acNavigationButton And ctl.Tag = "b1" Then
                      ctl.BackColor = 11193702
                      ctl.PressedColor = 11193702
             End If
    Next ctl
End Sub
โพสต์นี้ได้รับคำขอบคุณจาก: Kxess

403
ความคิดผมอยากทำ Form 1 หน้าแล้วมี Textbox หรือ ช่องเลือกวัน 2 ช่องให้ใส่วันที่ต้องการเริ่มต้นหากับวันสุดท้ายที่ต้องการหาแล้วให้ Query ออกมาเป็น DataSheet ข้อมูลที่ต้องการ ไม่ทราบว่า Criteria ใน Query สามารถทำได้ไหมครับ???
สามารถทำได้ครับ  แนวทางที่คิดมาถูกต้องแล้ว

ผมสมมุติ ฟิลล์ ชื่อ OpenJobs นะครับ
มี textbox 2 ตัวชื่อ txtStartDate กับ txtEndDate
บน form ชื่อ form1

ที่ฟิลล์  OpenJobs ใส่ Criteria ว่า
Between Forms!Form1!txtStartDate And Forms!form1!txtEndDate

ข้อมูลที่ได้จากคิวรี่นี้จะได้จากวันที่ เริ่มต้นและสิ้นสุดที่เรากำหนดครับ
หมายเหตุ ให้ดูนะครับว่า ที่ตารางกำหนด Format วันที่ไว้เป็นแบบไหน ให้กำหนดให้เหมือนกันนะครับ
โพสต์นี้ได้รับคำขอบคุณจาก: pattan0013

404
มีฟิลด์ SEQ ชนิด Number Long Integer
ต้องการเขียนโคตให้ Run เลขตั้งแต่ที่กำหนด ไปจนหมดเรคคอด
ต้องเขียนยังงัยครับ ขอบคุณครับ
มี 2 แบบไม่รู้ใช่ที่ต้องการไหมนะครับ

แบบที่1
ผมสมมุติมีฟอร์มแบบ Continuous Forms
มี textbox ระบุเลขที่กำหนดชื่อ txtSeqNum
โดยตารางที่จะดำเนินการชื่อ table1
มีฟิลล์ที่จะ รันตัวเลขชื่อ Seq




Private Sub Command0_Click()
Dim db As Database
Dim rs As DAO.Recordset
   Set db = CurrentDb
   Set rs = db.OpenRecordset("table1")   
 rs.MoveFirst
 Do Until rs.EOF
 If Nz(DMax("seq", "table1"), 0) = 0 Then
         rs.Edit
         rs!Seq = Me.txtSeqNum
         rs.Update
Else
         rs.Edit
         rs!Seq = DMax("seq", "table1") + 1
         rs.Update
End If
         rs.MoveNext
Loop
         Me.Requery
End Sub

แบบที่ 2
ถ้าไม่อยากสร้างฟอร์มขึ้นมาเพิ่ม ใช้การสร้าง ปุ่มขึ้นมา 1 ปุ่มแล้วใส่โค้ดนี้เข้าไปเพื่อทำการแก้ไขตาราง ที่ฟิลล์ Seq
โค๊ด: [Select]
Private Sub Command0_Click()
Dim db As Database
Dim rs As DAO.Recordset
Dim IntSeq As Integer
   Set db = CurrentDb
   Set rs = db.OpenRecordset("table1")
   IntSeq = InputBox("ระบุตัวเลขเริ่มต้น", "ระบบรันSeq")   
 rs.MoveFirst
 Do Until rs.EOF
 If Nz(DMax("seq", "table1"), 0) = 0 Then
         rs.Edit
         rs!Seq = IntSeq
         rs.Update
 Else
         rs.Edit
         rs!Seq = DMax("seq", "table1") + 1
         rs.Update
 End If
         rs.MoveNext
 Loop
    End Sub
โพสต์นี้ได้รับคำขอบคุณจาก: Un

405

ใช้ Like มาช่วยก็ได้ครับ เจอประโยคนี้ในข้อความก็เข้าเงื่อนไข เช่น

โค๊ด: [Select]
Private Sub Command2_Click()
If Me.Combo1.Value Like "*กรุงไทย*" Then
DoCmd.OpenForm "frm01"
ElseIf Me.Combo1.Value Like "*ไทยพานิชย์*" Then
DoCmd.OpenForm "frm02"
End If
End Sub
โพสต์นี้ได้รับคำขอบคุณจาก: Un

406
ของผม เชื่อม ODBC driver Link Table ต่างๆ เข้ามากำหนด column ได้ปกติ
โพสต์นี้ได้รับคำขอบคุณจาก: Tatchawin

407
โดย combobox มัน สามารถกำหนด Column ที่จะผูกค่าไว้เช่น กำหนดให้มี ServiceID และ  ServiceType
โดยกำหนดให้ BoundColumn 1 (ServiceID)
กำหนดให้แสดง 2 คอลัมน์
และกำหนด ความกว้างColumn = 0;5
มันจะแสดงเฉพาะ ServiceType แต่ค่าที่เรากำหนดให้เก็บในตารางคือ ServiceID ซึ่งเป็น Number นั้นเองครับ

ซึ่งการ lookup ในตารางก็มีการทำแบบนี้เหมือนกัน คือการให้แสดง column ServiceType  แต่จริงๆแล้วถ้าไม่ lookup นั้นมันเป็น ServiceID ตามปกติครับ
โพสต์นี้ได้รับคำขอบคุณจาก: Tatchawin

408
ตัว Control เป็น Unbound หรือ Bound ครับ

อ้างถึง
แบบที่พอจบงานแล้วเคลียคอนโทรลทั้งหมด
เคลียทีเดียวหมด ใช้แบบนี้ดูครับ
ใช้กับ Control ที่ Unbound

โค๊ด: [Select]
Sub ResetForm()   
    On Error GoTo Err_Err
    Dim ctl As Control           
    For Each ctl In Me
        If ctl.ControlType = acComboBox Then  'ถ้าเป็นCombobox ทำให้ว่าง
                ctl = Null       
        End If
        If ctl.ControlType = acTextBox Then   'ถ้าเป็น textbox ทำให้ว่าง
                ctl = Null
        End If
        If ctl.ControlType = acCheckBox Then  'ถ้าเป็น Checkbox ทำให้แสดงเป็น False คือไม่ได้ติ๊กเลือก
                ctl = False
        End If
          Next ctl
Exit_err:
        Exit Sub
Err_Err:
        MsgBox Error$
        MsgBox ("ผิดพลาดตรง Reset Form")
        Resume Exit_err
End Sub

เวลาจะเรียกใช้ ที่ปุ่มเซฟ เมื่อเซฟเสร็จแล้ว
ก็ต่อคำสั่งด้วย
Call ResetForm

อ้างถึง
มีวธีทำให้คิวรีแบบอัปเดททำงานพร้อมกันหมดไหมครับ แบบ ไม่ต้องมากดอัทเดททีล่ะกัน แต่ให้กดทีเดียว ทำงานทุกอันอะครับ
พอจะมีวิธีใหมครับ
สั่งทำงานทีเดียวได้เลยเช่น
DoCmd.SetWarnings False  'ปิดระบบสอบถาม
DoCmd.OpenQuery "Query1"
DoCmd.OpenQuery "Query2"
DoCmd.OpenQuery "Query3"
DoCmd.SetWarnings True  'เมื่อเรียกใช้คิวรี่ทั้ง 3 เสร็จ ก็ให้เปิดระบบสอบถามปกติ

อ้างถึง
แบบที่พอจบงานแล้วเคลียเรคคอททั้งหมด
เคลียเรคคอร์ดทั้งหมด ในตารางใช่หรือไม่ถ้าใช่สามารถใช้คำสั่ง Sql ลบได้เช่น

Dim db As Database
Set db = CurrentDb
db.Execute "DELETE ชื่อตาราง.* FROM ชื่อตาราง;"

อ้างถึง
แบบเลือกให้ลบข้อมูลในเรคคอทที่ต้องการได้

เราสามารถใช้คำสั่งแบบข้างบน โดยกำหนดลบจาก ID เช่น

    Dim strSQL as string
    DoCmd.SetWarnings False
    strSQL = "Delete * From [student] WHERE [Student ID]='007'"
    DoCmd.RunSQL strSQL
    DoCmd.SetWarnings True

คำสั่งนี้ก็จะลบเรคคอร์ดจากตาราง Student โดยจะลบเรคคอร์ดที่ Student ID = 007 ออกไป
โพสต์นี้ได้รับคำขอบคุณจาก: ชุติพนธ์ มหาทรัพย์

409
อยากได้ตัวอย่าง
คลิกปุ่มแล้วเปิดหน้าต่างเลือกไฟล์(Textfile)
เมื่อเลือกแล้ว มีปุ่มกดเพื่อนนำเข้าไฟล์นั้นมาในฐานข้อมูล
ขอบคุณครับ
สมมุติ ตารางที่จะนำเข้าจากไฟล์ CSV ชื่อ table1 นะครับ

โค๊ด: [Select]
Private Sub Command0_Click()
Dim File As FileDialog
Dim getfile As String
Set File = Application.FileDialog(msoFileDialogFilePicker)
With File
.Title = "เลือก ไฟล์ที่จะนำเข้า"
        .Filters.Clear
        .Filters.Add "Text Files", "*.csv"
.AllowMultiSelect = False
End With
If File.Show Then
getfile = File.SelectedItems.Item(1)

DoCmd.TransferText TransferType:=acImportDelim, TableName:="table1", FileName:=getfile, HasFieldNames:=True
MsgBox "นำ้้เข้าเรียบร้อยแล้ว"

End If
End Sub

โพสต์นี้ได้รับคำขอบคุณจาก: Un

410
ลองดูครับ

โค๊ด: [Select]
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.txtTelephoneMobile2 = Me.txtTelephoneMobile1
'---------------------------------------------------------------------------------------------
' Check IsNullOrEmpty

For 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],[TelephoneMobile],[ImagePath])" & _
            "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.txtTelephoneMobile1 & "', '" & Me.txtImagePath & "');"
   
            DoCmd.RunSQL "INSERT INTO tblWork([NationalID],[NamePrefixThai],[FirstNameThai],[LastNameThai],[NamePrefixEng],[FirstNameEng],[LastNameEng],[NickName],[AddessNo],[VillageNo],[Road],[SubDistrict],[District],[Province],[Postcode],[TelephoneMobile],[CompanyID],[PlantID],[DepartmentID],[SectionID],[SubSectionName],[JobAreaName]," & _
            "[WorkDetail],[ContractType],[WorkContractID],[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.txtTelephoneMobile2 & "', '" & Me.txtCompanyID & "', '" & Me.txtPlantID & "', '" & Me.txtDepartmentID & "', '" & _
            Me.txtSectionID & "', '" & Me.txtSubSectionName & "', '" & Me.txtJobAreaName & "', '" & Me.txtWorkDetail & "', '" & Me.txtContractType & "', '" & Me.txtWorkContractID & "', '" & 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.txtTelephoneMobile1 = ""
            Me.txtImagePath = ""
           
            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.txtTelephoneMobile2 = ""
            Me.txtCompanyID = ""
            Me.txtPlantID = ""
            Me.txtDepartmentID = ""
            Me.txtSectionID = ""
            Me.txtSubSectionName = ""
            Me.txtJobAreaName = ""
            Me.txtWorkDetail = ""
            Me.txtContractType = ""
            Me.txtWorkContractID = ""
            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
โพสต์นี้ได้รับคำขอบคุณจาก: Tatchawin

411
อ่อข้อมูลเป็น Bound Control อยู่แล้ว งั้นก็ทำแบบนี้ละครับ
โพสต์นี้ได้รับคำขอบคุณจาก: Tatchawin

412
ครับ ต้องตั้งค่าตรงนี้เป็นไทย หรือ อังกฤษ ครับ



ตัวอย่างแปลงแล้วด้านล่างครับ
โพสต์นี้ได้รับคำขอบคุณจาก: SakDa

413
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.txtTelephoneMobile2 = Me.txtTelephoneMobile1
'---------------------------------------------------------------------------------------------
' Check IsNullOrEmpty

For 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
   
   
'---------------------------------------------------------------------------------------------
'CheckDuplicate
   DontDuplicate = Me.txtNationalID1.Value
           str1 = "[National]=" & "'" & DontDuplicate & "'"
       
        If Me.txtNationalID1 = DLookup("[NationalID]", "tblContractor", str1) Then
        MsgBox "A record " & txtNationalID1 & " is duplicate. " & "Please check record.", vbInformation
       exit sub
   else
' Insert into table
   If IsNull(str) Or str = "" Then
           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],[TelephoneMobile],[ImagePath])" & _
            "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.txtTelephoneMobile1 & "', '" & Me.txtImagePath & "');"
   
            DoCmd.RunSQL "INSERT INTO tblWork([NationalID],[NamePrefixThai],[FirstNameThai],[LastNameThai],[NamePrefixEng],[FirstNameEng],[LastNameEng],[NickName],[AddessNo],[VillageNo],[Road],[SubDistrict],[District],[Province],[Postcode],[TelephoneMobile],[CompanyID],[PlantID],[DepartmentID],[SectionID],[SubSectionName],[JobAreaName]," & _
            "[WorkDetail],[ContractType],[WorkContractID],[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.txtTelephoneMobile2 & "', '" & Me.txtCompanyID & "', '" & Me.txtPlantID & "', '" & Me.txtDepartmentID & "', '" & _
            Me.txtSectionID & "', '" & Me.txtSubSectionName & "', '" & Me.txtJobAreaName & "', '" & Me.txtWorkDetail & "', '" & Me.txtContractType & "', '" & Me.txtWorkContractID & "', '" & 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.txtTelephoneMobile1 = ""
            Me.txtImagePath = ""
           
            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.txtTelephoneMobile2 = ""
            Me.txtCompanyID = ""
            Me.txtPlantID = ""
            Me.txtDepartmentID = ""
            Me.txtSectionID = ""
            Me.txtSubSectionName = ""
            Me.txtJobAreaName = ""
            Me.txtWorkDetail = ""
            Me.txtContractType = ""
            Me.txtWorkContractID = ""
            Me.txtCompanyHiringDate = ""
            Me.txtJobAreaEntryDate = ""
       
            Me.Requery
        Else
            mSave = False
            Me.Undo
           
            Exit Sub
        End If
        Else
'            MsgBox "Please enter data for all the required fields below" & vbNewLine & _
'            String(52, "-") & vbCrLf & str, vbInformation + vbOKOnly, "Form is not fullfilled completly"
            MsgBox "Please enter data for all the required fields", vbInformation + vbOKOnly, "Form is not fullfilled completly"
        Exit Sub
       
        End If
End if
End Sub
โพสต์นี้ได้รับคำขอบคุณจาก: Tatchawin

414
โค๊ด: [Select]
Private Sub Form_BeforeUpdate(Cancel As Integer)
   Dim ctl As Control
   Dim msg As String
   On Error GoTo Err_BeforeUpdate
   If Me.Dirty Then
   msg = (MsgBox("คุณต้องการบันทึกหรือไม่?", vbYesNo + vbQuestion, "Save Record"))
   If msg = vbNo Then
         Me.Undo
    End If
   If msg = vbYes Then
         Modified = Now()
         ModifiedBy = CurrentUser()
      End If
   End If
Exit_BeforeUpdate:
   Exit Sub
Err_BeforeUpdate:
   MsgBox Err.Number & " " & Err.Description
   Resume Exit_BeforeUpdate
End Sub


ยุบรวมกัน เมื่อเราตอบ Yes ครับ
โดย จากโค้ดนี้ ต้องมี textbox  Modified และ ModifiedBy ไว้รับค่าด้วยนะ
         Modified = Now()   คือ Modified จะจัดเก็บวันที่และเวลาตอนนั้นไว้
         ModifiedBy = CurrentUser() คือ ModifiedBy จะจัดเก็บUser ที่ทำการบันทึก

แต่ปกติผมไม่ค่อยใช้ Event BeforeUpdate ครับ
ถ้ามีตัวอย่างผมเพิ่มเติมให้ได้ครับ
โพสต์นี้ได้รับคำขอบคุณจาก: Tatchawin

หน้า: 1 ... 20 21 22 [23] 24 25 26 ... 32