Problems in converting an SQL query into vba rowsource - sql

So I'm having some problems trying to populate a Listbox in Access with an SQL Query. I'm still learning VBA but this is one problem that has stumped me. I've tried fixing up my code but it the AND-date-BETWEEN part doesn't seem to work with my other AND-columnname-LIKE parts.
I was able to get the BETWEEN part to work by itself and the LIKE parts to work by themselves but not together. Here is the primary vb code that I've been trying to modify. Also DQ is another function that helps with the double quotes for the query.
Me.lstGngSheets.RowSource = _
"SELECT gangID, fileName, date, crossStreets FROM tblgangSheets " & _
"WHERE borough LIKE ""*" & DQ(Me.cboBoro.Value) & _
"*"" AND date BETWEEN #" & txtDtBeg.Value & "# and #" & txtDtEnd.Value & "#" & _
"*"" AND onStreet LIKE ""*" & DQ(Me.cboOnStr.Value) & _
"*"" AND yard LIKE ""*" & DQ(Me.cboYrd.Value) & _
"*"" AND safeStreets LIKE ""*" & DQ(Me.chkSenior.Value) & "*"""
Here is an SQL output that I'm trying to aim for to populate my listbox.
SELECT gangID, fileName, date, crossStreets
FROM tblgangSheets
WHERE borough LIKE "Queens"
AND date BETWEEN #01/1/2013# and #1/1/2014#
AND onStreet LIKE "**"
AND yard LIKE "**"
AND safeStreets LIKE "**";
I've been wracking my brain over this for a while, if any of you can give me advice or a solution, I would definitely appreciate it!

YOu have some weird spacing in your SQL between the date and the like's. Try this.
Me.lstGngSheets.RowSource = "SELECT gangID, fileName, date, crossStreets FROM tblgangSheets " & _
"WHERE borough LIKE ""*" & DQ(Me.cboBoro.Value) & _
"*"" AND date BETWEEN #" & txtDtBeg.Value & "# and #" & txtDtEnd.Value & "#" & _
" AND onStreet LIKE ""*" & DQ(Me.cboOnStr.Value) & _
"*"" AND yard LIKE ""*" & DQ(Me.cboYrd.Value) & _
"*"" AND safeStreets LIKE ""*" & DQ(Me.chkSenior.Value) & "*"""
Note that I removed the "*"" immediately after the # closing your end date.

Related

Use one filter value to search and output several fields

Using Visual Basic.
Having searched and searched for an answer, my filter only selects from the 'Recipe' field. I'd like to input 'egg' into my txtSearch textbox and have my button give all recipes that have 'egg' in the recipe or as text in an ingredient. Currently only outputs 2 results: egg nog and eggs benedict. There are 15 recipes with egg in that I'd like to display, too.
Private Sub Search_Button_Click()
On Error GoTo Search_Button_Click_Err
Dim strSQL As String
strSQL = "[Cocktail] like '*" & [Forms]![Find]![txtSearch] & "*'" & _
" Or [Ing1] like '*" & [Forms]![Find]![txtSearch] & "*'" & _
" Or [Ing2] like '*" & [Forms]![Find]![txtSearch] & "*'" & _
" Or [Ing3] like '*" & [Forms]![Find]![txtSearch] & "*'" & _
" Or [Ing4] like '*" & [Forms]![Find]![txtSearch] & "*'" & _
" Or [Ing5] like '*" & [Forms]![Find]![txtSearch] & "*'" & _
" Or [Ing6] like '*" & [Forms]![Find]![txtSearch] & "*'" & _
" Or [Ing7] like '*" & [Forms]![Find]![txtSearch] & "*'"
If Len(strSQL) > 255 Then
MsgBox "ApplyFilter string length exceeds 255 characters"
Else
DoCmd.ApplyFilter "", strSQL
End If
Search_Button_Click_Exit:
Exit Sub
Search_Button_Click_Err:
MsgBox Error$
Resume Search_Button_Click_Exit
End Sub
an option is to concatenate all of the SQL fields that may contain what you're looking for then have that result in the where xxx like ....
Also some SQL environments use a % as a wildcard.

Issues filtering a listbox using variables

