且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

添加值多列列表框在Access 07 VBA

更新时间:2022-06-17 00:44:29

如果我正确地理解您的需求,您将需要一个文本框添加到您的形式,这样用户可以指定数量时,他们选择一个项目。

If I understand your requirements correctly, you will need to add a textbox to your form so the user can specify a quantity when they select an item.

[假设]

- >表的数据:我的[tblSampleData]看起来是这样的:

--> Table data: My [tblSampleData] looks like this:

ID  Item    Rate    QtyAvailable
1   Item1   Rate1   3
2   Item2   Rate2   5

- >组合框:在绑定列组合框是 1 ,(隐藏)第一列,即[ID]。

--> Combo box: The Bound Column of the combo box is 1, the (hidden) first column, which is [ID].

- >数据绑定:组合框有一个行来源(tblSampleData),但没有控制源 。列表框是完全绑定。

--> Data bindings: The combo box has a Row Source (tblSampleData), but no Control Source. The list box is completely unbound.

如果您的情况是不同的,他们就需要调整样本code,以适应。

If your situation is different they you'll need to tweak the sample code to suit.

[/假设]

我创建了一个测试的形式,看起来像这样第一次打开时:

I created a test form that looks like this when it is first opened:

添加值多列列表框在Access 07 VBA

形式背后的VBA code是这样的:

The VBA code behind the form is this:

Option Compare Database
Option Explicit

Private Sub btnTransfer_Click()
Dim cdb As DAO.Database, rst As DAO.Recordset, qtySelected As Long
Set cdb = CurrentDb
Set rst = cdb.OpenRecordset("SELECT * FROM tblSampleData WHERE ID=" & Me.cbxItems.Value, dbOpenSnapshot)
qtySelected = Val(Nz(Me.txtQty.Value, 0))
If qtySelected <= 0 Then
    MsgBox "Please specify a (positive) quantity.", vbExclamation
Else
    If qtySelected <= rst!QtyAvailable Then
        Me.lstSelected.AddItem rst!ID & ";" & rst!Item & ";" & rst!Rate & ";" & qtySelected
    Else
        MsgBox "Quantity selected exceeds quantity available.", vbExclamation
    End If
End If
rst.Close
Set rst = Nothing
Set cdb = Nothing
End Sub

Private Sub Form_Load()
Do While Me.lstSelected.ListCount > 0
    Me.lstSelected.RemoveItem 0
Loop
Me.lstSelected.AddItem ";Item;Rate;QtySelected"
End Sub

用户从组合框中选择项目...

The user selects the item from the combo box...

添加值多列列表框在Access 07 VBA

...输入在文本框中的数量...

...enters the quantity in the text box...

添加值多列列表框在Access 07 VBA

...,然后点击传送按钮,将项目+选择的数量移动到列表框中:

...and then clicks the "Transfer" button to move the item + selected quantity into the list box:

添加值多列列表框在Access 07 VBA