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
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.
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 & "')"
AMTSelect is a declared variable variant type for the Getrows array
rCount is an integer
I'm trying to pull the first field value from each row but I keep getting an error saying that the subscript is out of range. The error happens in the for loop.
Code is below:
If Contractnum <> "" Then
CNTRecords = "Select Count(*) from [Manual_AINs] WHERE [Manual_AINs].[Contract_Number]= '" & Contractnum & "';"
Set rs = CurrentDb.OpenRecordset(CNTRecords)
rCount = rs.Fields(0)
Set rs = Nothing
If rCount > 1 Then
qAMT = "Select [Dollar Amount] from [Manual_AINs] WHERE ((([Manual_AINs].[Contract_Number])='" & Contractnum & "'));"
Set rs = CurrentDb.OpenRecordset(qAMT)
AMTSelect = rs.GetRows
AMTSelectString = "Choose appropriate dollar amount of AIN from the selection below:" & Chr(10) & Chr(10)
For i = 1 To rCount
AMTSelectString = AMTSelectString & i & ".) " & Format(AMTSelect(0, (i - 1)), "$#,##0.00") & Chr(10)
Next i
You aren't requesting any rows with .GetRows - this is basically just calling .GetRows(0), which won't return any rows into your Recordset.
Change this line...
AMTSelect = rs.GetRows
...to:
AMTSelect = rs.GetRows(rCount)
That said, since you are apparently trying to use every line in the Recordset, this is much simpler:
If Contractnum <> "" Then
qAMT = "Select [Dollar_Amount] from [Manual_AINs] WHERE [Manual_AINs].[Contract_Number]='" & Contractnum & "';"
With CurrentDb.OpenRecordset(qAMT)
If Not .EOF Then .MoveFirst
AMTSelectString = "Choose appropriate dollar amount of AIN from the selection below:" & Chr(10) & Chr(10)
Dim i As Long
Do While Not .EOF
i = i + 1
AMTSelectString = AMTSelectString & i & ".) " & Format$(.Fields(0), "$#,##0.00") & Chr(10)
.MoveNext
Loop
End With
End If
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
rs2.FindFirst "[aniin] ='" & strTemp & "'"
aniin being an alias from the SQL within the function.
also tried ...
rs2.FindFirst (niin = newdata)
is my attempt to isolate the field name niin from the record value in the form from the one in the strSQL2. All my attempts have failed. I am trying to make sure that what the user typed in does match the list from the SQL string.
Private Function IsPartOfAEL(newdata) As Boolean
On Error GoTo ErrTrap
Dim db2 As DAO.Database
Dim rs2 As DAO.Recordset
Dim strTemp As String
strSQL2 = "SELECT tbl_ael_parts.master_ael_id, tbl_master_niin.niin as aniin " & vbCrLf & _
"FROM tbl_master_niin INNER JOIN tbl_ael_parts ON tbl_master_niin.master_niin_id = tbl_ael_parts.master_niin_id " & vbCrLf & _
"WHERE (((tbl_ael_parts.master_ael_id)= " & Forms!frm_qry_niin_local!master_ael_id & "));"
Set db2 = CurrentDb
Set rs2 = db2.OpenRecordset(strSQL2)
strTemp = newdata
If rs2.RecordCount <> 0 Then
rs2.FindFirst "[aniin] ='" & strTemp & "'"
If rs2.NoMatch Then
IsPartOfAEL = False
Else
IsPartOfAEL = True
End If
Else
MsgBox "Query Returned Zero Records", vbCritical
Exit Function
End If
rs.Close
Set rs2 = Nothing
Set db2 = Nothing
ExitHere:
Exit Function
ErrTrap:
MsgBox Err.description
Resume ExitHere
End Function
First: You should never include a constant like vbCrLf when building a query string. The query parser doesn't care if there's a linefeed, and in fact this can sometimes cause issues.
Your code seems to do nothing more that verify whether the value in newdata exists in the tbl_ael_parts and is associated with the value master_ael_id value currently showing on frm_qry_niin_local. If so, then just use DCount, or use this for your query:
strSQL2 = "SELECT tbl_ael_parts.master_ael_id INNER JOIN tbl_ael_parts ON
tbl_master_niin.master_niin_id = tbl_ael_parts.master_niin_id WHERE (((tbl_ael_parts.master_ael_id)=
" & Forms!frm_qry_niin_local!master_ael_id & ") AND niin=" & newdata & ");"
Dim rst As DAO.Recordset
Set rst = currentdb.OPenrecordset(strsql2)
If (rst.EOF and rst.BOF) Then
' no records returned
Else
' records found
End If
If niin is a Text field:
strSQL2 = "SELECT tbl_ael_parts.master_ael_id INNER JOIN tbl_ael_parts ON
tbl_master_niin.master_niin_id = tbl_ael_parts.master_niin_id WHERE (((tbl_ael_parts.master_ael_id)=
" & Forms!frm_qry_niin_local!master_ael_id & ") AND (niin='" & newdata & "'));"
If both niin and master_ael_id are Text fields:
strSQL2 = "SELECT tbl_ael_parts.master_ael_id INNER JOIN tbl_ael_parts ON
tbl_master_niin.master_niin_id = tbl_ael_parts.master_niin_id WHERE (((tbl_ael_parts.master_ael_id)=
'" & Forms!frm_qry_niin_local!master_ael_id & "') AND (niin='" & newdata & "'));"