2017-08-25 92 views
0

我正在写一个函数,用于检查支付的金额是否等于或多于选中的项数。对于这个例子,有10个项目和10个复选框。我可以按任意顺序检查一个或多个盒子。如果一件或多件物品符合条件,则清除,否则它将保留。如何循环使用提交按钮的多个条件?

Public Sub processItem1() 

Dim db As DAO.Database 
Dim pr As DAO.Recordset, so As DAO.Recordset 
Dim strSQL1 As String 
Dim strSQL2 As String 

Set db = CurrentDb 

    strSQL1 = "SELECT * FROM PharmSales WHERE PharmSalesID= (SELECT MAX(PharmSalesID) FROM PharmSales WHERE HospitalNo='" & Me.txtRegNo & "' And TDate = #" & Format(Me.txtTDate, "M\/dd\/yyyy") & "# AND SalesItem1 = '" & Me.txtSalesItem1 & "')" 
    strSQL2 = "SELECT * FROM tblItem WHERE ItemName = '" & Me.txtSalesItem1 & "'" 
Set pr = db.OpenRecordset(strSQL1) 
Set so = db.OpenRecordset(strSQL2) 

With pr 
If Not .BOF And Not .EOF Then 'Ensure that the recordset contains records 
.MoveLast 
.MoveFirst 
If .Updatable Then 'To ensure record is not locked by another user 
.Edit 'Must start an update with the edit statement 
![DispQty1] = Nz(![DispQty1] + Me.txtSalesQty1.Value, 0) 
    .Update 
End If 
End If 

pr.Close 'Make sure you close the recordset.. 
Set pr = Nothing '...and set it to nothing 
Set db = Nothing 
End With 

With so 
If Not .BOF And Not .EOF Then 'Ensure that the recordset contains records 
.MoveLast 
.MoveFirst 
If .Updatable Then 'To ensure record is not locked by another user 
.Edit 'Must start an update with the edit statement 
![Stock_Out] = Nz(![Stock_Out] + Me.txtSalesQty1.Value, Me.txtSalesQty1.Value) 
![SO_Date] = Me.txtTDate 
    ![Stock_In] = Nz(![Stock_In] + 0, 0) 
    .Update 'And finally we will need to confirm the update 

End If 
End If 
so.Close 'Make sure you close the recordset.. 
ExitSub: 
Set so = Nothing '...and set it to nothing 
Set db = Nothing 

    End With 
    End Sub 

对于processItem2:

Public Sub processItem2() 

Dim db As DAO.Database 
Dim pr As DAO.Recordset, so As DAO.Recordset 
Dim strSQL1 As String 
Dim strSQL2 As String 

Set db = CurrentDb 

    strSQL1 = "SELECT * FROM PharmSales WHERE PharmSalesID= (SELECT MAX(PharmSalesID) FROM PharmSales WHERE HospitalNo='" & Me.txtRegNo & "' And TDate = #" & Format(Me.txtTDate, "M\/dd\/yyyy") & "# AND SalesItem2 = '" & Me.txtSalesItem2 & "')" 
    strSQL2 = "SELECT * FROM tblItem WHERE ItemName = '" & Me.txtSalesItem2 & "'" 
Set pr = db.OpenRecordset(strSQL1) 
Set so = db.OpenRecordset(strSQL2) 

With pr 
If Not .BOF And Not .EOF Then 'Ensure that the recordset contains records 
.MoveLast 
.MoveFirst 
If .Updatable Then 'To ensure record is not locked by another user 
.Edit 'Must start an update with the edit statement 
![DispQty2] = Nz(![DispQty2] + Me.txtSalesQty2.Value, 0) 
    .Update 
End If 
End If 

pr.Close 'Make sure you close the recordset.. 
Set pr = Nothing '...and set it to nothing 
Set db = Nothing 
End With 

With so 
If Not .BOF And Not .EOF Then 'Ensure that the recordset contains records 
.MoveLast 
.MoveFirst 
If .Updatable Then 'To ensure record is not locked by another user 
.Edit 'Must start an update with the edit statement 
![Stock_Out] = Nz(![Stock_Out] + Me.txtSalesQty2.Value, Me.txtSalesQty2.Value) 
![SO_Date] = Me.txtTDate 
    ![Stock_In] = Nz(![Stock_In] + 0, 0) 
    .Update 'And finally we will need to confirm the update 
End If 
End If 
so.Close 'Make sure you close the recordset.. 
ExitSub: 
Set so = Nothing '...and set it to nothing 
Set db = Nothing 

End With 
End Sub 

image

回答

1

永远不要复制粘贴&这样的代码。这是一个维护噩梦。

你也可以遍历对象在运行时串联他们的名字:

Me("txtSalesItem" & i)  ' form control 
pr("DispQty" & i).Value  ' recordset field 

旁注:

With recordset 
    .MoveLast 
    .MoveFirst 

这些MoveLast /命令的MoveFirst是不必要的。如果您想获取记录集的正确.RecordCount,则只需要它们。

+0

此外,据我所知,“Updatable”指的是_Recordset_,而不是实际的记录。 – Gustav

+0

谢谢安德烈对这些意见。我希望你能够更清楚地知道在何处以及如何插入(“txtSalesItem”&i)基于提供的示例 –

+0

@LibertyCrownInfotech:请尝试一下。无论你有一个像txtSalesItem1或DispQty1这样的编号字段/控件名称,你都可以这样做。另一个注意事项:通过将编号字段转换为单独的表“Today_Sales”,将整个事情变得更简单,并将其更改为子表单。那么你可以简单地循环它的记录,而不用任何动态名字构造 – Andre