Page 1 of 1

คำสั่ง VBA ค้นหาข้อมูลที่บันทึกไปแล้วและทำการแก้ไขข้อมูลและบันทึกการแก้ไขค่ะ

Posted: Fri Dec 14, 2018 3:48 pm
by Bella08
รบกวนตรวจสอบ Code ใน Userform 1 คำสั่ง:เมื่อค้นหาข้อมูลจาก sheet DataInput จากนั้นทำการแก้ไขข้อมูลและบันทึก โดยกดปุ่ม"บันทึกข้อมูลการสั่งซื้อ" ข้อมูลจะบันทึกในบรรทัดถัดไป ไม่แก้ไขข้อมูลในแถวที่เลือกค่ะ ซึ่งต้องการให้ข้อมูลบันทึกลงแถวเดิมค่ะ และ ใน UserFom 1 เมื่อคลิกปุ่มถัดไปและก่อนหน้าก็ ข้อมูลที่แสดงไม่ตรงกับแถวที่อยู่ถัดไปและก่อนหน้าคะ
ตัวอย่างตามไฟล์แนบค่ะ

Code: Select all

Private Sub CommandButton2_Click()
    Dim irow As Long
    Dim ws As Worksheet
    Set ws = Worksheets("DataInput")
    
    'find first empty row in database
    irow = ws.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0).Row
        
    'Check for Id
    If (Me.Textpo.Text) = "" Then
        Me.Textpo.SetFocus
    Exit Sub
   End If
    If Application.CountIf(Range("c:c"), Textpo.Text) > 0 Then
        irow = Application.Match((Textpo.Text), Range("c:c"), 0)
        msgRepns = MsgBox("เลขที่ PO นี้มีอยู่แล้วถ้าต้องการแก้ไขข้อมูลคลิก Yes หากต้องการยกเลิกให้คลิก NO", vbYesNo)
    Else
        irow = ws.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0).Row
    End If
    If msgRepns = vbNo Then
        Exit Sub
    End If
    
    'copy the data to the database
    ws.Cells(irow, 3).Value = Me.Textpo.Value
    ws.Cells(irow, 4).Value = Me.Textpr.Value
    ws.Cells(irow, 5).Value = Format(Me.Textdate.Value, "dd-mmm-yyyy")
    ws.Cells(irow, 6).Value = Me.ComboBox1.Value
    ws.Cells(irow, 7).Value = Me.TextSup.Value
    ws.Cells(irow, 8).Value = Me.TextPrice.Value
    ws.Cells(irow, 9).Value = Me.Textqty.Value
    ws.Cells(irow, 10).Value = Me.Textqtyrec.Value
    ws.Cells(irow, 11).Value = Format(Me.TextDue.Value, "dd-mmm-yyyy")
    ws.Cells(irow, 12).Value = Format(Me.Textdaterec.Value, "dd-mmm-yyyy")
    Unload Me

    'Clear the data
    Me.Textpo.Value = ""
    Me.Textpr.Value = ""
    Me.Textdate.Value = ""
    Me.ComboBox1.Value = ""
    Me.TextSup.Value = ""
    Me.TextPrice.Value = ""
    Me.Textqty.Value = ""
    Me.Textqtyrec.Value = ""
    Me.TextDue.Value = ""
    Me.Textdaterec.Value = ""
    MsgBox "บันทึกข้อมูลสำเร็จ"


End Sub
ขอบคุณมากค่ะ
พยายามศึกษาจากกระทู้เดิมแล้วและปรับแก้ตามแล้ว แต่ยังไม่ได้ค่ะ

Re: คำสั่ง VBA ค้นหาข้อมูลที่บันทึกไปแล้วและทำการแก้ไขข้อมูลและบันทึกการแก้ไขค่ะ

Posted: Fri Dec 14, 2018 7:38 pm
by snasui
:D ตัวอย่างการปรับ Code ครับ

Code: Select all

'Other code
If Application.CountIf(Worksheets("DataInput").Range("c:c"), Textpo.Text) > 0 Then
    irow = Application.Match((Textpo.Text), Worksheets("DataInput").Range("c:c"), 0)
    msgRepns = MsgBox("เลขที่ PO นี้มีอยู่แล้วถ้าต้องการแก้ไขข้อมูลคลิก Yes หากต้องการยกเลิก? NO", vbYesNo)
Else
    irow = ws.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0).Row
End If
'Other code
การจะคลิก ไปหน้า ย้อนกลับแล้วให้แสดงค่าในบรรทัดนั้น ๆ จะต้องหาค่าของบรรทัดปัจจุบันให้ได้ก่อน บรรทัดปัจจุบันคือค่าใน ComboBox2 ให้เอาไป Mactch จากฐานข้อมูลแล้วใช้ค่าที่ได้จากการ Match เป็นค่าตั้งต้นในการที่จะหาค่าไปหน้าหรือย้อนกลับครับ

Re: คำสั่ง VBA ค้นหาข้อมูลที่บันทึกไปแล้วและทำการแก้ไขข้อมูลและบันทึกการแก้ไขค่ะ

Posted: Fri Dec 14, 2018 10:11 pm
by Bella08
ขอบคุณมากค่ะ

Re: คำสั่ง VBA ค้นหาข้อมูลที่บันทึกไปแล้วและทำการแก้ไขข้อมูลและบันทึกการแก้ไขค่ะ

Posted: Sat Dec 15, 2018 9:27 am
by Bella08
รบกวนสอบถามเพิ่มเติมค่ะ
อาจารย์ค่ะ กำหนด nextRow ให้ตรงกับ ComboBox 2 แล้วแต่เวลาสั่งเดินหน้าหรือถอยหลังก็ยังติดปัญหาอยู่ค่ะรบกวนด้วยค่ะ

Code: Select all

Private Sub Cmdpre_Click()
nextRow = Worksheets("DataInput").Columns(3).Find(ComboBox2.Text).Row
If Cells(nextRow - 1, 2) = "ลำดับ" Then
MsgBox "Can't previous because last one"
GoTo NextStep

End If
nextRow = ActiveCell.Row - 1
Cells(nextRow, 1).Activate
    Textpo.Value = Sheets("DataInput").Cells(nextRow, 3)
    Textpr.Value = Sheets("DataInput").Cells(nextRow, 4)
    Textdate.Value = Sheets("DataInput").Cells(nextRow, 5)
    ComboBox1.Value = Sheets("DataInput").Cells(nextRow, 6)
    TextSup.Value = Sheets("DataInput").Cells(nextRow, 7)
    TextPrice.Value = Sheets("DataInput").Cells(nextRow, 8)
    Textqty.Value = Sheets("DataInput").Cells(nextRow, 9)
    Textqtyrec.Value = Sheets("DataInput").Cells(nextRow, 10)
    TextDue.Value = Sheets("DataInput").Cells(nextRow, 11)
    Textdaterec.Value = Sheets("DataInput").Cells(nextRow, 12)

NextStep:

End Sub
ขอบคุณมากค่ะ

Re: คำสั่ง VBA ค้นหาข้อมูลที่บันทึกไปแล้วและทำการแก้ไขข้อมูลและบันทึกการแก้ไขค่ะ

Posted: Sat Dec 15, 2018 9:41 am
by snasui
:D แนบไฟล์ล่าสุดที่ได้ปรับตาม Code นี้มาแล้วด้วยจะได้ดูต่อไปจากนั้นครับ

Re: คำสั่ง VBA ค้นหาข้อมูลที่บันทึกไปแล้วและทำการแก้ไขข้อมูลและบันทึกการแก้ไขค่ะ

Posted: Sat Dec 15, 2018 9:59 am
by Bella08
เรียนอาจารย์
แนบไฟล์ที่ปรับ Code ตามคำแนะนำค่ะ

ขอบคุณค่ะ

Re: คำสั่ง VBA ค้นหาข้อมูลที่บันทึกไปแล้วและทำการแก้ไขข้อมูลและบันทึกการแก้ไขค่ะ

Posted: Sat Dec 15, 2018 10:24 am
by snasui
:D ตัวอย่าง Code ที่ปุ่ม << ก่อนหน้า ครับ

Code: Select all

Private Sub Cmdpre_Click()
'nextRow = Worksheets("DataInput").Columns(3).Find(ComboBox2.Text).Row
With Worksheets("DataInput")
    nextrow = Application.Match(ComboBox2.Text, .Range("c:c"), 0) - 1
End With
If Cells(nextrow, 2) = "ลำดับ" Then
MsgBox "Can't previous because last one"
GoTo NextStep

End If
'nextRow = ActiveCell.Row - 1
'Cells(nextRow, 1).Activate
    ComboBox2.Text = Sheets("DataInput").Cells(nextrow, 3)
'Other code

Re: คำสั่ง VBA ค้นหาข้อมูลที่บันทึกไปแล้วและทำการแก้ไขข้อมูลและบันทึกการแก้ไขค่ะ

Posted: Sat Dec 15, 2018 11:08 am
by Bella08
ทำได้แล้วค่ะ
ขอบคุณมากค่ะ