Hi
I apologize if I sound a little incoherent but I have scrambled my thought processes somewhat; working on this the last few hours.
The following is a simplification of what I am trying to accomplish but will stand to the question without over complicating it.
I do business from Canada and buy products in three currencies and pay for them from accounts in two banks.
The currencies are Canadian Dollars (CAD), U.S. Dollars (USD), and Euro (EUR)
The two banks are a Canadian Bank with separate CAD and USD accounts and a European bank with accounts in Euro.
I list all my purchases on a--continuous-- form [Transactions] showing [Date], [Account], [Item], [Price], [Currency], [FXRate] and [PriceCAD]
I use two tables to accomplish this:
Transactions Table with fields for:
[Date], [Account], [Item], [Price] and [FXRate]
BankAccount Table with fields for:
[AccountName], [Currency]
The RecordSource for the form is a query using the two tables, linked by [Account]-[AccountName], which provides all the data required for the form except for [PriceCad] which is calculated by [Price]*[FXRate] (FXRate is 1 if the account is in CAD).
All this works nicely.
Now.
I enter quite a few transactions in at one time. They cover many dates and each date has many transactions. Currently I have to manually enter the [FXRate] for each transaction which is cumbersome.
I would like the [FXRate] to be populated by a third table [tblFX] that contains:
[Date], [Currency], [FXRate].
It would be quite simple for me to set this up and have the data entry form that I now use populate the [FXRate] field in Transactions table with the correct number. However, this would mean that I would be storing the [FXRate] in each transaction as well as in the [FX] table and, as I understand it, according to rules of Data Normalization, that is not good form. As I am using this Database to teach myself Access and VBA I would like to learn how to do it properly.
My Idea is to eliminate the [FXRate] field from the Transactions table and change the [FXRate] control in the Transactions Form so it contains a call to a Public Function that would use a RecordSet to Query the [tblFX] Table using the form's [Date] and [Currency] controls as criteria in the SQL that would define the query.
I am trying (In the [FXRate] control in the Transactions form) to pass the Date and Currency to a Public Function, include them in the SQL and return the proper FXRate. I have gotten to this point.
In the [FXRate] control of the Transactions Form I put
Then in a Standard Module I define the function (trying to ) as
curCur is defined as a String as it would Contain CAD,EUR or USD.
Because I am unsure of how to procede on each level it is impossible for me to work this out.
Specifically
1) Can I use the [Date] and [Currency] fields as arguments in the Function FXRate which is in the [FXRate] control of the Transactions form? FXRate([Date],[Currency])
2) How do I define the Recordset to run the query
3) How do I get the results of the query to be returned by the function?
Sorry for being so long winded. Any assistance would be greatly appreciated!
AR
I apologize if I sound a little incoherent but I have scrambled my thought processes somewhat; working on this the last few hours.
The following is a simplification of what I am trying to accomplish but will stand to the question without over complicating it.
I do business from Canada and buy products in three currencies and pay for them from accounts in two banks.
The currencies are Canadian Dollars (CAD), U.S. Dollars (USD), and Euro (EUR)
The two banks are a Canadian Bank with separate CAD and USD accounts and a European bank with accounts in Euro.
I list all my purchases on a--continuous-- form [Transactions] showing [Date], [Account], [Item], [Price], [Currency], [FXRate] and [PriceCAD]
I use two tables to accomplish this:
Transactions Table with fields for:
[Date], [Account], [Item], [Price] and [FXRate]
BankAccount Table with fields for:
[AccountName], [Currency]
The RecordSource for the form is a query using the two tables, linked by [Account]-[AccountName], which provides all the data required for the form except for [PriceCad] which is calculated by [Price]*[FXRate] (FXRate is 1 if the account is in CAD).
All this works nicely.
Now.
I enter quite a few transactions in at one time. They cover many dates and each date has many transactions. Currently I have to manually enter the [FXRate] for each transaction which is cumbersome.
I would like the [FXRate] to be populated by a third table [tblFX] that contains:
[Date], [Currency], [FXRate].
It would be quite simple for me to set this up and have the data entry form that I now use populate the [FXRate] field in Transactions table with the correct number. However, this would mean that I would be storing the [FXRate] in each transaction as well as in the [FX] table and, as I understand it, according to rules of Data Normalization, that is not good form. As I am using this Database to teach myself Access and VBA I would like to learn how to do it properly.
My Idea is to eliminate the [FXRate] field from the Transactions table and change the [FXRate] control in the Transactions Form so it contains a call to a Public Function that would use a RecordSet to Query the [tblFX] Table using the form's [Date] and [Currency] controls as criteria in the SQL that would define the query.
I am trying (In the [FXRate] control in the Transactions form) to pass the Date and Currency to a Public Function, include them in the SQL and return the proper FXRate. I have gotten to this point.
In the [FXRate] control of the Transactions Form I put
Code:
FXRate([Date],[Currency])
Then in a Standard Module I define the function (trying to ) as
Code:
Public Function FXRate(ByVal dtDate As Date, ByVal strCur As String) As Long
Define a recordset -Can't figure out how to do this!
FXRate = SELECT FX.FXrate FROM tblFX WHERE (((FX.Date)=dtDate) AND ((FX.Currency)=strCUR));
End Function
curCur is defined as a String as it would Contain CAD,EUR or USD.
Because I am unsure of how to procede on each level it is impossible for me to work this out.
Specifically
1) Can I use the [Date] and [Currency] fields as arguments in the Function FXRate which is in the [FXRate] control of the Transactions form? FXRate([Date],[Currency])
2) How do I define the Recordset to run the query
3) How do I get the results of the query to be returned by the function?
Sorry for being so long winded. Any assistance would be greatly appreciated!
AR
Last edited: