I was wondering if it would be possible to combine the following two queries given the scenario. I have a value list listbox that I want populated with certain data. So, this is what I do.
I select all records with a given criteria. Then for each result, I do another query and based on the results of the second query, I either add or don't add the data to the listbox. I was using the .AddItem method of the listbox, but that's new to Access 2002, and we need it to work in 2000. I was hoping to get it all into one query so I could change the listbox to use a Table/Query source.
I think the best thing to do is just post the code up. So, here it is:
That's the first section. The second query is in the CheckTraining function, which I'll post now:
I'm not even sure if you're able to tell anything or help from the given info, but any help would be appreciated GREATLY. I've been mulling over this for a while now. If you need any other background info or anything, please let me know. Thanks much!
I select all records with a given criteria. Then for each result, I do another query and based on the results of the second query, I either add or don't add the data to the listbox. I was using the .AddItem method of the listbox, but that's new to Access 2002, and we need it to work in 2000. I was hoping to get it all into one query so I could change the listbox to use a Table/Query source.
I think the best thing to do is just post the code up. So, here it is:
Code:
Private Sub cmdRefresh_Click()
Dim Conn As ADODB.Connection
Dim Rs1, Rs2 As ADODB.Recordset
Dim strSQL1, strSQL2 As String
Dim nIndex As Integer
Dim strName, strDescription, strNumber As String
Dim dDate As Date
Dim nDuration As Integer
Set Conn = CurrentProject.Connection
Set Rs1 = New ADODB.Recordset
strSQL1 = "SELECT Employee.Name, Employee.[No], Training.Description, Training.Duration, Max(Training.dDate) AS MaxOfdDate FROM Employee INNER JOIN Training ON Employee.[No]=Training.[Employee No] WHERE (((Training.Duration)>0) And ((Training.Required)=-1) And ((Training.Completed)=-1) And ((Employee.[No])=[Employee No])) GROUP BY Employee.Name, Employee.[No], Training.Description, Training.Duration"
Rs1.Open strSQL1, Conn, adOpenStatic, adLockReadOnly
While Not Rs1.EOF
strName = Rs1.Fields(0)
strNumber = Rs1.Fields(1)
strDescription = Rs1.Fields(2)
nDuration = Rs1.Fields(3)
dDate = Rs1.Fields(4)
If CheckTraining(strNumber, dDate, nDuration) = 0 Then
strName = Replace(strName, ",", " ")
lstReqTraining.AddItem (strName & ";" & strNumber & ";" & strDescription & ";" & dDate & ";" & nDuration)
End If
Rs1.MoveNext
Wend
Rs1.Close
Set Rs1 = Nothing
Set Conn = Nothing
End Sub
That's the first section. The second query is in the CheckTraining function, which I'll post now:
Code:
'Returned value will be 0 or 1
Private Function CheckTraining(ByVal nEmployeeNo As String, ByVal dDate As Date, ByVal nDuration As Integer)
Dim dOldDate, dNewDate As Date
dOldDate = dDate
dNewDate = DateAdd("m", nDuration, dOldDate)
Dim Conn As ADODB.Connection
Dim Rs As ADODB.Recordset
Dim strSQL As String
Dim nCount As Integer
Set Conn = CurrentProject.Connection
Set Rs = New ADODB.Recordset
strSQL = "SELECT T.ID FROM Employee E, Training T "
strSQL = strSQL & "WHERE ((T.Duration>0) AND (T.Required=-1) AND (T.Completed=0) AND (E.No=T.[Employee No]) AND (E.No="
strSQL = strSQL & nEmployeeNo
strSQL = strSQL & ") AND (T.dDateRequired=#"
strSQL = strSQL & dNewDate
strSQL = strSQL & "#))"
Rs.Open strSQL, Conn, adOpenStatic, adLockReadOnly
nCount = Rs.RecordCount
Rs.Close
Set Rs = Nothing
Set Conn = Nothing
CheckTraining = nCount
End Function
I'm not even sure if you're able to tell anything or help from the given info, but any help would be appreciated GREATLY. I've been mulling over this for a while now. If you need any other background info or anything, please let me know. Thanks much!