Hi
I'll try and explain this in a simplistic way as I tend to overcomplicate my questions. I think my problem will be revealed quite readily by the code.
I am trying to assign the result of a query to a variable so I can use it in a formula. From what I can gather this is possible by way of a Custom Function or Assigning the SQL result directly. I am posting each as a separate thread.
This question relates to the function.
I am trying to modify some code that was suggested in an earlier question. That question was resolved in a different manner, so this is the first time I have tried to do this.
Here is the code
To test the function I am entering, in the Immediate Window, both:
?XRate(12/18/2015,"USD") and
?XRate(12/18/2015,USD)
Both times I get a "Compile Error: Syntax Error" indicated on the line:
Xrate = NZ(db.OpenRecordSet(strSQL)(0),(0)
I suspect that the root of the error is in the SQL but I have quadruple checked and it is copied exactly as written.
Can someone point out where I am going wrong?
Thank you very much for your time
A/R
I'll try and explain this in a simplistic way as I tend to overcomplicate my questions. I think my problem will be revealed quite readily by the code.
I am trying to assign the result of a query to a variable so I can use it in a formula. From what I can gather this is possible by way of a Custom Function or Assigning the SQL result directly. I am posting each as a separate thread.
This question relates to the function.
I am trying to modify some code that was suggested in an earlier question. That question was resolved in a different manner, so this is the first time I have tried to do this.
Here is the code
Code:
Public Function Xrate(ByVal dtTransDate As Date, ByVal strCurrency As String) As Single
'Declare Variables
Dim strSQL As String
Dim db As DAO.Database
'Define Variables
Set db = CurrentDb
strSQL = "SELECT tblFXRates.fxXRate FROM tblFXRates WHERE tblFXRates.[fxDate] = #" & Format(dtTransDate, "mm/dd/yyyy") & "# AND " & _
"tblFXRates.[fxCurrency] = '" & strCurrency & "';"
Xrate = NZ(db.OpenRecordSet(strSQL)(0),(0)
Set db = Nothing
End Function
To test the function I am entering, in the Immediate Window, both:
?XRate(12/18/2015,"USD") and
?XRate(12/18/2015,USD)
Both times I get a "Compile Error: Syntax Error" indicated on the line:
Xrate = NZ(db.OpenRecordSet(strSQL)(0),(0)
I suspect that the root of the error is in the SQL but I have quadruple checked and it is copied exactly as written.
Can someone point out where I am going wrong?
Thank you very much for your time
A/R