Hi,
I have this form frmSearchProducts with several textboxes where the user can fill in a value.
My code works perfectly for all the fields on the form, except for the fields where the Productcode and Analytical code have to be filled in.
So, when the user enters for example the letter 'p' in the textbox 'txtProductName' and then presses the command button on the form, all the products that haven a name starting with 'p' from the table tblProducts are shown in the listbox. But when the user enters for example the analytical code '6050' all records from the table tblProducts are shown, while only those with the choosen analytical code should be in the list.
this is my code behind the form frmSearchProducts:
-------------------------------------------
Option Compare Database
Option Explicit
-------------------------------------------
Private Sub cmdSearchNow_Click()
Dim strSql As String
Dim strCriteria As String
Dim strRowsource As String
strSql = "SELECT * FROM tblProducts"
strCriteria = ""
AddArgument NameProduct, "NameProduct", strCriteria ArgumentToevoegen Price, "Price", strCriteria
......
AddArgument Productcode_Supplier_, "Productcode_Supplier_", strCriteria
AddArgument Analytical_code, "Analytical_code", strCriteria
If strCriteria <> "" Then
strSql = strSql & " WHERE "
End If
strRowsource = strSql & strCriteria
lstResultSet.RowSource = strRowsource
lstResultSet.Requery
End Sub
---------------------------------------------------------
Public Sub AddArgument(ByVal varFieldvalue As Variant, ByVal strFieldName As String, ByRef strCriteria As String)
Dim intL As Integer
If varFieldvalue <> "" Then
If strCriteria <> "" Then
strCriteria = strCriteria & " And "
End If
intL = Len(varFieldValue)
strCriteria = strCriteria & " LEFT(" & strFieldName & _
"," & intL & "
= '" & varFieldvalue & "'"
End If
End Sub
-----------------------------------------------------------
Private Sub cmdSelect_Click()
Dim bytDummy As Byte
If IsNull(lstResultSet) Then
bytDummy = MsgBox("Select an item from the list", _
vbOKOnly + vbExclamation, "Controle "
lstResultSet.SetFocus
Exit Sub
End If
Forms!frmProducts.Form.RecordSource = "SELECT * FROM " & _
" tblProducts WHERE ProductId=" & lstResultSet
DoCmd.Close
End Sub
-----------------------------------------------------------
Private Sub lstResultSet_DblClick(Cancel As Integer)
cmdSelect_Click
End Sub
----------------------------------------------------------
Thanks a lot in advance for any help,
dj.
I have this form frmSearchProducts with several textboxes where the user can fill in a value.
My code works perfectly for all the fields on the form, except for the fields where the Productcode and Analytical code have to be filled in.
So, when the user enters for example the letter 'p' in the textbox 'txtProductName' and then presses the command button on the form, all the products that haven a name starting with 'p' from the table tblProducts are shown in the listbox. But when the user enters for example the analytical code '6050' all records from the table tblProducts are shown, while only those with the choosen analytical code should be in the list.
this is my code behind the form frmSearchProducts:
-------------------------------------------
Option Compare Database
Option Explicit
-------------------------------------------
Private Sub cmdSearchNow_Click()
Dim strSql As String
Dim strCriteria As String
Dim strRowsource As String
strSql = "SELECT * FROM tblProducts"
strCriteria = ""
AddArgument NameProduct, "NameProduct", strCriteria ArgumentToevoegen Price, "Price", strCriteria
......
AddArgument Productcode_Supplier_, "Productcode_Supplier_", strCriteria
AddArgument Analytical_code, "Analytical_code", strCriteria
If strCriteria <> "" Then
strSql = strSql & " WHERE "
End If
strRowsource = strSql & strCriteria
lstResultSet.RowSource = strRowsource
lstResultSet.Requery
End Sub
---------------------------------------------------------
Public Sub AddArgument(ByVal varFieldvalue As Variant, ByVal strFieldName As String, ByRef strCriteria As String)
Dim intL As Integer
If varFieldvalue <> "" Then
If strCriteria <> "" Then
strCriteria = strCriteria & " And "
End If
intL = Len(varFieldValue)
strCriteria = strCriteria & " LEFT(" & strFieldName & _
"," & intL & "
End If
End Sub
-----------------------------------------------------------
Private Sub cmdSelect_Click()
Dim bytDummy As Byte
If IsNull(lstResultSet) Then
bytDummy = MsgBox("Select an item from the list", _
vbOKOnly + vbExclamation, "Controle "
lstResultSet.SetFocus
Exit Sub
End If
Forms!frmProducts.Form.RecordSource = "SELECT * FROM " & _
" tblProducts WHERE ProductId=" & lstResultSet
DoCmd.Close
End Sub
-----------------------------------------------------------
Private Sub lstResultSet_DblClick(Cancel As Integer)
cmdSelect_Click
End Sub
----------------------------------------------------------
Thanks a lot in advance for any help,
dj.