如何使用多个条件循环提交按钮? [英] How do I loop using multiple conditions for a submit button?

查看:82
本文介绍了如何使用多个条件循环提交按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个函数,用于检查付款金额是否等于或等于选中的多个项目(如果已选中)。对于此示例,有10个项目,其中包含10个复选框。我可以检查任何其他的一个或多个方框。如果一个或多个项目满足条件,它将被清除,否则它仍然存在。



我尝试过:



I am writing a function that checks if the amount paid is equal or more to a number of items selected if they are checked. For this example there are 10 items with 10 check boxes. I could check one or more boxes in any other.If an item or more meets the condition it is cleared otherwise it remains.

What I have tried:

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





第二项:



Second Item:

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查询。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。

名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

按示例进行SQL注入攻击 [ ^ ]

SQL注射预防备忘单 - OWASP [ ^ ]
Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]


这篇关于如何使用多个条件循环提交按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