2017-08-25 51 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