I'm unsure whether or not this is the best way to accomplish what I'm attempting to accomplish, but I'm currently attempting to alter the active filters on a listbox using multiple text boxes.
I have one main search box that filters an initial list box, this will also filter a few of my other list boxes that also populate relevant data.
When I look into my other list boxes (not my main one) I have data that I would like to filter further. So, I go into another textbox and input whatever it is I'm attempting to sort out, when I click the second "search button" this is where I get my error. It's a syntax error, but I don't fully understand the syntax behind it.
I'd like for my initial filter to be kept, in addition to my new criteria for searching.
Here's the code behind my initial search button (which this works)
Private Sub Command37_click()
Dim sql As String
Dim sql2 As String
sql = "SELECT People.LName, People.[Phone #], People.State " _
& "FROM People" _
& "WHERE SystemLocation LIKE '*" & Me.SearchTxt1 & "*' " _
& "ORDER by People.LName "
me.List35.RowSource = sql
me.list35.requery
sql2 = "SELECT Orders.Item, Orders.Price, Orders.Department " _
& "FROM People INNER JOIN Orders " _
& "ON People.SystemLocation = Orders.Department " _
& "WHERE Department LIKE '*" & Me.SearchTxt1 & "*' " _
& "ORDER by Orders.Department"
Me.List41.RowSource = sql2
Me.List41.Requery
Here's the code that I was trying to use with the secondary filter
Dim sql2 As String
sql2 = "SELECT Orders.Item, Orders.Price, Orders.Department " _
& "FROM People INNER JOIN Orders " _
& "ON People.SystemLocation = Orders.Department " _
& WHERE (People.SystemLocation LIKE '*" & Me.SearchTxt1 & "*') & (Orders.Item LIKE '*" & Me.SearchTxt2 & "*' " _
& "ORDER by Orders.Department"
Me.List41.RowSource = sql2
Me.List41.Requery
Ideally I'd like to be able to create a "search engine" of sorts.
The syntax error is surrounding your where clause.
Firstly, you are missing a double-quote here:
& WHERE (People.SystemLocation LIKE '*" & Me.SearchTxt1 & "*') & (Orders.Item LIKE '*" & Me.SearchTxt2 & "*' " _
^----- Double quote missing
But the main problem is that you are using the ampersand concatenation operator (&) to represent a logical and statement:
"*') & (Orders.Item LIKE '*"
^----- This should be AND
Instead, you should use the and operator, e.g.:
& "WHERE (People.SystemLocation LIKE '*" & Me.SearchTxt1 & "*') AND (Orders.Item LIKE '*" & Me.SearchTxt2 & "*' " _
However, in this case, the concatenation of form control values isn't required (and opens the potential for SQL injection) since you can directly reference a form value from the Row Source of the list box.
As such, the code can become:
Me.List41.RowSource = _
"SELECT Orders.Item, Orders.Price, Orders.Department " & _
"FROM People INNER JOIN Orders ON People.SystemLocation = Orders.Department " & _
"WHERE " & _
" People.SystemLocation LIKE '*' & Forms![YourForm]!SearchTxt1 & '*' AND " & _
" Orders.Item LIKE '*' & Forms![YourForm]!SearchTxt2 & '*' " & _
"ORDER by Orders.Department"
Me.List41.Requery
Here, change [YourForm] to the name of your form.

How to sort filtered ListBox

I have created search box for a ListBox in form. Search function works as intended, but I would like results to show in ascending order based on first column. I am having trouble understanding how to do that. My code:
Private Sub searchTB_Change()
Dim strSource As String, strSearch As String
'code for searching records
strSearch = Replace(searchTB.Text, "'", "''")
strSource = "SELECT [ID], [VP_veiklioji], [VP_invented_name], [Pareisk_pav], [Par_gavimo_data], [Finished] " _
& "FROM TerPar_Oldsys " _
& "WHERE [ID] LIKE '*" & strSearch & "*' " _
& "Or [VP_veiklioji] LIKE '*" & strSearch & "*' " _
& "Or [VP_invented_name] LIKE '*" & strSearch & "*' " _
& "Or [Pareisk_pav] LIKE '*" & strSearch & "*' " _
& "Or [Par_gavimo_data] LIKE '*" & strSearch & "*' " _
& "Or [Finished] LIKE '*" & strSearch & "*' " _
'up to this part everything works
'line below do not work (it gets whole code in red in debugger) and I do not know how to fix it
& "ORDER BY" "[ID]" ASC,"
'bottom two lines work too, I have tryed DoCmd.SetOrderBy but do not understand how to make it work either
Me.oldListBox.ColumnWidths = "1.5 cm; 3 cm; 4 cm; 4 cm; 2 cm; 0.6 cm"
Me.oldListBox.RowSource = strSource
End Sub
EDIT: In my opinion it is not duplicate, since I am aiming at totally different architecture which turns out needed only quotes removal as Gustav suggested.
Remove the quotes and the comma:
& "ORDER BY [ID] ASC"

AdvancedSearch on Outlook through Excel VBA

I'm trying to perform an advancedsearch in my mailbox through Excel VBA but i don't know how to make a case-insensitive search.
Here is my Filter:
Filter = "urn:schemas:httpmail:subject LIKE '%Pending%'" & _
"AND urn:schemas:httpmail:textdescription LIKE '%" & ISIN & "%'" & _
"AND urn:schemas:httpmail:textdescription LIKE '%" & StlDate & "%'"
I tried with this filter but it does not work. I can't find the emails with "PENDING" in the subject. I also tried with this filter:
Filter = "(urn:schemas:httpmail:subject LIKE '%Pending%'" & _
"AND urn:schemas:httpmail:textdescription LIKE '%" & ISIN & "%'" & _
"AND urn:schemas:httpmail:textdescription LIKE '%" & StlDate & "%' ) OR " & _
"(urn:schemas:httpmail:subject LIKE '%PENDING%'" & _
"AND urn:schemas:httpmail:textdescription LIKE '%" & ISIN & "%'" & _
"AND urn:schemas:httpmail:textdescription LIKE '%" & StlDate & "%' ) "
Using "Option Compare Text" above your sub will allow you to search and find regardless of case. Try that and see if it solves it.

SQL Converting Access DB null value to blank string

I am stuck trying to fix this error and have been at it for far too long. I've done a lot of Googleing and searching this site but can't seem to find the right answer for me even though there are many different variations of this question.
I am writing a front end GUI using VB.Net with an Access database on the back end. The DB has NULL values in PartnerFirstName, PartnerLastName, and Organization in my Partner Table. I am loading these into a Datatable in my application so they can be searched by the user. I just found out last week that some partners (the one's with NULL values) are not loading.
Code below:
("SELECT IDPartner, IIF(ISNULL(PartnerFirstName),'',PartnerFirstName) as PartnerFirstName, IIF(ISNULL(PartnerLastName), '', PartnerLastName) AS PartnerLastName, IIF(ISNULL(Organization), '', Organization) AS Organization, " & _
"Address1, City, State, Zip, Country, Active " & _
"FROM tblPartner " & _
"WHERE IDPartner like '%" & txtPartnerID.Text & "%' AND " & _
" PartnerFirstName like '%" & txtFirstName.Text & "%' AND " & _
" PartnerLastName like '%" & txtLastName.Text & "%' AND " & _
" Organization like '%" & txtOrganization.Text & "%' AND " & _
" Address1 like '%" & txtStreet.Text & "%' AND " & _
" City like '%" & txtCity.Text & "%' AND " & _
" State like '%" & txtState.Text & "%' AND " & _
" Zip like '%" & txtZipCode.Text & "%' AND " & _
" Country like '%" & txtCountry.Text & "%' " & _
"ORDER BY IDPartner, partnerlastname, partnerfirstname", cn)
The reason for the likes are because I want the search to update in real time based on the form's text box values. This is the source of my problem since they are blanks by default and the DB has nulls.
===================
EDIT:
Thank for the initial comments. I realize now that I was using an improper function. the IIF suggested by Baro gets me through the SQL now which is great!
It appears the issue I'm having with records not showing up is in my WHERE clause. If I remove that then all the records load into my Datatable. When I include the Where clause as shown the records will NULL values are excluded.
I have tried to use an OR PartnerFirstName IS NULL (as well as the other fields) but that leaves too many extra results that also have NULL values. I cannot figure out what to do to allow the search now that I know the problem is in the Where section of the statement.
The issue is that the WHERE predicate is targeting the original field(s), which have not had the IIF(ISNULL(...)) expression applied.
It is difficult to see because you've aliased the field in the SELECT statement as the same name as the underlying field. So, the query is doing what you've instructed it to do, albeit not what you've meant for it to do.
To resolve, I would use a subquery which converts the NULL values to empty strings and then in the outer query add your WHERE predicate. Something like the following should do the trick:
("SELECT q.* FROM (SELECT IDPartner, IIF(ISNULL(PartnerFirstName),'',PartnerFirstName) as PartnerFirstName, IIF(ISNULL(PartnerLastName), '', PartnerLastName) AS PartnerLastName, IIF(ISNULL(Organization), '', Organization) AS Organization, " & _
"Address1, City, State, Zip, Country, Active " & _
"FROM tblPartner) q " & _
"WHERE q.IDPartner like '%" & txtPartnerID.Text & "%' AND " & _
" q.PartnerFirstName like '%" & txtFirstName.Text & "%' AND " & _
" q.PartnerLastName like '%" & txtLastName.Text & "%' AND " & _
" q.Organization like '%" & txtOrganization.Text & "%' AND " & _
" q.Address1 like '%" & txtStreet.Text & "%' AND " & _
" q.City like '%" & txtCity.Text & "%' AND " & _
" q.State like '%" & txtState.Text & "%' AND " & _
" q.Zip like '%" & txtZipCode.Text & "%' AND " & _
" q.Country like '%" & txtCountry.Text & "%' " & _
"ORDER BY q.IDPartner, q.partnerlastname, q.partnerfirstname", cn)
Thanks to all for you help and suggestions, I have learned a lot from the few suggestions and really appreciate it!
I have solved the problem by going back into Access. I ran an update query on any NULL field and changed it to a "". Such a simple solution!
If I didn't have the WHERE requirements in place that I do this wouldn't have been such a big deal.