Need help with SQL query

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.

Shurik12
02-02-2004, 09:34 AM
Hi,

Why don't you try?
" WHERE NFD_DATE <= ... AND NFD_HR<=..."

Regards,
Shurik.

Tado69
02-02-2004, 09:50 AM
That will not do as the first condition ("NFD_DATE <= ...") must be met before the 2nd condition ("NFD_HR <= ...") . I guess it really is 2 queries. The first gets the set of records whcih meets the first condition, then the 2nd condition must be applied to the set of records generated by the first. Can this be done by a single query?

Shurik12
02-02-2004, 10:33 AM
Please drop a couple of lines with the values from the table and what you would like to see as a result after applying the conditions.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum