M
mieke
Hi,
I would like to create a search form that is pretty flexible so I
created a from with oll the fields possible on it so you can fill them
in or not.
then I put a button on it that will get the query in a listbox.
this is the code behind the search button:
Private Sub cmdSearch_Click()
Dim ctl As Control
Dim qry As String
Dim teller As Integer
Dim lengte As Integer
Dim i As Integer
Dim field As String
Dim fieldtxtlen As String
Dim fieldtxt As String
Dim Condition As String
Dim sSelect As String
'Clear the listBox
lstQuery.RowSource = ""
' Check how much fields are filled in and put those fieldnames in
variable qry
teller = 0
For Each ctl In Me.Controls
If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox
Then
If Not IsNull(ctl.Value) Then
qry = qry + ctl.Name + ","
teller = teller + 1
End If
End If
Next
' Determine where condition
Condition = ""
lengte = Len(qry)
For i = 1 To lengte
If Mid(qry, i, 1) <> "," Then
fieldtxt = fieldtxt + Mid(qry, i, 1)
Else
fieldtxtlen = Len(fieldtxt)
field = Mid(fieldtxt, 4, fieldtxtlen - 3)
Condition = Condition + "tblmaterial." + field + " = me."
+ fieldtxt + _
".value and "
fieldtxt = ""
End If
Next
'get rid of the last " and " in the condition
lengte = Len(Condition)
If Mid(Condition, lengte - 4, 5) = " and " Then
Condition = Mid(Condition, 1, lengte - 4)
End If
'open the recordset to fill in the listbox
rstado.Open "select tblMaterial.AssetTag, tblmaterial.ClassID," &
_
"tblMaterial.SerialNumber, tblMaterial.Model,
tblMaterial.Status, " & _
"tblUsers.LastName, tblUsers.FirstName,
tblMaterial.OfficeLocation," & _
"tblUsers.CostCenter from tblMaterial " & _
"inner join tblUsers on tblMaterial.accountname =
tblUsers.accountname" & _
" where " & Condition & "", CurrentProject.Connection,
adOpenStatic, adLockReadOnly
lstQuery.RowSourceType = "Value List"
lstQuery.ColumnCount = 9
Do While Not rstado.EOF
lstQuery.AddItem (rstado!AssetTag & ";" & rstado!ClassID & ";"
& rstado!SerialNumber & _
";" & rstado!Model & ";" & rstado!Status &
";" & rstado!LastName & _
";" & rstado!FirstName & ";" & rstado!
OfficeLocation & ";" & rstado!CostCenter)
rstado.MoveNext
Loop
rstado.Close
End Sub
The problem is that my open Recordset is not working, and I don't see
why.
Can anyone help me on this?
I would like to create a search form that is pretty flexible so I
created a from with oll the fields possible on it so you can fill them
in or not.
then I put a button on it that will get the query in a listbox.
this is the code behind the search button:
Private Sub cmdSearch_Click()
Dim ctl As Control
Dim qry As String
Dim teller As Integer
Dim lengte As Integer
Dim i As Integer
Dim field As String
Dim fieldtxtlen As String
Dim fieldtxt As String
Dim Condition As String
Dim sSelect As String
'Clear the listBox
lstQuery.RowSource = ""
' Check how much fields are filled in and put those fieldnames in
variable qry
teller = 0
For Each ctl In Me.Controls
If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox
Then
If Not IsNull(ctl.Value) Then
qry = qry + ctl.Name + ","
teller = teller + 1
End If
End If
Next
' Determine where condition
Condition = ""
lengte = Len(qry)
For i = 1 To lengte
If Mid(qry, i, 1) <> "," Then
fieldtxt = fieldtxt + Mid(qry, i, 1)
Else
fieldtxtlen = Len(fieldtxt)
field = Mid(fieldtxt, 4, fieldtxtlen - 3)
Condition = Condition + "tblmaterial." + field + " = me."
+ fieldtxt + _
".value and "
fieldtxt = ""
End If
Next
'get rid of the last " and " in the condition
lengte = Len(Condition)
If Mid(Condition, lengte - 4, 5) = " and " Then
Condition = Mid(Condition, 1, lengte - 4)
End If
'open the recordset to fill in the listbox
rstado.Open "select tblMaterial.AssetTag, tblmaterial.ClassID," &
_
"tblMaterial.SerialNumber, tblMaterial.Model,
tblMaterial.Status, " & _
"tblUsers.LastName, tblUsers.FirstName,
tblMaterial.OfficeLocation," & _
"tblUsers.CostCenter from tblMaterial " & _
"inner join tblUsers on tblMaterial.accountname =
tblUsers.accountname" & _
" where " & Condition & "", CurrentProject.Connection,
adOpenStatic, adLockReadOnly
lstQuery.RowSourceType = "Value List"
lstQuery.ColumnCount = 9
Do While Not rstado.EOF
lstQuery.AddItem (rstado!AssetTag & ";" & rstado!ClassID & ";"
& rstado!SerialNumber & _
";" & rstado!Model & ";" & rstado!Status &
";" & rstado!LastName & _
";" & rstado!FirstName & ";" & rstado!
OfficeLocation & ";" & rstado!CostCenter)
rstado.MoveNext
Loop
rstado.Close
End Sub
The problem is that my open Recordset is not working, and I don't see
why.
Can anyone help me on this?