Hello everyone.
Hoping this wasn't covered elsewhere.
I have a listbox in a database form, which shows all products in the database according to various search criteria. Currently, the rowsource is based on an underlying query, ordered by several different things (Supplier name, Product ID, etc etc). I want to be able to click buttons to re-sort differently. If I understand correctly, there isn't a simple listbox function that can do this and I should do it changing the datasource by SQL.
However, although the query with the "fixed" sort order is working fine, I can't work out how to convert it to a string that works in vba as it is very long:
SELECT tblProducts.ProductStatusID, tblManufacturer.ManufacturerName, tblSuppliers.SupplierName, tblProducts.ProductID, tblProducts.ProductTitle, tblProductStatus.ProductStatusCategory, tblProducts.DealerDiscount, tblProducts.ProductDateAdded, tblProducts.ProductLastModified, tblProducts.SupplierPartNumber, tblProducts.ProductPrice, tblSupplierCurrency.SupplierCurrency, Nz([CountofInventoryQuantity],0) AS NZQuantity, tblProducts.ProductDetailDescription, tblProducts.ProductSummaryDescription, tblProducts.ProductVatable, tblProducts.ProductNotes, tblProducts.ProductInventoryRequiresSerialNumber, tblProducts.ProductIsVirtualItem, tblProducts.ProductModifiedBy, tblProducts.ProductsSupplierQuoteNumber, tblSupplierCurrency.SupplierCurrencyID, tblProducts.ProductPriceIfNotSterling, tblProducts.ProductSupplierLocalPrice, tblProducts.ProductDeleted, qryInventoryCountForProductSearch.CountOfInventoryQuantity FROM (tblSupplierCurrency INNER JOIN tblSuppliers ON tblSupplierCurrency.SupplierCurrencyID = tblSuppliers.SupplierCurrencyID) INNER JOIN (tblProductStatus INNER JOIN (tblManufacturer RIGHT JOIN (tblProducts LEFT JOIN qryInventoryCountForProductSearch ON tblProducts.ProductID = qryInventoryCountForProductSearch.ProductID) ON tblManufacturer.ManufacturerID = tblProducts.ManufacturerID) ON tblProductStatus.ProductStatusID = tblProducts.ProductStatusID) ON tblSuppliers.SupplierID = tblProducts.SupplierID WHERE (((tblProducts.ProductStatusID) Like "*" & [Forms]![frmProductSearch].[Form]![txtProductStatus] & "*") AND ((tblManufacturer.ManufacturerName) Like "*" & [Forms]![frmProductSearch].[Form]![txtManufacturer] & "*") AND ((tblSuppliers.SupplierName) Like "*" & [Forms]![frmProductSearch].[Form]![txtSupplier] & "*") AND ((tblProducts.ProductID) Like "*" & [forms]![frmProductSearch].[Form]![txtProductID] & "*") AND ((tblProducts.ProductTitle) Like "*" & [Forms]![frmProductSearch].[Form]![txtTitleSearch1] & "*" And (tblProducts.ProductTitle) Like "*" & [Forms]![frmProductSearch].[Form]![txtTitleSearch2] & "*") AND ((tblProducts.ProductDateAdded) Between (Nz(([Forms]![frmProductSearch].[Form]![txtDate1]),1/1/1901)) And (Nz([Forms]![frmProductSearch].[Form]![txtDate2],#1/1/3000#))) AND ((tblProducts.SupplierPartNumber) Like "*" & [Forms]![frmProductSearch].[Form]![txtSupplierPartNumber] & "*") AND ((tblProducts.ProductDeleted)=False) AND ((tblProducts.SupplierID) Like "*" & [Forms]![frmProductSearch].[Form]![cboSupplierID] & "*")) OR (((tblProducts.ProductStatusID) Like "*" & [Forms]![frmProductSearch].[Form]![txtProductStatus] & "*") AND ((tblSuppliers.SupplierName) Like "*" & [Forms]![frmProductSearch].[Form]![txtSupplier] & "*") AND ((tblProducts.ProductID) Like "*" & [forms]![frmProductSearch].[Form]![txtProductID] & "*") AND ((tblProducts.ProductTitle) Like "*" & [Forms]![frmProductSearch].[Form]![txtTitleSearch1] & "*" And (tblProducts.ProductTitle) Like "*" & [Forms]![frmProductSearch].[Form]![txtTitleSearch2] & "*") AND ((tblProducts.ProductDateAdded) Between (Nz(([Forms]![frmProductSearch].[Form]![txtDate1]),1/1/1901)) And (Nz([Forms]![frmProductSearch].[Form]![txtDate2],#1/1/3000#))) AND ((tblProducts.SupplierPartNumber) Like "*" & [Forms]![frmProductSearch].[Form]![txtSupplierPartNumber] & "*") AND ((tblProducts.ProductDeleted)=False) AND ((tblProducts.SupplierID) Like "*" & [Forms]![frmProductSearch].[Form]![cboSupplierID] & "*") AND (([Forms]![frmProductSearch].[Form]![txtManufacturer]) Is Null)) ORDER BY tblProducts.ProductStatusID, tblSuppliers.SupplierName, tblProducts.ProductID;
I'm trying to specify the row source thus:
me.lstSearch.rowsource = mySQL
When the appropriate button is clicked, I want to use modified SQL with different ORDER BY.
However, due to the size of the code, I can't add it to a single line of vba. I've tried to do it incrementally (mySQL = "first bit", mySQL = mySQL & "second bit"). In this case, I'm getting a type mismatch error (although the same query worked fine).
Any pointers about how to approach this will be gratefully received.
Pete
Hoping this wasn't covered elsewhere.
I have a listbox in a database form, which shows all products in the database according to various search criteria. Currently, the rowsource is based on an underlying query, ordered by several different things (Supplier name, Product ID, etc etc). I want to be able to click buttons to re-sort differently. If I understand correctly, there isn't a simple listbox function that can do this and I should do it changing the datasource by SQL.
However, although the query with the "fixed" sort order is working fine, I can't work out how to convert it to a string that works in vba as it is very long:
SELECT tblProducts.ProductStatusID, tblManufacturer.ManufacturerName, tblSuppliers.SupplierName, tblProducts.ProductID, tblProducts.ProductTitle, tblProductStatus.ProductStatusCategory, tblProducts.DealerDiscount, tblProducts.ProductDateAdded, tblProducts.ProductLastModified, tblProducts.SupplierPartNumber, tblProducts.ProductPrice, tblSupplierCurrency.SupplierCurrency, Nz([CountofInventoryQuantity],0) AS NZQuantity, tblProducts.ProductDetailDescription, tblProducts.ProductSummaryDescription, tblProducts.ProductVatable, tblProducts.ProductNotes, tblProducts.ProductInventoryRequiresSerialNumber, tblProducts.ProductIsVirtualItem, tblProducts.ProductModifiedBy, tblProducts.ProductsSupplierQuoteNumber, tblSupplierCurrency.SupplierCurrencyID, tblProducts.ProductPriceIfNotSterling, tblProducts.ProductSupplierLocalPrice, tblProducts.ProductDeleted, qryInventoryCountForProductSearch.CountOfInventoryQuantity FROM (tblSupplierCurrency INNER JOIN tblSuppliers ON tblSupplierCurrency.SupplierCurrencyID = tblSuppliers.SupplierCurrencyID) INNER JOIN (tblProductStatus INNER JOIN (tblManufacturer RIGHT JOIN (tblProducts LEFT JOIN qryInventoryCountForProductSearch ON tblProducts.ProductID = qryInventoryCountForProductSearch.ProductID) ON tblManufacturer.ManufacturerID = tblProducts.ManufacturerID) ON tblProductStatus.ProductStatusID = tblProducts.ProductStatusID) ON tblSuppliers.SupplierID = tblProducts.SupplierID WHERE (((tblProducts.ProductStatusID) Like "*" & [Forms]![frmProductSearch].[Form]![txtProductStatus] & "*") AND ((tblManufacturer.ManufacturerName) Like "*" & [Forms]![frmProductSearch].[Form]![txtManufacturer] & "*") AND ((tblSuppliers.SupplierName) Like "*" & [Forms]![frmProductSearch].[Form]![txtSupplier] & "*") AND ((tblProducts.ProductID) Like "*" & [forms]![frmProductSearch].[Form]![txtProductID] & "*") AND ((tblProducts.ProductTitle) Like "*" & [Forms]![frmProductSearch].[Form]![txtTitleSearch1] & "*" And (tblProducts.ProductTitle) Like "*" & [Forms]![frmProductSearch].[Form]![txtTitleSearch2] & "*") AND ((tblProducts.ProductDateAdded) Between (Nz(([Forms]![frmProductSearch].[Form]![txtDate1]),1/1/1901)) And (Nz([Forms]![frmProductSearch].[Form]![txtDate2],#1/1/3000#))) AND ((tblProducts.SupplierPartNumber) Like "*" & [Forms]![frmProductSearch].[Form]![txtSupplierPartNumber] & "*") AND ((tblProducts.ProductDeleted)=False) AND ((tblProducts.SupplierID) Like "*" & [Forms]![frmProductSearch].[Form]![cboSupplierID] & "*")) OR (((tblProducts.ProductStatusID) Like "*" & [Forms]![frmProductSearch].[Form]![txtProductStatus] & "*") AND ((tblSuppliers.SupplierName) Like "*" & [Forms]![frmProductSearch].[Form]![txtSupplier] & "*") AND ((tblProducts.ProductID) Like "*" & [forms]![frmProductSearch].[Form]![txtProductID] & "*") AND ((tblProducts.ProductTitle) Like "*" & [Forms]![frmProductSearch].[Form]![txtTitleSearch1] & "*" And (tblProducts.ProductTitle) Like "*" & [Forms]![frmProductSearch].[Form]![txtTitleSearch2] & "*") AND ((tblProducts.ProductDateAdded) Between (Nz(([Forms]![frmProductSearch].[Form]![txtDate1]),1/1/1901)) And (Nz([Forms]![frmProductSearch].[Form]![txtDate2],#1/1/3000#))) AND ((tblProducts.SupplierPartNumber) Like "*" & [Forms]![frmProductSearch].[Form]![txtSupplierPartNumber] & "*") AND ((tblProducts.ProductDeleted)=False) AND ((tblProducts.SupplierID) Like "*" & [Forms]![frmProductSearch].[Form]![cboSupplierID] & "*") AND (([Forms]![frmProductSearch].[Form]![txtManufacturer]) Is Null)) ORDER BY tblProducts.ProductStatusID, tblSuppliers.SupplierName, tblProducts.ProductID;
I'm trying to specify the row source thus:
me.lstSearch.rowsource = mySQL
When the appropriate button is clicked, I want to use modified SQL with different ORDER BY.
However, due to the size of the code, I can't add it to a single line of vba. I've tried to do it incrementally (mySQL = "first bit", mySQL = mySQL & "second bit"). In this case, I'm getting a type mismatch error (although the same query worked fine).
Any pointers about how to approach this will be gratefully received.
Pete