Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This query does not run at the beginning. Could someone please help look at what is wrong?
This query does not run at the beginning. Could someone please help look at what is wrong?

What I have tried:

<pre>    strSQL1 = "SELECT * FROM PharmSales WHERE HospitalNo='" & Me.txtRegNo & "' And TDate = #" & Format(Me.txtTDate, "M\/dd\/yyyy") & "# AND SalesItem1 = '" & Me.txtSalesItem1 & "' And PharmSalesID=  (SELECT MAX(PharmSalesID) FROM PharmSales)"
            
        Set pr = db.OpenRecordset(strSQL1)
        
        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
         If IsNull(![TotalPaid]) = True And Me.txtGrand_TotalPay.Value >= Me.txtSalesAmt1.Value Then
         ![DispQty1] = Nz(![DispQty1] + Me.txtSalesQty1.Value, 0)
          .Update
               ElseIf IsNull(![TotalPaid]) = False And (Me.txtGrand_TotalPay.Value - Me.txtSalesAmt1.Value) >= (txtGrand_TotalFee - Me.txtGrand_TotalPay.Value + Me.txtSalesAmt1.Value) Then
         ![DispQty1] = Nz(![DispQty1] + Me.txtSalesQty1.Value, 0)
          .Update
        
         Else: MsgBox ("Insufficient balance!")
         End If
        End If
        End If
    
         pr.Close 
         Set pr = Nothing 
        Set db = Nothing
        End With
    
    End Sub
Posted
Updated 21-Aug-17 5:42am
Comments

1 solution

For starters, don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

Secondly, the default ToString implementation for a TextBox always returns a string like this: "System.Windows.Forms.TextBox, Text: ..." - it does not return the Text property.
So your WHERE clauses are unlikely to match anything at all, so no rows will be returned.
 
Share this answer
 
Comments
Liberty Crown Infotech 24-Aug-17 13:24pm    
Thank you for your observation. Kindly show some samples on how to parameterize a query

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