学编程以来第一次做出了点能看的东西o(╥﹏╥)o,虽然是用VBA写的——门店销售工具

先上效果图:

学编程以来第一次做出了点能看的东西o(╥﹏╥)o,虽然是用VBA写的——门店销售工具_第1张图片

废话不多说,上代码! 

Dim arr()
Dim ID As String
Dim DJ As Long

Private Sub CommandButton1_Click()

If Me.ListBox1.Value <> "" And Me.ListBox2.Value <> "" And Me.ListBox3.Value <> "" And Me.TextBox1.Value > 0 Then
    Me.ListBox4.AddItem
    Me.ListBox4.List(Me.ListBox4.ListCount - 1, 0) = ID
    Me.ListBox4.List(Me.ListBox4.ListCount - 1, 1) = Me.ListBox1.Value
    Me.ListBox4.List(Me.ListBox4.ListCount - 1, 2) = Me.ListBox2.Value
    Me.ListBox4.List(Me.ListBox4.ListCount - 1, 3) = Me.ListBox3.Value
    Me.ListBox4.List(Me.ListBox4.ListCount - 1, 4) = Me.TextBox1.Value
    Me.ListBox4.List(Me.ListBox4.ListCount - 1, 5) = DJ * Me.TextBox2.Value
    Me.Label4.Caption = Me.Label4.Caption + DJ * Me.TextBox2.Value
    Me.Label4.Visible = True
Else:
    MsgBox "商品还未选择"
End If

End Sub

Private Sub CommandButton2_Click()

If Me.ListBox4.Value <> "" Then
Me.Label4.Visible = True
End If

For i = 0 To Me.ListBox4.ListCount - 1
    If Me.ListBox4.Selected(i) Then
        Me.Label4.Caption = Me.Label4.Caption - Me.ListBox4.List(i, 5)
        Me.ListBox4.RemoveItem (i)
    End If
Next

End Sub

Private Sub CommandButton3_Click()

Dim DDID As String
Dim conn As New ADODB.Connection
Dim str As String

If Me.ListBox4.ListCount > 0 Then
    DDID = "D" & Format(VBA.Now, "yyyymmddhhmmss")
    conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\data\data.accdb"

    For i = 0 To Me.ListBox4.ListCount - 1
        str = " ('" & DDID & "','" & Date & "','" & Me.ListBox4.List(i, 0) & "'," & Me.ListBox4.List(i, 4) & "," & Me.ListBox4.List(i, 5) & ")"
        conn.Execute ("insert into [销售记录] (订单号,日期,产品编号,数量,金额) values" & str)
    Next

    conn.Close

MsgBox "结算成功"
Unload Me

Else:
    MsgBox "购物清单为空"

End If

End Sub

Private Sub ListBox1_Click()

Me.ListBox2.Clear

Dim dic
Set dic = CreateObject("Scripting.Dictionary")

For i = LBound(arr) To UBound(arr)
    If arr(i, 2) = Me.ListBox1.Value Then
        dic(arr(i, 3)) = 1
    End If
Next

Me.ListBox2.List = dic.keys
Me.ListBox3.Clear
Me.TextBox2.Value = ""

End Sub

Private Sub ListBox2_Click()

Me.ListBox3.Clear

Dim dic
Set dic = CreateObject("Scripting.Dictionary")

For i = LBound(arr) To UBound(arr)
    If arr(i, 2) = Me.ListBox1.Value And arr(i, 3) = Me.ListBox2.Value Then
        dic(arr(i, 4)) = 1
    End If
Next

Me.ListBox3.List = dic.keys
Me.TextBox2.Value = ""

End Sub

Private Sub ListBox3_Click()

Dim dic
Set dic = CreateObject("Scripting.Dictionary")

For i = LBound(arr) To UBound(arr)
    If arr(i, 2) = Me.ListBox1.Value And arr(i, 3) = Me.ListBox2.Value And arr(i, 4) = Me.ListBox3.Value Then
        ID = arr(i, 1)
        DJ = arr(i, 5)
    End If
Next

Me.TextBox2.Value = DJ

End Sub

Private Sub UserForm_Activate()

Dim dic

arr = Sheet1.Range("b2:f" & Sheet1.Range("a65536").End(xlUp).Row)

Set dic = CreateObject("Scripting.Dictionary")


For i = LBound(arr) To UBound(arr)
    dic(arr(i, 2)) = 1
Next

Me.ListBox1.List = dic.keys

End Sub

 

你可能感兴趣的:(学编程以来第一次做出了点能看的东西o(╥﹏╥)o,虽然是用VBA写的——门店销售工具)