I'm populating a TempTable in Access using data from the queries. I have populated 3 columns (Add, Delete, Buy) so far and I'm trying to find the total of the three of them. Thing is, a lot of don't have values, and some of them are negative values. How would I go about addnig them? I have data in rs2("Add"), rs2("Delete"), and rs2("Buy"). So all ADD values (if there are any are positive), all DELETE are negative (if any) and all BUY are positive.
I'd like to go in a loop possibly...
I took the count of records as intRsCount = 25
With rs2
While Not .EOF
lngCompanyID = rs2("CompanyID")
lngUnitPrice = rs2("UnitPrice")
strSQL2 = "SELECT AddQty from qryAddx WHERE InvValue = " & lngUnitPrice & " And CompanyId = " & lngCompanyId
strSQL3 = "SELECT DeleteQty from qryUsedx where UnitValue=" & lngUnitPrice & " And CompanyId = " & lngCompanyId
strSQL4 = "SELECT BuyQty from qryVoidx where InvValue =" & lngUnitPrice & " And CompanyId = " & lngCompanyId
Set rs3 = CurrentDb.OpenRecordset(strSQL2)
Set rs4 = CurrentDb.OpenRecordset(strSQL3)
Set rs5 = CurrentDb.OpenRecordset(strSQL4)
If rs3.recordCount > 0 Then
rs2.Edit
rs2("Added") = rs3("Addqty")
rs2.Update
End If
If rs4.recordCount > 0 Then
rs2.Edit
rs2("Delete") = rs4("DelQty")
rs2.Update
End If
If rs5.recordCount > 0 Then
rs2.Edit
rs2("Buy") = rs5("BuyQty")
rs2.Update
End If
rs2.MoveNext
Set rs3 = Nothing
Set rs4 = Nothing
Set rs5 = Nothing
Wend
End With
This is my code. I'm trying to add the SUM of ADDED, DELETED and BUY and place it in rs2("Subtotal") and doing this for all 25 rows. I'm not really familiar with ACCESS and how it deals with NULL values, and negative values (all DELETE values are negative). Thanks.
Dim strCriteria as String
With rs2
While Not .EOF
strCriteria = "InvValue = " & !UnitPrice & " And CompanyId = " & !CompanyID
.Edit
!Added = Nz(DSum("Addqty", "qryAddx", strCriteria), 0)
!Delete = Nz(DSum("DeleteQty", "qryUsedx", strCriteria), 0)
!Buy = Nz(DSum("BuyQty", "qryVoidx", strCriteria), 0)
.Update
Wend
End With
DSum() is inefficient. If you are running many operations, it may be slow, but otherwise it is fine.
Nz() converts a Null value to zero.
If rs2 is a recordset, you would refer to the columns like this: rs2.fields("Add").Value
Do loop through the rows, go with
if rs2.recordcount > 0 then
rs2.movefirst
while not rs2.eof
'put your commands here
wend
end if
Related
I am trying to go through my forms checkboxes and If True, I need to check to see if the record exists and then, If it exists and True - Do Nothing. If True and Doesn't exist - Add the record.
If False - I also need to check if it exists and If it exists - Delete it, and If it doesn't - Do nothing.
I have tried using just recordset and looping through the table.
I also tried using DLookup and just got lost with needing three criteria values to find the record.
Now I am trying to use both recordset and SQL and keep getting the error "too few parameters".
RT = "Rise Time"
If Me.RiseTime.Value = True Then
strSQL = "SELECT * FROM Weekly_StartTime_Challenges WHERE UserID = '" &
Me.UserID.Value & "' AND WeekNumber = '" & Me.WeekNumber.Value
& "' AND StartTimeAction =" & RT
Set sast = db.OpenRecordset(strSQL, dbOpenDynaset)
If Not sast.EOF And sast.BOF Then
' It Does Exist and Do Nothing
Else
sast.AddNew
sast!WeekNumber = Me.WeekNumber.Value
sast!StartDate = Me.StartDate.Value
sast!UserID = Me.UserID.Value
sast!FullName = Me.FullName.Value
sast!Index = 1
sast!Tab1 = 8
sast!StartTimeAction = RT
sast.Update
End If
Else
strSQL = "SELECT * FROM Weekly_StartTime_Challenges WHERE UserID = '" &
Me.UserID.Value & "' AND WeekNumber = '" & Me.WeekNumber.Value & "' AND
StartTimeAction = RT"
Set sast = db.OpenRecordset(strSQL, dbOpenDynaset)
If Not sast.EOF And sast.BOF Then
' It Does Exist and needs deleted
sast.Delete
Else
End If
End If
Do it step by step. For example if you want to check if a record exists:
If DCount("ColumnName", "TableName", "ID = 4") = 0 Then
MsgBox "No Record Found"
'Do stuf
Else
MsgBox "Record/Records found"
'Do other stuf
End If
If you find a record/s you can loop through it with a recordset:
Dim rs As RecordSet
Set rs = CurrentDb.OpenRecordset("Select * From Table", dbOpenDynaset)
rs.MoveFirst
Do Until rs.EOF
If "You want to Update the record" = True Then 'Apply your update condition
rs.Edit
rs.ID = 4 'Change ID
rs.Name = "New Name" 'Change Name
rs.Update
Else
'Do other stuf
End If
rs.MoveNext
Loop
rs.close
Set rs = Nothing
For changing a datarow you need always recordset rs.Edit and in the end rs.Update.
I'm trying to set command buttons to enter data into a table. If the record already exists I want the button to update it, and if it does not the button needs to create it. Here's a sample of what the table looks like
ID scenarioID reductionID Impact Variable Variable Impact
1 1 1 Safety 4
2 1 1 Environmental 2
3 1 1 Financial 1
In order to accurately locate records, it needs to search for the specific impact variable paired with the scenarioID. I'm trying to use a select statement, but DoCmd.RunSQL doesn't work for select statements, and I'm not sure how else to do it.
Here's the code. I left DoCmd.SQL in front of the select statement for lack of anything else to place there for now.
Private Sub Var1R1_Click() 'Stores appropriate values in tImpact upon click
'Declaring database and setting recordset
Dim db As Database
Dim rs As Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("tImpact")
'Declaring Variable as Scenario Choice combobox value
Dim Sc As Integer
Sc = Schoice.Value
'Stores impact variable
Dim impvar1 As String
'Desired impact variable for column 1
impvar1 = DLookup("impactVariable", "tImpactVars", "ID = 1")
DoCmd.RunSQL "SELECT * FROM tImpact WHERE [Impact Variable] = " & impvar1 & " AND scenarioID = " & Sc
If rs.EOF Then
DoCmd.RunSQL "INSERT INTO tImpact(scenarioID, [Impact Variable], [Variable Impact])" & "VALUES (" & Sc & ", " & impvar1 & ", 1)"
MsgBox "Record Added", vbOKOnly
Else
db.Execute "UPDATE tImpact SET [Variable Impact] = 1 WHERE [Impact Variable] = " & impvar1 & " AND scenarioID = " & Sc
MsgBox "Record Updated", vbOKOnly
End If
End Sub
If anyone can tell me how to get that SELECT statement to run, or another way of doing this, that would be great.
Any help is greatly appreciated!
You can use a recordset. In this case a recordset is better, since you only execute the SQL one time, if it returns a record, you "edit" and if not then you "add" with the SAME reocrdset. This approach is FAR less code, and the values you set into the reocrdset does not require messy quotes or delimiters etc.
eg:
scenaridID = 1 ' set this to any number
impvar1 = "Safety" ' set this to any string
updateTo = "Financial"
strSQL = "select * from tImpact where [Impact Variable] = '" & impvar1 & "'" & _
" AND scenaridID = " & scenaridID
Set rst = CurrentDb.OpenRecordset(strSQL)
With rst
If .RecordCount = 0 Then
' add the reocrd
.AddNew
Else
.Edit
End If
!scenaridID = scenarid
![Impact Variable] = impvar1
![Variable Impact] = 1
.Update
End With
rst.Close
So you can use the same code for the update and the edit. It just a question if you add or edit.
Use OpenRecordset and retrieve the ID for the record if it exists.
Private Sub Command0_Click()
Dim aLookup(1 To 3) As String
Dim aAction(1 To 3) As String
Dim rs As Recordset
Dim db As Database
'Replace these two constants with where you get the information on your form
Const sIMPVAR As String = "Financial"
Const lSCENID As Long = 1
'Build the SQL to find the ID if it exists
aLookup(1) = "SELECT ID FROM tImpact"
aLookup(2) = "WHERE ScenarioID = " & lSCENID
aLookup(3) = "AND ImpactVariable = """ & sIMPVAR & """"
'Run the sql to find the id
Set db = CurrentDb
Set rs = db.OpenRecordset(Join(aLookup, Space(1)))
If rs.BOF And rs.EOF Then 'it doesn't exist, so build the insert statement
aAction(1) = "INSERT INTO tImpact"
aAction(2) = "(ScenarioID, ImpactVariable, VariableImpact)"
aAction(3) = "VALUES (" & lSCENID & ", '" & sIMPVAR & "', 1)"
Else 'it does exist, so build the update statement
aAction(1) = "UPDATE tImpact"
aAction(2) = "SET VariableImpact = 1"
aAction(3) = "WHERE ID = " & rs.Fields(0).Value
End If
'Run the action query
db.Execute Join(aAction, Space(1))
rs.Close
Set rs = Nothing
Set db = Nothing
End Sub
This query does not run at the beginning. Could someone please help look at what is wrong?
If there is any other way to achieve this kindly suggest.
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
Your SQL checks multiple criteria, but your subquery doesn't have any of these criteria, so it will probably select a record that doesn't conform to your other criteria, causing your recordset to always be empty.
You need to add these criteria to the subquery, not the main query.
Since the subquery will just return one record, you don't have to add them to both.
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 & "')"
I'm trying to populated a Temp Table with some data from my queries. The problem I came across is trying to SELECT certain data using 2 WHERE criteria. As long as I have one, it pulls up the correct data and populates it as such. However, when I try to filter by two criteria (which I need to do in order to pupulate info in correct rows), it gives me an error "TYPE MISMATCH". What happens is, after my first criteria WHERE = " & lngCompanyID - There is no quote at the end of it, however, when I add another criteria... WHERE CompanyID = " & lngCompanyID and UnitPrice = " & lngUnitPrice " <--- there is a double quote at end of it and I think it's stopping it from passing the value. This is what the code looks like
With rs2
While Not .EOF
rs2.Edit
lngCompanyID = rs2("CompanyID")
lngUnitPrice = rs2("UnitPrice")
strSQL2 = "SELECT SumOfInvQty from qryAddx WHERE InvValue = " & lngUnitPrice And CompanyId = " & lngCompanyID"
Set rs3 = CurrentDb.OpenRecordset(strSQL2)
If rs3.recordCount > 0 Then
rs2("Added") = rs3("sumofinvqty")
rs2.Update
End If
rs2.MoveNext
Set rs3 = Nothing
Wend
End With
You're not continuing you're concatenation of the string correctly. Try this instead:
With rs2
While Not .EOF
rs2.Edit
lngCompanyID = rs2("CompanyID")
lngUnitPrice = rs2("UnitPrice")
strSQL2 = "SELECT SumOfInvQty from qryAddx WHERE InvValue = " & lngUnitPrice & " And CompanyId = " & lngCompanyID
Set rs3 = CurrentDb.OpenRecordset(strSQL2)
If rs3.recordCount > 0 Then
rs2("Added") = rs3("sumofinvqty")
rs2.Update
End If
rs2.MoveNext
Set rs3 = Nothing
Wend
End With
I'm trying to look up some values with my dlookup function in Access. I'm going into queries and pulling data with 2 different criteria. So I pull data from a query then I'm inserting it into a temp table. I'm having a hard time avoiding NULL values.
With rs2
While Not .EOF
lngVendorID = rs2("CompanyID")
lngUnitPrice = rs2("UnitPrice")
'Beginning Count
lngBegCount = (DLookup("BegCount", "qryBegInv", "UnitPrice = " & [lngUnitPrice] & " AND CompanyID = " & [lngCompanyID] & ""))
If IsNull(lngBegCount) Or lngBegCount = "" Then
lngBegCount = 0
End If
.Edit
rs2("BegInvCount") = lngBegCount
.Update
rs2.MoveNext
Wend
I keep getting a variety of errors. Basically I want to see if DLOOKUP value is null, if it is, then use a 0 and insert that into the rs2("BegInvCount"), if it isn't null, then insert the lngBegCount into rs2("BegInvCount").
Use the Nz() function to handle NULL values:
lngBegCount = Nz(DLookup("BegCount", "qryBegInv", _
"UnitPrice = " & lngUnitPrice & " AND CompanyID = " & lngCompanyID & ""), 0)
You don't need brackets around variables.