Hallo Marsh,
Thanks for the reply. When I saw how short your sql string was compared to
mine, I became determined to improve mine as well until I went back to
re-read my notes on variables and quotes. I am new to quotes and literals
surrounding variables and I will manage over time. I managed to work around
them by standardizing my variables to variant types, working with one table
as a subform (Me) linked to unbound form (frmCashSales).
The problem I have now is that when I have more than one record in the
subform with different stockids (say 10 records with cigarettes, matches,
cooking oil etc) which have different prices, the function returns the
unitprice for the record on focus (ie unitprice has focus) for all records.
Furthermore, the price is applied to all stockids (cigarette price Tsh 8,000,
cooking oil 8,000 etc). When you move to another record, the unit price
changes as well to that stockid on focus, the whole form.
It's 5:30 right and I will go home soon but what I will try tomorrow is to
move the code to stockid after_update event and update the value and not the
function, like copying and pasting special values in excel.
What do you think?
Rgds,
atu
Private Function GetUnitPrice() As Double
'On Error GoTo Err_GetUnitPrice
'Establish connection to the SalesPriceList table
'recordset and connection variables
Dim Cnxn As ADODB.Connection
Dim rstSalesPriceList As ADODB.Recordset
Dim strCnxn As String
Dim strSQL As String
Dim strStockID As Variant
Dim dteTransactionDate As Variant
Dim varResult As Variant
strStockID = CVar(Me.txtStockID)
dteTransactionDate = CVar(Forms!frmCashSales.txtTransactionDate)
' Open a connection
Set Cnxn = New ADODB.Connection
strCnxn = "Provider='Microsoft.Jet.OLEDB.4.0';Data Source='C:\Documents
and Settings\atupele.kyomo.AG\Desktop\Atupele\Business Plans\Shilabela
Trading Company\Accounting Database\StandingData.mdb';"
Cnxn.Open strCnxn
' Open Ledger Table with a cursor that allows updates
Set rstSalesPriceList = New ADODB.Recordset
rstSalesPriceList.CursorLocation = adUseClient
strSQL = "SELECT * FROM SalesPriceList WHERE CVar(StockID) ='" &
strStockID & "' And CVar(SalesPriceList.StartDate) <='" & dteTransactionDate
& "' Order By StartDate DESC"
rstSalesPriceList.Open strSQL, strCnxn, adOpenKeyset, adLockOptimistic,
adCmdText
varResult = rstSalesPriceList!UnitPrice
'Assign the return value.
GetUnitPrice = CDbl(varResult)
Exit_GetUnitPrice:
'Close the connection
rstSalesPriceList.Close
Cnxn.Close
Set rstSalesPriceList = Nothing
Set Cnxn = Nothing
Exit Function
'Err_GetUnitPrice:
' MsgBox Err.Description, vbExclamation, "GetUnitPrice Error " & Err.Number
'Resume Exit_GetUnitPrice
End Function
Private Sub txtUnitPrice_GotFocus()
txtUnitPrice = GetUnitPrice()
End Sub

Signature
atu
> >I have a mainform CashSales (TransactionDate as short date) and subform
> >CashSalesDetails (stockid, unitprice) bound to their respective tables. I
[quoted text clipped - 25 lines]
> Format(dteTransactionDate), "\#m\/d\/yyyy\#")
> & " ORDER BY StartDate DESC"
Marshall Barton - 12 Mar 2008 16:19 GMT
>Thanks for the reply. When I saw how short your sql string was compared to
>mine, I became determined to improve mine as well until I went back to
[quoted text clipped - 16 lines]
>
>What do you think?
I think is you should not be using code for this
calculation. Instead you could bind the price text box to a
calculated field in the record source query where. The
calculated field could be almost the same query I posted
used as a subquery.
A DAO query might be along these lines:
SELECT T.ItemID, ... ,
(SELECT TOP 1 X.UnitPrice
FROM SalesPriceList As X
WHERE X.StockID = T.StockID
And StartDate <= T.TransactionDate
ORDER BY StartDate DESC) As Price
FROM sometable As T
WHERE . . .

Signature
Marsh
MVP [MS Access]