Tado69
02-02-2004, 09:04 AM
I have a table in Access 2000 which has a concatenated primary key that consists of the 2 fields NFD_DATE (Date) & NFD_HR (Hour). I am using VB 6 for the front-end and need a query that will first search for records with a date (NFD_DATE) <= to a date selected by the user. Then, out of those records I need to find the the matching record with with an hour value (NFD_HR) <= the hour selected by the user. I am currently using the fllowing code to accomoplish the task, but I am thinking there must be an easier way:
Public Sub gsbGetNorFixedReference(ByVal adRefDate As Date, ByVal aiRefHour As Integer, _
ByRef adNorFixedDate As Date, ByRef aiNorFixedHour As Integer)
Dim rs As ADODB.Recordset, sSQL As String
On Error GoTo ErrorHandler
sSQL = "SELECT NFD_DATE, NFD_HR FROM NOR_FIXED_DATA WHERE NFD_DATE <= " & gfnSQLDate(adRefDate) & " ORDER BY NFD_DATE DESC, NFD_HR DESC"
Set rs = New ADODB.Recordset
rs.Open sSQL, gcnLWFlowsDB, adOpenKeyset, adLockOptimistic, adCmdUnknown
With rs
If Not .EOF Then
.MoveFirst
'.Find "NFD_DATE <= " & gfnSQLDate(adRefDate) 'Find 1st record where current record date is less than or equal to the reference date
If Not .EOF Then 'If found
If .Fields("NFD_DATE").Value = adRefDate Then 'Check if the record's date is equal to the reference date
Do 'If so, then check to see if the hourly value is less than or equal to the reference hour
If .Fields("NFD_HR").Value <= aiRefHour Then
Exit Do 'If yes, then exit the loop
Else
.MoveNext 'else move to the next record
If Not .EOF Then
If .Fields("NFD_DATE").Value <> adRefDate Then
Exit Do 'If the record date is not equal to the reference date then exit the loop
End If
Else
Exit Do 'exit the loop
End If
End If
Loop
End If
adNorFixedDate = .Fields("NFD_DATE").Value 'Set the Norman Fixed Data date
aiNorFixedHour = .Fields("NFD_HR").Value 'Set the Norman Fixed Data hour
End If
End If
End With
ExitRoutine:
If rs.State = adStateOpen Then
rs.Close
Set rs = Nothing
End If
Exit Sub
ErrorHandler:
Call gsbHandleAllErrors(True, Err.Number, Err.Description, MODULE_NAME, "GetNorFixedReference")
Resume ExitRoutine
End Sub
If anyone could help me with this it would be greatly appreciated. Thanks.
Public Sub gsbGetNorFixedReference(ByVal adRefDate As Date, ByVal aiRefHour As Integer, _
ByRef adNorFixedDate As Date, ByRef aiNorFixedHour As Integer)
Dim rs As ADODB.Recordset, sSQL As String
On Error GoTo ErrorHandler
sSQL = "SELECT NFD_DATE, NFD_HR FROM NOR_FIXED_DATA WHERE NFD_DATE <= " & gfnSQLDate(adRefDate) & " ORDER BY NFD_DATE DESC, NFD_HR DESC"
Set rs = New ADODB.Recordset
rs.Open sSQL, gcnLWFlowsDB, adOpenKeyset, adLockOptimistic, adCmdUnknown
With rs
If Not .EOF Then
.MoveFirst
'.Find "NFD_DATE <= " & gfnSQLDate(adRefDate) 'Find 1st record where current record date is less than or equal to the reference date
If Not .EOF Then 'If found
If .Fields("NFD_DATE").Value = adRefDate Then 'Check if the record's date is equal to the reference date
Do 'If so, then check to see if the hourly value is less than or equal to the reference hour
If .Fields("NFD_HR").Value <= aiRefHour Then
Exit Do 'If yes, then exit the loop
Else
.MoveNext 'else move to the next record
If Not .EOF Then
If .Fields("NFD_DATE").Value <> adRefDate Then
Exit Do 'If the record date is not equal to the reference date then exit the loop
End If
Else
Exit Do 'exit the loop
End If
End If
Loop
End If
adNorFixedDate = .Fields("NFD_DATE").Value 'Set the Norman Fixed Data date
aiNorFixedHour = .Fields("NFD_HR").Value 'Set the Norman Fixed Data hour
End If
End If
End With
ExitRoutine:
If rs.State = adStateOpen Then
rs.Close
Set rs = Nothing
End If
Exit Sub
ErrorHandler:
Call gsbHandleAllErrors(True, Err.Number, Err.Description, MODULE_NAME, "GetNorFixedReference")
Resume ExitRoutine
End Sub
If anyone could help me with this it would be greatly appreciated. Thanks.