I've search and tried a lot of procedure but did not work.
CREATE PROCEDURE did not work, SET #variable is not acceptable and a couple more codes. This is my last "query" code that still didn't work.
qry = "select * from employeefile where empfname LIKE '%''" + input + "''%'"
empfname is a name of an employee from table employeefile that possibly consists of three words. The input could be the first word, second word, third word or the entire words.
when i tried to input any word within the name, the program will still prompt, "no records found." when i tried to change the query into
qry = "select * from employeefile where empfname LIKE '%existingname%'"
and my input is "existingname", the program runs just as i want it to.
This code is one of those that i seached but still didn't work.
T-SQL and the WHERE LIKE %Parameter% clause
How to use like clause in MySQL 5.0 Statement
T-SQL and the WHERE LIKE %Parameter% clause
The problem here is when i use a variable... i probably get the wrong way of using it into query. Please help me. I am new here by the way.
If I'm understanding your question correctly:
Dim command As New MySqlCommand("SELECT * FROM emplyeefile WHERE empfname LIKE '%' + #empfname + '%'", connection)
command.Parameters.AddWithValue("#empfname", input)
or:
Dim command As New MySqlCommand("SELECT * FROM emplyeefile WHERE empfname LIKE #empfname", connection)
command.Parameters.AddWithValue("#empfname", "%" & input & "%")
You have to concatenate the wildcards with the input text, either in your SQL code or your VB code.
This is the correct syntax: LIKE '%blah%', but this code does not put the quotes outside the %s: LIKE '%''" + input + "''%'".
i got the answer. it turns out that i just overdid the single quote. it must be written this way:
qry = "select * from employeefile where empfname LIKE '%" + input + "%'"
So I'm starting to learn some vb.net, and I have the following mysql statement to get a distinct count for a column in one of my tables. It's been giving me fits with syntax errors, but pretty much everything I've read suggests what I have should be correct. Also, I took it into MySQL Workbench and it works fine, for whatever reason it just refuses to work in my code.
Dim sRetrieve As String
Dim numvals As OdbcDataReader
sRetrieve = "SELECT COUNT (DISTINCT defect_code) FROM daily_data WHERE MONTH(date)=" & select_month & " AND YEAR(date)=" & select_year
Dim query_exe As New Odbc.OdbcCommand(sRetrieve, cn)
numvals = query_exe.ExecuteReader()
I've been banging my head against this for awhile now, so any help telling me what's wrong with that mysql statement would be greatly appreciated.
Figured it out, the space between COUNT and the first ( was doing it. Such a hilariously small thing for it to have held me up for so long, but I guess that's how it goes. Thanks for the help, everybody.
MONTH(date) in mysql returns string. So you need to enclose with ' '.
sRetrieve = "SELECT COUNT (DISTINCT defect_code) FROM daily_data WHERE MONTH(date)='" & select_month & "' AND YEAR(date)=" & select_year;
For example, in sql
all ` should be replaced with `` right?
Well, is there a function built in by vb.net that does that sort of thing already?
That way I do not have to encode it.
By the way, I do not access sql database directly. Basically I am creating a text file and that text file contains raw sql statements. Most of the answers deal with accessing sql data directly.
I don't think so as I think the only case where something like this would be relevant is if you were doing inline SQL Commands without parameters.
This has a risk of SQL Injection, and therefore you should create commands like this:
Dim cmd As New SqlCommand("UPDATE [TableA] SET ColumnA=#ColumnA WHERE ID=#ID", Conn)
cmd.Parameters.Add("#ColumnA", SqlDbType.NVarChar).Value = txtColumnA.Text
cmd.Parameters.Add("#ID", SqlDbType.Int).Value = ID
cmd.ExecuteNonQuery()
Dont try and do this! I know you are trying to avoid SQL Injection so you are to be commended for thinking about security. However rolling your own sanitisation routine is something that is easy to get wrong.
Use parameters in your query along the lines of
cmd.CommandText = "select * from customer where id=?id";
cmd.Parameters.AddWithValue("?id",CustomerIDValue);
If you are using a string then you'll be using " in your code so you won't need to escape these characters.
Dim mySql As String = "SELECT `MyColumn` FROM `Table`"
I've been having this problem with MySQL for the past month. It's not a big problem, but it's pretty annoying.
query = "SELECT * FROM MESSAGE_TEMPLATES WHERE MESSAGE_SEND_DATE = '" & _date & "'"
This simple line works fine when I pass it to the dataReader in MySQL. However:
query = "SELECT * FROM MESSAGE_TEMPLATES" & _
" WHERE MESSAGE_SEND_DATE = '" & _date & "'"
This does not work, even though intellisense shows them both to have the same value. MySQL complains about having an error in the syntax. You can imagine this becomes more problematic for larger statements that take up several lines. What exactly am I missing here?
Can you print out each string to a console or a log and look for any random white space or involuntarily-included control characters?
It should work - can't be an MySQL thing as a strings a string. If it persists consider using XML syntax to allow you to do the multi-line thing:
Dim query = <sql><![CDATA[
SELECT * FROM MESSAGE_TEMPLATES
WHERE MESSAGE_SEND_DATE = :Date
]]></sql>.Value
Also note that you should be using parameterized queries as they are more secure easier to read and faster.
I have the following 'set recordset' line that I cannot get working. The parameters seem correct according to all available help I can find on the subject.
The error displays :
"Run-time error '3061'. Too few parameters. Expected 1."
Here is the line of code:
Set rs = dbs.OpenRecordset("SELECT Centre_X, Centre_Y FROM [qry_all_details]
WHERE ID = " & siteID & ";", dbOpenSnapshot)
Where rs is the recordset (Dim rs As Recordset) and dbs = CurrentDb()
Any help would be appreciated.
I have tried removing the WHERE cause with no effect, and also using single quotes between double quotes, but no joy.
Many thanks.
"Run-time error '3061'. Too few parameters. Expected 1."
I believe this happens when the field name(s) in your sql query do not match the table field name(s), i.e. a field name in the query is wrong or perhaps the table is missing the field altogether.
you have:
WHERE ID = " & siteID & ";", dbOpenSnapshot)
you need:
WHERE ID = "'" & siteID & "';", dbOpenSnapshot)
Note the extra quotations ('). . . this kills me everytime
Edit: added missing double quote
(For those who read all answers). My case was simply the fact that I created a SQL expression using the format Forms!Table!Control. That format is Ok within a query, but DAO doesn't recognize it. I'm surprised that nobody commented this.
This doesn't work:
Dim rs As DAO.Recordset, strSQL As String
strSQL = "SELECT * FROM Table1 WHERE Name = Forms!Table!Control;"
Set rs = CurrentDb.OpenRecordset(strSQL)
This is Ok:
Dim rs As DAO.Recordset, strSQL, val As String
val = Forms!Table!Control
strSQL = "SELECT * FROM Table1 WHERE Name = '" & val & "';"
Set rs = CurrentDb.OpenRecordset(strSQL)
My problem was also solved by the Single Quotes around the variable name
I got the same error message before.
in my case, it was caused by type casting.
check if siteID is a string, if it is you must add simple quotes.
hope it will help you.
My problem turned out to be, I had altered a table to add a column called Char.
As this is a reserved word in MS Access it needed square brakcets (Single or double quote are no good) in order for the alter statement to work before I could then update the newly created column.
Make sure [qry_all_details] exists and is runnable. I suspect it or any query it uses, is missing the parameter.
I got the same error with something like:
Set rs = dbs.OpenRecordset _
( _
"SELECT Field1, Field2, FieldN " _
& "FROM Query1 " _
& "WHERE Query2.Field1 = """ & Value1 & """;" _
, dbOpenSnapshot _
)
I fixed the error by replacing "Query1" with "Query2"
In my case, I got this error when I tried to use in a query a new column, which I added to MySQL table (linked to MS Access), but didn't refresh it inside MS Access.
To refresh a linked remote table:
Open "Linked Table Manager" ("External Data" tab on ribbon);
Select a checkbox near the table you want to refresh;
Press "OK" button.
In my case I was receiving this error when running a query from VBA with this command:
CurrentDb.Execute "qryName"
Double clicking on the query to execute it, worked fine, no error.
Changing the code to the following also worked fine, no error.
DoCmd.OpenQuery "qryName"
Hope this helps someone else who is unexpectedly getting this error.
If someone could explain why the first command caused the error I'd love to know.
Does the query has more than the parameter siteID, becouse if you want to run the query one parameter still isn't filled witch gives you the error
In my case, I had simply changed the way I created a table and inadvertently changed the field name I tried to query. Make sure the field names you reference in the query actually exist in the table/query you are querying.
This Message is also possible to pop up, if there is a typo in the fields on which you define a join
Thanks for John Doe's solution that helped a lot. Mine is very similar with some difference, using TempVars
Instead of :
strSQL = "SELECT * FROM Table1 WHERE Name = Forms!Table!Control;"
I used:
strSQL = "SELECT * FROM Query1" , Query1 being common for other usage
Query1 as:
"Select Field1, Field2 from Table1 where Id= [TempVars]![MyVar]
And similarly, removing [TempVars]![MyVar] from view solved the problem.
In My case I had an INSERT INTO TableA (_ ,_ ,_) SELECT _ ,_ ,_ from TableB, a run-time error of 33061 was a field error. As #david mentioned. Either it was a field error: what I wrote in SQL statement as a column name did not match the column names in the actual access tables, for TableA or TableB.
I also have an error like #DATS but it was a run-time error 3464.