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
Related
I have a textbox [ScanItem] on a Split Form that take two different barscan values, Works Really Slick!
Alphanumeric, Len 11
Numeric, Len 12
My Update Query works, but for only one WHERE Condition. I was hoping to use the Len function to combine the WHERE function's but can't get it to work.
Update Query for Alphanumeric Len 11 (Example: 019-WMD-001 ):
strSQL = "UPDATE [Location Table]" & _
"SET [Location]='" & Me.Location1.Value & "'" & _
"WHERE [Location Table].[Custom Label] = [ScanItem]"
Update Query for Numeric Len 12
"WHERE [Location Table].[Item ID] = [ScanItem]"
I understand the table spaces are a no-no but I do not have control over that. If this will not work, any other solution would be welcomed.
If I understand your question correctly you want to apply one WHERE clause evaluation if length of [ScanItem] = 11 (in which case use [Custom Label] in comparison) and another WHERE clause evaluation if length of [ScanItem] = 12 (in which case use [Item ID] in comparison). There are multiple ways to do this. I'm not addressing SQL injection issue--just the query logic, but you can always rewrite this using parameters (recommended best practice). You could write the query to use conditional logic or your vba could use the conditional logic (assuming there are only possible length conditions of 11 and 12). I'm a fan of scenario 2.
Scenario 1 (query uses conditional logic):
strSQL = "UPDATE [Location Table] " & _
"SET [Location]='" & Me.Location1.Value & "' " & _
"WHERE ([Location Table].[Custom Label] = '" & Me.ScanItem.Value & "' AND LEN('" & Me.ScanItem.Value & "') = 11) " & _
" OR ([Location Table].[Item ID] = '" & Me.ScanItem.Value & "' AND LEN('" & Me.ScanItem.Value & "') = 12)"
Scenario 2 (vba uses conditional logic--assuming only 2 possible lengths of 11 and not 11):
strSQL = "UPDATE [Location Table] " & _
"SET [Location]='" & Me.Location1.Value & "' " & _
"WHERE ([Location Table]." & IIF(Len(Me.ScanItem.Value)=11,"[Custom Label]","[Item ID]") & " = '" & Me.ScanItem.Value & "'"
For the first WHERE condition, you've handled it with string concatenation.
For the second one, however, you're just referring to the field.
A solution using string concatenation (at risk for SQL injection):
strSQL = "UPDATE [Location Table]" & _
" SET [Location]='" & Me.Location1.Value & "'" & _
" WHERE [Location Table].[Custom Label] = '" & Me.ScanItem.Value & "'"
A more proper solution using parameters:
strSQL = "UPDATE [Location Table]" & _
" SET [Location]= paramLocation" & _
" WHERE [Location Table].[Custom Label] = paramLabel"
Set qdf = CurrentDb.CreateQueryDef("", strSQL)
qdf.Parameters("paramLocation").Value = Me.Location1.Value
qdf.Parameters("paramLabel").Value = Me.ScanItem.Value
qdf.Execute
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
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%'
Hi guys I dont know if this makes sense but how can I query another query in VBA?
I will show with example below
This is my first query
strSQL1 = "SELECT DISTINCT SourceBank" _
& ", Fullname, FirstNames" _
& ", Surname, Company" _
& ", EmailAddress" _
& " FROM question" _
& " WHERE FirstNames = '" & strFirstNames & "'" _
Set rs = dbs.OpenRecordset(strSQL)
Then I want to do something like this. Query the first query
strSQL2 = "S"SELECT * from " & strSQL1
Set rs1 = dbs.OpenRecordset(strSQL)
I just want to know if this is possible and if not then what is the best way around it?
All I want to do is to be able to query another query/string/recordset.
thanks
You can do it almost like you've wrote:
strSQL2="SELECT * FROM (" & strSQL1 & ")"
but be sure not to include ; in strSQL1
upd, try:
strSQL2 = "SELECT Question.EmailAddress, SUBQUERY.EmailAddress &" _
& "FROM Question LEFT JOIN (" & strSQL1 & ") AS SUBQUERY ON Question.EmailAddress = SUBQUERY.EmailAddress"
OR just save sql1 into QueryDef (Query in ms access) and use it like source table.
Having problems getting a csv imported into a MYSQL database to update existing data. The importing new data works fine, just getting errors updating.
`CSVSerial = arrServiceList(0)
CSVAssetTag = arrServiceList(1)
CSVLocation = arrServiceList(2)
CSVRoom = arrServiceList(3)
CSVCheckedOutTo = arrServiceList(4)
commandText = "Update Inventory Set Room = CSVRoom, Location = CSVLocation, AssetTag = CSVAssetTag, CheckedOutTo = CSVCheckedOutTo where SerialNumber = CSVSerial"`
I've tried doing $CSVSerial, '$CSVSerial', 'CSVSerial', they don't seem to recognize any of them. If I do a
Msgbox (CSVSerial & "," & CSVRoom & "," & CSVLocation & "," & CSVAssettag & "," & CSVCheckedOutTo)
all the data is correct there. I'm sure it's something simple, just don't have alot of MySQL experience.
Thanks
Your query just uses the literal strings CSVRoom, CSVLocation, etc. rather than their values. The $CSVSerial won't work in VB - that's a PHP thing (plus some other languages). In addition, if these are string values they need to be surrounded by single quotes. Try something like this instead:
commandText = "Update Inventory Set Room = '" & CSVRoom & "', " & _
"Location = '" & CSVLocation & "', " & _
"AssetTag = '" & CSVAssetTag & "', " & _
"CheckedOutTo = '" & CSVCheckedOutTo & "' where SerialNumber = '" & CSVSerial & "'"
I've assumed all the columns are strings. If any are numbers you don't need the single quotes around them but then again they shouldn't hurt either.
Addendum: Followup question is how to update the columns only if they're not blank or null.
To test for blank or null, you can use a condition like this:
Room IS NULL OR Room = ''
... or like this:
COALESCE(Room, '') = ''
The second one is shorter so I'll go with that because the command lines are getting pretty long. The basic MySQL command goes like this:
UPDATE Inventory SET
Room = CASE WHEN COALESCE(Room, '') = '' THEN Room ELSE 'new value' END,
...
So if Room is '' or NULL it's set to its existing '' or NULL value. If not, it's set to the new value.
Here's how it looks in VBScript. This is untested because I don't have VBScript available.
commandText = "UPDATE Inventory SET " & _
"Room = CASE WHEN COALESCE(Room, '') = '' THEN Room ELSE '" & CSVRoom & "' END, " & _
"Location = CASE WHEN COALESCE(Location, '') = '' THEN Location ELSE '" & CSVLocation & "' END, " & _
"AssetTag = CASE WHEN COALESCE(AssetTag, '') = '' THEN AssetTag ELSE '" & CSVAssetTag & "' END, " & _
"CheckedOutTo = CASE WHEN COALESCE(CheckedOutTo, '') = '' THEN CheckedOutTo ELSE '" & CSVCheckedOutTo & "' END " & _
"WHERE SerialNumber = '" & CSVSerial & "'"