พะเนงกอง

* กระทู้นี้สามารถใช้งานได้เฉพาะผู้ที่มี Link นี้เท่านั้นค่ะ
กระทู้ข่าว
Option Explicit

Dim currentRow As Long

Private Sub UserForm_Initialize()
    currentRow = 2 ' เริ่มที่แถวแรกของข้อมูล
    ShowRecord currentRow
End Sub

Private Sub ShowRecord(ByVal r As Long)
    With Worksheets("Stock")
        Me.txtPartNo.Value = .Cells(r, 1).Value
        Me.txtPartName.Value = .Cells(r, 2).Value
        Me.txtBalance.Value = .Cells(r, 3).Value
        Me.txtLocation.Value = .Cells(r, 4).Value
    End With
End Sub

' ปุ่มเลื่อนไป Record ก่อนหน้า
Private Sub cmdPrev_Click()
    If currentRow > 2 Then
        currentRow = currentRow - 1
        ShowRecord currentRow
    End If
End Sub

' ปุ่มเลื่อนไป Record ถัดไป
Private Sub cmdNext_Click()
    If Worksheets("Stock").Cells(currentRow + 1, 1).Value <> "" Then
        currentRow = currentRow + 1
        ShowRecord currentRow
    End If
End Sub

' ปุ่มใช้ของ (Use)
Private Sub cmdUse_Click()
    Dim qty As Long
    qty = CLng(Me.txtQty.Value)
    
    If qty > 0 Then
        If CLng(Me.txtBalance.Value) >= qty Then
            Me.txtBalance.Value = CLng(Me.txtBalance.Value) - qty
            Worksheets("Stock").Cells(currentRow, 3).Value = Me.txtBalance.Value
            MsgBox "ใช้ของออก " & qty & " ชิ้น เรียบร้อย", vbInformation
        Else
            MsgBox "สต็อกไม่พอ!", vbExclamation
        End If
    End If
End Sub

' ปุ่มซื้อของเข้า (Buy)
Private Sub cmdBuy_Click()
    Dim qty As Long
    qty = CLng(Me.txtQty.Value)
    
    If qty > 0 Then
        Me.txtBalance.Value = CLng(Me.txtBalance.Value) + qty
        Worksheets("Stock").Cells(currentRow, 3).Value = Me.txtBalance.Value
        MsgBox "เพิ่มของเข้า " & qty & " ชิ้น เรียบร้อย", vbInformation
    End If
End Sub

' ปุ่มออกจากโปรแกรม
Private Sub cmdExit_Click()
    Unload Me
End Sub
แสดงความคิดเห็น
โปรดศึกษาและยอมรับนโยบายข้อมูลส่วนบุคคลก่อนเริ่มใช้งาน อ่านเพิ่มเติมได้ที่นี่