S
s_wadhwa
Hi,
I'm populating the listbox with query having a where condition in VBA
code. the first time list box fills correctly but when I change the
WHERE condition which is coming from the combo box in the MS Access
Form it doesn not clear the list box values from the query done first
time and appends the resultset query in the listbox.
Please anyone can suggest me how to clear the previous recordset values
and fill with new recordset values.
I hope the question is clear. I am also appending the code I have
written for it.
Any suggestions are welcome.
Thanks,
Shalini
------------------------------------------------------------------------------------------------
' Variable Declaration
Dim cnn1 As ADODB.Connection
Dim rstDept, rstFloor, rstRoomUseCode As ADODB.Recordset
Dim i, j, k As Integer
' Set Connection to Current Project
Set cnn1 = CurrentProject.Connection
' Set values for Department, Floor and RoomUseCode data based on
BuildingNumber
Set rstDept = New ADODB.Recordset
Set rstFloor = New ADODB.Recordset
Set rstRoomUseCode = New ADODB.Recordset
rstDept.Open "SELECT departmentcode,abbrev FROM
vFDXQryDepartmentFilterCboValue WHERE ((BuildingNumber='" +
[Forms]![frmrooms]![BuildingNumber] + "')) ORDER BY Abbrev;", cnn1
rstFloor.Open "SELECT DISTINCT floor FROM vFDXQryCtlFloorCboValue WHERE
((BuildingNumber='" + [Form].[BuildingNumber] + "')) ORDER BY Floor; ",
cnn1
rstRoomUseCode.Open "SELECT DISTINCT RoomUseCode FROM
vFDXQryRoomsCboValue WHERE ((BuildingNumber='" +
[Form].[BuildingNumber] + "')) ORDER BY RoomUseCode; ", cnn1
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
'Fill the DepartmentFilter Listbox with values from recordset
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If rstDept.RecordCount <> 0 Then
rstDept.MoveFirst
i = 1
Me.DepartmentFilter.AddItem "*", 0
Do While Not rstDept.EOF
Me.DepartmentFilter.AddItem rstDept!DepartmentCode + " - " +
rstDept!abbrev, i
rstDept.MoveNext
i = i + 1
Loop
End If
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
'Fill the Floor Listbox with values from recordset
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If rstFloor.RecordCount <> 0 Then
rstFloor.MoveFirst
j = 1
Me.ctlFloor.AddItem "*", 0
Do While Not rstFloor.EOF
Me.ctlFloor.AddItem rstFloor!Floor, j
rstFloor.MoveNext
j = j + 1
Loop
End If
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
'Fill the RoomUseCode Listbox with values from recordset
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If rstRoomUseCode.RecordCount <> 0 Then
rstRoomUseCode.MoveFirst
k = 1
Me.RmCdFilter.AddItem "*", 0
Do While Not rstRoomUseCode.EOF
Me.RmCdFilter.AddItem rstRoomUseCode!RoomUseCode, k
rstRoomUseCode.MoveNext
k = k + 1
Loop
End If
' Close the Recordset and Connection
rstDept.Close
rstFloor.Close
rstRoomUseCode.Close
cnn1.Close
' Release the memory
Set rstDept = Nothing
Set rstFloor = Nothing
Set rstRoomUseCode = Nothing
Set cnn1 = Nothing
I'm populating the listbox with query having a where condition in VBA
code. the first time list box fills correctly but when I change the
WHERE condition which is coming from the combo box in the MS Access
Form it doesn not clear the list box values from the query done first
time and appends the resultset query in the listbox.
Please anyone can suggest me how to clear the previous recordset values
and fill with new recordset values.
I hope the question is clear. I am also appending the code I have
written for it.
Any suggestions are welcome.
Thanks,
Shalini
------------------------------------------------------------------------------------------------
' Variable Declaration
Dim cnn1 As ADODB.Connection
Dim rstDept, rstFloor, rstRoomUseCode As ADODB.Recordset
Dim i, j, k As Integer
' Set Connection to Current Project
Set cnn1 = CurrentProject.Connection
' Set values for Department, Floor and RoomUseCode data based on
BuildingNumber
Set rstDept = New ADODB.Recordset
Set rstFloor = New ADODB.Recordset
Set rstRoomUseCode = New ADODB.Recordset
rstDept.Open "SELECT departmentcode,abbrev FROM
vFDXQryDepartmentFilterCboValue WHERE ((BuildingNumber='" +
[Forms]![frmrooms]![BuildingNumber] + "')) ORDER BY Abbrev;", cnn1
rstFloor.Open "SELECT DISTINCT floor FROM vFDXQryCtlFloorCboValue WHERE
((BuildingNumber='" + [Form].[BuildingNumber] + "')) ORDER BY Floor; ",
cnn1
rstRoomUseCode.Open "SELECT DISTINCT RoomUseCode FROM
vFDXQryRoomsCboValue WHERE ((BuildingNumber='" +
[Form].[BuildingNumber] + "')) ORDER BY RoomUseCode; ", cnn1
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
'Fill the DepartmentFilter Listbox with values from recordset
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If rstDept.RecordCount <> 0 Then
rstDept.MoveFirst
i = 1
Me.DepartmentFilter.AddItem "*", 0
Do While Not rstDept.EOF
Me.DepartmentFilter.AddItem rstDept!DepartmentCode + " - " +
rstDept!abbrev, i
rstDept.MoveNext
i = i + 1
Loop
End If
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
'Fill the Floor Listbox with values from recordset
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If rstFloor.RecordCount <> 0 Then
rstFloor.MoveFirst
j = 1
Me.ctlFloor.AddItem "*", 0
Do While Not rstFloor.EOF
Me.ctlFloor.AddItem rstFloor!Floor, j
rstFloor.MoveNext
j = j + 1
Loop
End If
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
'Fill the RoomUseCode Listbox with values from recordset
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If rstRoomUseCode.RecordCount <> 0 Then
rstRoomUseCode.MoveFirst
k = 1
Me.RmCdFilter.AddItem "*", 0
Do While Not rstRoomUseCode.EOF
Me.RmCdFilter.AddItem rstRoomUseCode!RoomUseCode, k
rstRoomUseCode.MoveNext
k = k + 1
Loop
End If
' Close the Recordset and Connection
rstDept.Close
rstFloor.Close
rstRoomUseCode.Close
cnn1.Close
' Release the memory
Set rstDept = Nothing
Set rstFloor = Nothing
Set rstRoomUseCode = Nothing
Set cnn1 = Nothing