Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 25-Aug-17 11:55am
v2
Comments
OriginalGriff 25-Aug-17 5:16am    
And?
What does it do that you didn't expect, or not do that you did?
What help do you need?

1 solution

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[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900