【问题标题】:How do I loop using multiple conditions for a submit button?如何循环使用提交按钮的多个条件?
【发布时间】:2017-08-25 08:51:39
【问题描述】:

我正在编写一个函数,用于检查支付的金额是否等于或大于选中的项目数量。对于此示例,有 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

【问题讨论】:

    标签: sql ms-access dao


    【解决方案1】:

    永远不要像这样复制和粘贴代码。这是维护的噩梦。

    您可以通过在运行时连接它们的名称来循环对象:

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

    等等

    旁注:

    With recordset
        .MoveLast
        .MoveFirst
    

    这些 MoveLast/MoveFirst 命令是不必要的。仅当您想获得记录集的正确 .RecordCount 时才需要它们。

    【讨论】:

    • 另外,据我所知,Updatable 指的是 记录集,而不是实际记录。
    • 感谢安德烈的这些观察。我希望您根据提供的示例更明确地说明在何处以及如何插入 ("txtSalesItem" & i)
    • @LibertyCrownInfotech:请自行尝试。无论您在哪里有像txtSalesItem1DispQty1 这样的编号字段/控件名称,都可以执行此操作。另一个注意事项:通过将编号字段转换为单独的表“Today_Sales”并将其更改为子表单,整个事情会变得更加简单。然后你可以简单地循环它的记录,而不需要任何动态名称构造。
    • @Andre。 Today_Sales 实际上是一组动态显示另一个表的结果的控件。
    猜你喜欢
    • 1970-01-01
    • 2021-08-11
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多