New to VBA....quick easy ? I'm sure...
I have three variables (at the bottom of the function)
[CurrentRate][CurrentRateStDt][CurrentRateEndDt]
I would like to be able to pull each of these different variables into a different place in my report via the control source.... how do I do this? Thx for any assistance.
---------------------------------------------------
Control Source:
=Format(CurrentRateData(Forms!frm_Person!personID,DateSerial(Year(Date()),Month(Date()),1),DateSerial(Year(Date()),Month(Date())+1,0)),"mm/dd/yy")
---------------------------------------------------
Code:
Public Function CurrentRateData(PersonID As Integer, StartDate As Date, EndDate As Date) As Integer
Dim conn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim rsPerson As ADODB.Recordset
Dim sConnString As String
Dim iCtr As Integer
'Dim iStartDate As Date
'Dim iEndDate As Date
Dim curRate As Integer
Dim curRateStDt As Date
Dim curRateEndDt As Date
'**** make path to file dynamic
sConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Ron Johnson\My Documents\Blue&White.mdb;"
conn.Open sConnString
Set cmd.ActiveConnection = conn
cmd.CommandText = "SELECT * FROM tbl_PersonRate where PersonID = " & PersonID
cmd.CommandType = adCmdText
Set rsPerson = cmd.Execute
Do While Not rsPerson.EOF
For iCtr = 0 To rsPerson.Fields.Count - 1
'OutPuts Name and Value of each field
Debug.Print rsPerson.Fields(iCtr).Name & ": " & _
rsPerson.Fields(iCtr).Value
Next
'StartDate and EndDate before (Less Than) RateStDt - ignore the record
If StartDate < rsPerson!RateStartDate And EndDate < rsPerson!RateStartDate Then
'StartDate and EndDate after (Greater Than) RateEndDt - ignore the record
ElseIf StartDate > rsPerson!RateEndDate And EndDate > rsPerson!RateEndDate Then
'Do Nothing
Else
If StartDate >= rsPerson!RateStartDate Then
'StartDate after Person's RateStDt... so within that record
iRate = rsPerson!Rate
iRateStDt = rsPerson!RateStartDate
iRateEndDt = rsPerson!RateEndDate
ElseIf EndDate <= rsPerson!RateEndDate Then
'StartDate before Person's RateStDt...
'BUT EndDate before Person's RateStDt... so within that record
iRate = rsPerson!Rate
iRateStDt = rsPerson!RateStartDate
iRateEndDt = rsPerson!RateEndDate
End If
End If
rsPerson.MoveNext
Loop
CurrentRate = iRate
CurrentRateStDt = iRateStDt
CurrentRateEndDt = iRateEndDt
I have three variables (at the bottom of the function)
[CurrentRate][CurrentRateStDt][CurrentRateEndDt]
I would like to be able to pull each of these different variables into a different place in my report via the control source.... how do I do this? Thx for any assistance.
---------------------------------------------------
Control Source:
=Format(CurrentRateData(Forms!frm_Person!personID,DateSerial(Year(Date()),Month(Date()),1),DateSerial(Year(Date()),Month(Date())+1,0)),"mm/dd/yy")
---------------------------------------------------
Code:
Public Function CurrentRateData(PersonID As Integer, StartDate As Date, EndDate As Date) As Integer
Dim conn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim rsPerson As ADODB.Recordset
Dim sConnString As String
Dim iCtr As Integer
'Dim iStartDate As Date
'Dim iEndDate As Date
Dim curRate As Integer
Dim curRateStDt As Date
Dim curRateEndDt As Date
'**** make path to file dynamic
sConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Ron Johnson\My Documents\Blue&White.mdb;"
conn.Open sConnString
Set cmd.ActiveConnection = conn
cmd.CommandText = "SELECT * FROM tbl_PersonRate where PersonID = " & PersonID
cmd.CommandType = adCmdText
Set rsPerson = cmd.Execute
Do While Not rsPerson.EOF
For iCtr = 0 To rsPerson.Fields.Count - 1
'OutPuts Name and Value of each field
Debug.Print rsPerson.Fields(iCtr).Name & ": " & _
rsPerson.Fields(iCtr).Value
Next
'StartDate and EndDate before (Less Than) RateStDt - ignore the record
If StartDate < rsPerson!RateStartDate And EndDate < rsPerson!RateStartDate Then
'StartDate and EndDate after (Greater Than) RateEndDt - ignore the record
ElseIf StartDate > rsPerson!RateEndDate And EndDate > rsPerson!RateEndDate Then
'Do Nothing
Else
If StartDate >= rsPerson!RateStartDate Then
'StartDate after Person's RateStDt... so within that record
iRate = rsPerson!Rate
iRateStDt = rsPerson!RateStartDate
iRateEndDt = rsPerson!RateEndDate
ElseIf EndDate <= rsPerson!RateEndDate Then
'StartDate before Person's RateStDt...
'BUT EndDate before Person's RateStDt... so within that record
iRate = rsPerson!Rate
iRateStDt = rsPerson!RateStartDate
iRateEndDt = rsPerson!RateEndDate
End If
End If
rsPerson.MoveNext
Loop
CurrentRate = iRate
CurrentRateStDt = iRateStDt
CurrentRateEndDt = iRateEndDt