Hello I'm trying to display data in vb.net using MySQL syntax here is my Mysql syntax
SELECT COUNT(status) as 'Number of Grade School for the Month of January'
FROM blhtraining.userinfo
Where survey_at='Talisay'
and status='College' and Month(member_since)='1' and
Year(member_since)='2021'
And this code works in Mysql but when i modify it like this in vb.net
Dim count_gradeSchool1 As String = "Select Case COUNT(status) As 'Members'
From training.userinfo
Where survey_at='" & txtmonthlylocation.Text & "'
And status ='College'
And Month(member_since)='" & monthly_reports & "'
And YEAR(member_since)='" & txtmyear.Text & "'
And Day(member_since)='11'"
da = New MySqlDataAdapter(count_gradeSchool1, mycon)
dt = New DataTable()
da.Fill(dt)
lblgs1.Text = dt.Rows(0)("Members")
I recieved this error
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'As 'Members'
From training.u' at line 1
I'm sure the syntax is correct is it the variable declared?
Your problems probably came about because you pasted the SQL into your code without starting a string first, so VB saw "select" and helped you out by adding "case". So, here is code that...
...has fixed SQL syntax
...uses parameters. Always use parameters. You've no idea how many times a day I say this, trying to stem the tide of future SQL injection hacks. Writing code that doesn't use parameters will get you fired, or you'll have to live with the consequences of writing hack prone code on your conscience. Don't ever skip on using parameters in your SQLs, even if it's "only an app to track your grandma's record collection"
...doesn't call functions on columns in the where clause - don't do it; it's a huge waste of resources and kills opportunities to use indexes. Always, always try to leave table data alone, untransformed. In 99% of cases there is another way to write the query
...uses executescalar - you only want one value, pointless using an adapter/table for it
...doesn't use column alises with spaces in - as noted in the comments - don't do it; it's not the database's job to format your column names, it's the front end's job.
Dim count_gradeSchool1 As String = "Select COUNT(*) as c
FROM training.userinfo
Where survey_at = #loc
And status = 'College'
And member_since = #ms"
Using c = New MySqlCommand(count_gradeSchool1, mycon)
c.Parameters.AddWithValue("#loc", txtmonthlylocation.Text)
c.Parameters.AddWithValue("#ms", new Date(CInt(txtmyear.Text), CInt(monthly_reports), 11)
c.Connection.Open() 'if it's not already open
lblgs1.Text = c.ExecuteScalar().ToString()
End Using
So i'm doing this simple sql select from mysql database in my VBA code:
cmd.CommandText = "SELECT sum(operation_employee_execution_time) FROM employee_operation where employee_last_name like '" & sEmployee & "'"
and it is not working. If I do msgbox(cmd.CommandText) I see properly formatted SQL query:
Unfortunately result of that query is nothing (meaning like clause could not find a match), but if I hardcode the value of the variable like this:
cmd.CommandText = "SELECT sum(operation_employee_execution_time) FROM employee_operation where employee_last_name like 'Levkovic'"
It works perfectly...
Anyone could give me some advice here? I thought that this will be some sort of encoding problem but adding "CharacterSet=utf8;" to my connection string did not help (that column in db is utf8mb4_bin)
Thank you all for input, problem was that the Excel cell that was read to the variable sEmployee had some unicode characters.
Good morning SO,
I'm having a bit of trouble with this query. It works fine when I tested it in the Workbench, but VB is throwing an exception that the column "ccID" is unknown. Am I doing something wrong? My intent is to pull the ccID field and the difference between today and the date in field ccAuthorizedUseEnd.
Your assistance would be greatly appreciated, thanks.
HERE'S THE QUERY I TESTED IN THE MySQL WORKBENCH:
SELECT ccID, DATEDIFF(DATE(NOW()), ccAuthorizedUseEnd) AS Days
FROM accounting.cc_master
HERE'S THE SAME QUERY I'M USING IN VB.NET
Dim dbQuery2 As String = "SELECT ccID, DATEDIFF(DATE(NOW()), ccAuthorizedUseEnd) as Days "
You've forgotten your FROM clause in the VB.NET version:
Dim dbQuery2 As String = "SELECT ccID, DATEDIFF(DATE(NOW()), ccAuthorizedUseEnd) as Days FROM accounting.cc_master"
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.