Ok, so the saga of my (Work)Calls form continues.
I am now able to pick Positions based on the Employer for the chosen Event (Thanks John G and June 7) and I have even managed to add an unbound TextBox to show the Department the chosen position is associated with.
What I would like to have now is an unbound TextBox that will show the (Pay)Rate of the position based on the date of the work call.
The date of the work call is important because the rates change on a yearly basis.
I have a table where I keep track of the rates (tblRates) and these rates are entered through a subform (sbfrmRates) of a form (frmPositions) to enter information about each Position. The Positions form has tblPositions as its recordsource.
The fields in the Rates Table (tbleRates) are:
RateID (PK, Autonumber)
Rate (Currency)
StartDate (Date/Time)
EndDate (Date/Time)
PositionFK (Number)
The fields in the Positions Table (tblPositions) are
PositionID (PK, Autonumber)
Position (Text)
Department (Text)
EmployerFK (Number)
I would like the unbound Rate Textbox (txtRate) to display the appropriate rate based on the following criteria.
I need to pick the correct Rate for the chosen Position based on the Call Date. To do this I need to look at the Rates Table (tblRates) and pick the Rate, for the chosen Position, that has a StartDate <= CallDate AND EndDate >= CallDate
I have tried various things but think I probably need to use DLookup somehow because the Rate TextBox (txtRate) gets its value from the Rates Table (tblRates) and the recordsource for the Calls Form (frmCalls) where the Rate TextBox resides, is the Calls Table (tbleCalls).
I cannot for the life of me figure out the correct arguments for the Dlookup Function. I have a feeling it should be something like this:
Field Name: Rate
Table Name: tblRates
Criteria: ???
I have entered the following code:
Private Sub cboPosition_AfterUpdate()
'Pick Rate for chosen Position based on Call Date and enter it in Rate TextBox
Dim Rate As Variant
Dim sqlRate As String
sqlRate = "SELECT tblRates.Rate " & _
"FROM tblRates " & _
"WHERE tblRate.PositionFK = " & Me![cboPosition].Column(0) & " " & _
"AND tblRates.StartDate <= " & Me![cboDate] & " " & _
"AND tblRates.EndDate >= " & Me![cboDate] & " "
Rate = DLookup("Rate", "tblRates", "Rate='sqlRate'")
Me![txtRate] = Rate
Stumped (once again).
John V