MajP
You've got your good things, and you've got mine.
- Local time
- Today, 16:23
- Joined
- May 21, 2018
- Messages
- 9,951
I do not think this code goes in button search. Call it instead from all of the following events
TxtSearch after update
SearchFrame after update
Startdate after update
Enddate after update.
I renamed the strings for clarity.
However, this is not going to work as designed, if you plan to be able to put values in more than one control (txtSearch, CmboSearch, the dates)
Example
If the user puts a value in txtSearch the SQL for the listbox is limited to the following
However if they also then put a value in the cmbo and the frame is 3 (Condition 5) you want to search where the DocumentNumber is like txtSearch and the EntityPK = cmboSearch. Also you would like to see entity information in the listbox.
The problem is you cannot because if there is a value in txtSearch then the sql does not include any Entity information, and there is only three columns in the listbox.
Obviously the easiest fix is one conditions with SQL and columns that includes all possible fields.
DocumentNumber, TranType, TranDate,VendorPK, EntityFK, CompanyName, CompanyNameArabic...
This same problem happens if you put in StartDate, EndDate and then pick something from txtSearch. Or dates and the cmbo.
If you want to narrow down the fields returned you could maybe reorganize. So first check if Condition 5 (it requires the most fields), then condition 3 etc.
TxtSearch after update
SearchFrame after update
Startdate after update
Enddate after update.
I renamed the strings for clarity.
Code:
Private Sub SetListbox()
Dim sqlInvoice As String
Dim sqlTranType As String
Dim sqlVendor As String
Dim sqlEntity As String
Dim sqlDate As String
sqlInvoice = "SELECT DocumentNumber, TranType, TranDate FROM tblTranTypes INNER JOIN tblTransactions ON tblTranTypes.TranTypePK = tblTransactions.TranTypeFK WHERE DocumentNumber='" & Me.txtSearch & "'" 'LIKE '*" & Me.txtSearch.Value & "*'"
sqlTranType = "SELECT DocumentNumber, TranType, TranTypeFK, TranDate FROM tblTranTypes INNER JOIN tblTransactions ON tblTranTypes.TranTypePK = tblTransactions.TranTypeFK WHERE TranTypeFK=" & Me.cboSearch 'LIKE '*" & Me.cboSearch.Value & "*'"
sqlVendor = "SELECT DocumentNumber, TranDate, VendorsFK, CompanyName, CompanyNameArabic FROM tblVendors INNER JOIN tblTransactions ON tblVendors.VendorsPK = tblTransactions.VendorsFK WHERE VendorsFK=" & Me.cboSearch 'LIKE '*" & Me.cboSearch.Value & "*'"
sqlDate = "SELECT DocumentNumber, TranDate, TranType, EntityName FROM tblEntities RIGHT JOIN (tblTranTypes INNER JOIN tblTransactions ON tblTranTypes.TranTypePK = tblTransactions.TranTypeFK) ON tblEntities.EntityPK = tblTransactions.EntityFK WHERE TranDate Between #" & Me.StartDate & "# And #" & Me.EndDate & "#"
sqlEntity = "SELECT tblTransactions.DocumentNumber, tblTransactions.TranDate, tblEntities.EntityName, tblEntities.EntityNameArabic, tblTranTypes.TranType, tblTranTypes.TranTypeArabic FROM tblTranTypes INNER JOIN (tblEntities RIGHT JOIN tblTransactions ON tblEntities.EntityPK = tblTransactions.EntityFK) ON tblTranTypes.TranTypePK = tblTransactions.TranTypeFK;"
If Not IsNull(Me.txtSearch) Then
'condition 1
Me.Searchlistbox1.RowSource = sqlIvoice
Me.Searchlistbox1.ColumnCount = 3
Me.Searchlistbox1.ColumnWidths = "75px;110px;110px"
ElseIf Me.SearchFrame.Value = 1 And Not IsNull(Me.cboSearch) Then
'condition 2
Me.Searchlistbox1.RowSource = sqlTranType
Me.Searchlistbox1.ColumnCount = 4
Me.Searchlistbox1.ColumnWidths = "75px;110px;0px;110px"
ElseIf Me.SearchFrame.Value = 2 And Not IsNull(Me.cboSearch) Then
'Codition 3
Me.Searchlistbox1.RowSource = sqlVendor
Me.Searchlistbox1.ColumnCount = 5
Me.Searchlistbox1.ColumnWidths = "75px;110px;110px;110px;110px"
ElseIf Not IsNull(Me.StartDate And Me.EndDate) Then
'Condition 4
Me.Searchlistbox1.RowSource = sqlDate
Me.Searchlistbox1.ColumnCount = 4
Me.Searchlistbox1.ColumnWidths = "75px;110px;110px;110px"
ElseIf Me.SearchFrame.Value = 3 And Not IsNull(Me.cboSearch) Then
'condition 5
Me.Searchlistbox1.RowSource = sqlEntity
Me.Searchlistbox1.ColumnCount = 6
Me.Searchlistbox1.ColumnWidths = "75px;110px;110px;110px;110px;110px"
Else
End Sub
However, this is not going to work as designed, if you plan to be able to put values in more than one control (txtSearch, CmboSearch, the dates)
Example
If the user puts a value in txtSearch the SQL for the listbox is limited to the following
Code:
sqlInvoice = "SELECT DocumentNumber, TranType, TranDate FROM tblTranTypes INNER JOIN tblTransactions ON tblTranTypes.TranTypePK = tblTransactions.TranTypeFK WHERE DocumentNumber='" & Me.txtSearch & "'" 'LIKE '*" & Me.txtSearch.Value & "*'"
However if they also then put a value in the cmbo and the frame is 3 (Condition 5) you want to search where the DocumentNumber is like txtSearch and the EntityPK = cmboSearch. Also you would like to see entity information in the listbox.
The problem is you cannot because if there is a value in txtSearch then the sql does not include any Entity information, and there is only three columns in the listbox.
Obviously the easiest fix is one conditions with SQL and columns that includes all possible fields.
DocumentNumber, TranType, TranDate,VendorPK, EntityFK, CompanyName, CompanyNameArabic...
This same problem happens if you put in StartDate, EndDate and then pick something from txtSearch. Or dates and the cmbo.
If you want to narrow down the fields returned you could maybe reorganize. So first check if Condition 5 (it requires the most fields), then condition 3 etc.
Last edited: