Currently am using the following code for search for a word in the entire table,
Dim searchKey As String = "Sample"
Dim mysql As String = "SELECT * FROM myTable " & _
"WHERE col1 LIKE '%" & searchKey & "%' " & _
" OR col2 LIKE '%" & searchKey & "%' " & _
" OR col3 LIKE '%" & searchKey & "%' " & _
" OR col4 LIKE '%" & searchKey & "%' "
But querying became more difficult when the number of column increases. so can anyone suggest me some alternative method for do the same.
use a loop to build your String programmatically .in this example you only need to add your columns to array.
Dim searchKey As String = "Sample"
Dim array() As String = {"col1", "col2", "col3", "col4"}
Dim b As Boolean = True
Dim mysql As String = "SELECT * FROM myTable WHERE "
For Each str As String In array
If b Then
mysql = mysql & str + " LIKE '%" & searchKey & "%'"
Else
mysql = mysql & " OR " & str & " LIKE '%" & searchKey & "%'"
End If
b = False
Next
Debug.WriteLine(mysql)
output>>
SELECT * FROM myTable WHERE col1 LIKE '%Sample%' OR col2 LIKE '%Sample%' OR col3 LIKE '%Sample%' OR col4 LIKE '%Sample%'
You can also take help of the fulltext search provided by MySQL.
This link provides details on fulltext search, http://dev.mysql.com/doc/refman/5.0/en/fulltext-natural-language.html
You can do something like this, If there is change in table structure then you need to add/remove columns from the query.
SELECT * FROM myTable WHERE Concat(col1, '',col2, '', coln) LIKE '%searchKey%'
Related
I was wondering if someone could help me with this. I have a form in Access that has a combo box to look up an item number. Right now the Row Source is something like
Select ItemCode, ItemDescription From MyTable Where ItemType = 'G'
I would like to change it to allow a wild card. So right now if you type in C* it will not work. How do I make that work? Do I just update it to say
Select ItemCode From MyTable Where ItemType = 'G'
or is there more to it?
It's calling a query which is calling a SQL database. It's passing parameters like the date and itemcode.
It's calling a pass-through query to the database That looks like this...
pt_JobAnalysis '12/26/2017' , '01/26/2018' , '' , '10516' , ''
sSQL = sProcedureName & " '" & Me.txtFrom & "' , '" & Me.txtTo & "'"
Thank you so much.
Something like this should do it:
"Like '*'"
OR . . .
If IsNull(Me.txtFirstName.Value) Then
strFirstName = "Like '*'"
Else
SelectCase Me.fraFirstName.Value
Case 1
strFirstName = "Like '" & Me.txtFirstName.Value & "*'"
Case 2
strFirstName = "Like '*" & Me.txtFirstName.Value & "*'"
Case 3
strFirstName = "Like '*" & Me.txtFirstName.Value & "'"
Case 4
strFirstName = "= '" & Me.txtFirstName.Value & "'"
End Select
End If
FINALLY . . .
The link below should help you a lot!
https://www.fontstuff.com/access/acctut19.htm
I have a table named (PostOff) which has a field called PAddress
and a form textbox named CAddress.
I'm trying to write code to select a row where the PAddress partially matches the form textbox CAddress. To be more specific, select the row if CAddress contains the value of PAddress table field.
The code I use only works if PAddress perfectly matches CAddress:
("SELECT * FROM PostOff WHERE [PostOff.PAddress] Like '" & Me.CAddress & "'")
I guess I need something like:
("SELECT * FROM PostOff WHERE "*[PostOff.PAddress]*" Like '" & Me.CAddress & "'")
but it is not working.
[MS Access solution]
Accordingly to your comment...
If you want to find only town word in PAdress field, you'd use IN operator.
sQry = "SELECT * FROM PostOff WHERE [PostOff.PAddress] IN (" & GetListOfWords(Me.CAddress) & ")"
A GetListOfWords function may look like:
Function GetListOfWords(ByVal sInputString As String) As String
Dim words() As String
Dim sResult As String
Dim i As Integer
words = Split(Replace(sInputString, ",", ""), " ")
For i = 0 To UBound(words)
sResult = sResult & "'" & words(i) & "',"
Next
GetListOfWords = Left(sResult, Len(sResult) - 1)
End Function
Above function returns: '123','St','Town','City','US'
If I understand correctly, you need to use % to match anything in front or back of the string
("SELECT * FROM PostOff WHERE [PostOff.PAddress] Like '%" & Me.CAddress & "%'")
So all I'd like to do is add two queries into a table called tmpGroupSearch. I'm not sure how to do it wit out creating a record set and looping through every record and individually adding them in. I'm thinking there is a much easier way to do this I just can't find the proper structure.
Here are my queries:
"SELECT tblGroupHeader.GroupName" _
& ", tblGroupHeader.GroupNum" _
& ", tblAlsoKnown.AlsoKnown" _
& " FROM tblGroupHeader" _
& " LEFT JOIN tblAlsoKnown ON tblGroupHeader.GroupNum = tblAlsoKnown.GroupNum" _
& " WHERE tblGroupHeader.GroupName like '" & txtgroupSearch.Value & "*'" _
& " OR tblGroupHeader.GroupNum like '" & txtgroupSearch.Value & "*';"
"Select * FROM tblActionLog WHERE AlsoKnown LIKE '" & txtgroupSearch.Value & "*';"
You can follow an INSERT INTO clause with a list of values, like #Asad shows, or also a SELECT query. Do yourself a favor and always list the field names in your SQL or you are just creating time bombs for the future.
Dim strSelect1 As String
Dim strSelect2 As String
strSelect1 = "SELECT tblGroupHeader.GroupName" _
& ", tblGroupHeader.GroupNum" _
& ", tblAlsoKnown.AlsoKnown" _
& " FROM tblGroupHeader" _
& " LEFT JOIN tblAlsoKnown ON tblGroupHeader.GroupNum = tblAlsoKnown.GroupNum" _
& " WHERE tblGroupHeader.GroupName like '" & txtgroupSearch.Value & "*'" _
& " OR tblGroupHeader.GroupNum like '" & txtgroupSearch.Value & "*';"
strSelect2 = "Select * FROM tblActionLog WHERE AlsoKnown LIKE '" & txtgroupSearch.Value & "*';"
Dim strInsert1 As String
Dim strInsert2 As String
strInsert1 = "INSERT INTO tmpGroupSearch (GroupName, GroupNum, AlsoKnown) " & strSelect1
'the next version is valid SQL, but *dangerous* because field names are not enumerated
strInsert2 = "INSERT INTO tmpGroupSearch " & strSelect2
It is possible to write the INSERT INTO statement in two forms.
The first form does not specify the column names where the data will be inserted, only their values:
INSERT INTO table_name
VALUES (value1,value2,value3,...);
The second form specifies both the column names and the values to be inserted:
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
Test this parameter query in the Access query designer and adjust as needed so that it returns the row set you expect for the pSearchText parameter value you supply.
PARAMETERS pSearchText Text ( 255 );
SELECT gh.GroupName, gh.GroupNum, ak.AlsoKnown
FROM
tblGroupHeader AS gh
LEFT JOIN tblAlsoKnown AS ak
ON gh.GroupNum = ak.GroupNum
WHERE
gh.GroupName Like "'" & pSearchText & "*'"
OR gh.GroupNum Like "'" & pSearchText & "*'"
UNION ALL
SELECT al.GroupName, al.GroupNum, al.AlsoKnown
FROM tblActionLog AS al
WHERE al.AlsoKnown Like "'" & pSearchText & "*'"
Once you have it returning the correct data, convert it to an INSERT query by changing the beginning of the statement to this ...
PARAMETERS pSearchText Text ( 255 );
INSERT INTO tmpGroupSearch (GroupName, GroupNum, AlsoKnown)
SELECT gh.GroupName, gh.GroupNum, ak.AlsoKnown
... and the rest
Save that query as qryGroupSearchAppend. Then you can execute that query from your VBA code:
Dim qdf As DAO.QueryDef
Set qdf = CurrentDb.QueryDefs("qryGroupSearchAppend")
qdf.Parameters("pSearchText").Value = Me.txtgroupSearch.Value
qdf.Execute dbFailOnError
i have this search function to search for Firstname Middlename individually with their own CheckBox, this thing came to my mind if i want to search for Firstname and Middlename together in one Textbox with space of course and tried to use SELECT statement with CONCAT, but it didn't work.
it looks like this one:
If CheckBox1.Checked And CheckBox2.Checked Then
SQL = "SELECT * from student where CONCAT(fname,"" "", mname) like '%" & TextBox1.Text & "%'"
End if
Try this:
If CheckBox1.Checked And CheckBox2.Checked Then
SQL = "SELECT * from student where CONCAT(fname,' ', mname) like '%" & TextBox1.Text & "%'"
End if
EDIT
SELECT * FROM student WHERE MATCH (fname, mname) AGAINST ('+" & TextBox1.Text & "* +" & TextBox1.Text & "*' IN BOOLEAN MODE)
Might be this will give you the desired result
So I am writing a string in VBA. It needs to use 2 different numeric variables from a form that the user clicked on. My code works great if I only use 1 variable, but I can't get the syntax correct for 2 variables. Here is what I originally wrote that works fine.
stringSQL = "select table1.field1, table2.field2 from table1 where table1.flag='flag' and
table1.weight = " & [Forms]![myform]![weight]
When I try to add age to the string (I want to use age and weight from myform) I write this which does not work -
stringSQL = "select table1.field1, table2.field2 from table1 where table1.flag='flag' and
table1.age= " & [Forms]![myform]![age] & " and " & table1.weight= " &
[Forms]![myform]![weight]
stringSQL = "select table1.field1, table2.field2 " & _
" from table1 where table1.flag='flag' " & _
" and table1.age= " & [Forms]![myform]![age] & _
" and table1.weight= " & [Forms]![myform]![weight]