Parameterizing query says "Mixing named and unnamed parameters is not allowed." - mysql

I want to parameterized my query so instead of this code (This is a working code, I didn't put all the codes here because we have nothing to do with it.)
MySQL_Query = "SET #row_number = 0; " _
& "SELECT hardware_add " _
& "FROM (" _
& "SELECT " _
& "#row_number:=#row_number + 1 AS num, " _
& "hardware_add AS hardware_add " _
& "FROM teller_info " _
& "WHERE status = 'Disconnected'" _
& ") AS sub_query " _
& "WHERE num = " & counter & ";"
Console.WriteLine(MySQL_Query)
Dim MySQL_CMD As New MySqlCommand(MySQL_Query, MysqlConn)
MySQL_CMD.Connection.Open()
I changed it to this.
MySQL_Query = "SET #row_number = 0; " _
& "SELECT hardware_add " _
& "FROM (" _
& "SELECT " _
& "#row_number:=#row_number + 1 AS num, " _
& "hardware_add AS hardware_add " _
& "FROM teller_info " _
& "WHERE status = 'Disconnected'" _
& ") AS sub_query " _
& "WHERE num = ?;"
Console.WriteLine(MySQL_Query)
Dim MySQL_CMD As New MySqlCommand(MySQL_Query, MysqlConn)
MySQL_CMD.Connection.Open()
MySQL_CMD.Parameters.Add(New MySqlParameter("counter", counter))
The error says
MySql.Data.MySqlClient.MySqlException (0x80004005): Mixing named and unnamed parameters is not allowed.
My question is how can I properly parameterize that query?

You have to give a name to that param, doing so:
MySQL_Query = "SET #row_number = 0; " _
& "SELECT hardware_add " _
& "FROM (" _
& "SELECT " _
& "#row_number:=#row_number + 1 AS num, " _
& "hardware_add AS hardware_add " _
& "FROM teller_info " _
& "WHERE status = 'Disconnected'" _
& ") AS sub_query " _
& "WHERE num = #counter;"
Console.WriteLine(MySQL_Query)
Dim MySQL_CMD As New MySqlCommand(MySQL_Query, MysqlConn)
MySQL_CMD.Connection.Open()
MySQL_CMD.Parameters.Add(New MySqlParameter("#counter", counter))

Related

Syntax error in number using query expression in MS-Access

I got Syntax error in number using query expression? error while I run below code
CurrentDb.Execute "UPDATE StateBudget " & _
" Set S_ID='" & Me.cbState & "'" & _
", C_ID=" & Me.cbCategory & _
", Year='" & Me.cbYear & "'" & _
", 1=" & Me.Ctl1 & _
", 2=" & Me.Ctl2 & _
", 3=" & Me.Ctl3 & _
" WHERE S_ID = """ & _
DLookup("ID", "States", "State='" & _
Me.subformStateBudget.Form.Recordset.Fields("State") & "'") & """"
S_ID and Year are Text fields, C_ID and the rest 1,2,3 are Number fields.
If there anything I missed? I am a beginner in programming so...
Probably one of number Me.* fields is null, so SQL will have syntax error. Use Nz function for such kind arguments. For debugging create SQL in string variable before executing and dump content of this variable to Immediate window using Debug.Print. You will see the problem easily.
Dim strSQL
strSQL = "UPDATE StateBudget " & _
" Set S_ID='" & Me.cbState & "'" & _
", C_ID=" & Nz(Me.cbCategory, 0) & _
", Year='" & Me.cbYear & "'" & _
", 1=" & Nz(Me.Ctl1, 0) & _
", 2=" & Nz(Me.Ctl2, 0) & _
", 3=" & Nz(Me.Ctl3, 0) & _
" WHERE S_ID = """ & _
DLookup("ID", "States", "State='" & _
Me.subformStateBudget.Form.Recordset.Fields("State") & "'") & """"
Debug.Print strSQL
CurrentDb.Execute strSQL, dbFailOnError

Number format of a query in access vba

I created a query in access using VBA, and I want to change the number format of the last two columns to * #.##0,00;* (#.##0,00);* -00
I didn't find how to do it. Here's what I did:
Sub orcMensal(periodo As String)
Dim rs As QueryDef
Dim SQL1 As String
Dim ano As String
Dim tabelaCA As String
Dim tabelaRealizado As String
tabelaCA = "[ORCAMENTO_CA_20" & Right(periodo, 2) & "]"
tabelaRealizado = "[" & periodo & "_MA]"
SQL1 = "SELECT " & tabelaCA & ".coger AS [Codigo Coger], " _
& tabelaCA & ".[Descricao], " _
& tabelaCA & ".[" & periodo & "] AS [Orcamento], " _
& tabelaRealizado & ".[REALIZADO] AS [Realizado], " _
& "(" & tabelaRealizado & ".[REALIZADO] - " & tabelaCA & ".[" & periodo & "]) AS [Diferenca] " _
& "FROM " & tabelaCA _
& " INNER JOIN " & tabelaRealizado & " ON" _
& " (" & tabelaCA & ".[coger] = " & tabelaRealizado & ".[COD_ES] AND " _
& tabelaCA & ".[Titular] = " & tabelaRealizado & ".[TITULAR]);"
Debug.Print SQL1
Set rs = CurrentDb.CreateQueryDef(periodo, SQL1)
DoCmd.OpenQuery rs.Name, , acReadOnly
End Sub
I used the Format function in the SQL query, like #Wayne G. Dunn commented. The format that I wanted was
1) 1000000,5023 >>>> 1.000.000,50;
2) -5200000,7833 >>>> (5.200.000,78);
3) 0 >>>> -
So I used '#,##0.00; (#,##0.00); -' in the format function, here's how the query looks:
SQL1 = "SELECT " & tabelaCA & ".coger AS [Codigo Coger], " _
& tabelaCA & ".[Descricao], " _
& "Format(" & tabelaCA & ".[" & periodo & "], '#,##0.00; (#,##0.00); -') AS [Orcamento], FORMAT(" _
& tabelaRealizado & ".[REALIZADO], '#,##0.00; (#,##0.00); -') AS [Realizado], " _
& "Format((" & tabelaRealizado & ".[REALIZADO] - " & tabelaCA & ".[" & periodo & "]), '#,##0.00; (#,##0.00); -') AS [Diferenca] " _
& "FROM " & tabelaCA _
& " LEFT JOIN " & tabelaRealizado & " ON" _
& " (" & tabelaCA & ".[coger] = " & tabelaRealizado & ".[COD_ES] AND " _
& tabelaCA & ".[Titular] = " & tabelaRealizado & ".[TITULAR])" _
& "WHERE " & tabelaCA & ".coger <> '';"

Can't run query in Access via vba with a long definition

I got a very long query definition that works when it run it in the Access GUI. I don't want to save it as a Query object that an end user can accidently run so I want to run it via VBA. When I tried to run it using DoCmd.RunSQL it throws an error:
"A RunSQL action requires an argument consisting of an SQL statement."
When I try it with mydatabase.Execute I get:
"The Microsoft Access engine cannot find the input table or query ''. Make sure it exists and that its name is spelled correctly."
I did a Debug.Print and copied the output to an Query in the Access GUI, after cleaning up the line brakes the Immediate window added the query ran fine. The query string is 5,293 characters long. Any help would be appreciated.
Dim dBase As Database
Set dBase = CurrentDb()
Dim stringSQL_3 As String
stringSQL_3 = _
"INSERT INTO Measurement_Period_History ( EMPLOYEE_NUMBER, PERIOD_START, PERIOD_END, DATE_REVIEWED, AVERAGE_HOURS, PERIOD_YEAR, PERIOD_TYPE_ID, FIRST_PAY_WEEK, LAST_PAY_WEEK ) " & _
"SELECT FinalData.EMPLOYEE_NUMBER " & _
",FinalData.START_PERIOD " & _
",FinalData.END_PERIOD " & _
",Now() AS DATE_REVIEWED " & _
",IIF(FinalData.FLSA = 2, 40.00, SUM(FinalData.HOURS) / 52) AS AVERAGE_HOURS " & _
",Cint(FinalData.PERIOD_YEAR) AS PERIOD_YEAR " & _
",FinalData.PERIOD_TYPE_ID " & _
",FinalData.FIRST_PAY_WEEK " & _
",FinalData.LAST_PAY_WEEK "
stringSQL_3 = stringSQL_3 & _
"FROM ( " & _
"SELECT MeasurementData.EMPLOYEE_NUMBER " & _
",MeasurementData.FLSA " & _
",MeasurementData.STARTING_HIRE_DATE " & _
",MeasurementData.VARIABLE_EMPLOYEE " & _
",MeasurementData.START_PERIOD " & _
",MeasurementData.END_PERIOD " & _
",MeasurementData.PERIOD_TYPE_ID " & _
",MeasurementData.PRIOR_PERIOD_START " & _
",MeasurementData.PERIOD_YEAR " & _
",MeasurementData.FIRST_PAY_WEEK " & _
",MeasurementData.LAST_PAY_WEEK " & _
",IIf(IsNull(Payroll_History.HOURS), 0, Payroll_History.HOURS) AS HOURS "
stringSQL_3 = stringSQL_3 & _
"FROM Payroll_History " & _
"RIGHT JOIN ( " & _
"SELECT BaseData.EMPLOYEE_NUMBER " & _
",BaseData.FLSA " & _
",BaseData.STARTING_HIRE_DATE " & _
",BaseData.VARIABLE_EMPLOYEE " & _
",BaseData.START_PERIOD " & _
",BaseData.END_PERIOD " & _
",BaseData.PERIOD_TYPE_ID " & _
",BaseData.PRIOR_PERIOD_START " & _
",BaseData.PERIOD_YEAR " & _
",DateAdd('d', 3, DateAdd('ww', - 52, DateAdd('d', - 2, DateAdd('ww', DateDiff('ww', 0, DateAdd('ww', IIf(DatePart('w', " & _
"BaseData.END_PERIOD) >= 5, 0, - 1), BaseData.END_PERIOD)), 0)))) AS FIRST_PAY_WEEK " & _
",DateAdd('d', - 2, DateAdd('ww', DateDiff('ww', 0, DateAdd('ww', IIf(DatePart('w', BaseData.END_PERIOD) >= 5, 0, - 1), " & _
"BaseData.END_PERIOD)), 0)) AS LAST_PAY_WEEK "
stringSQL_3 = stringSQL_3 & _
"FROM ( " & _
"SELECT ElgEmployees.EMPLOYEE_NUMBER " & _
",ElgEmployees.FLSA " & _
",ElgEmployees.STARTING_HIRE_DATE " & _
",ElgEmployees.VARIABLE_EMPLOYEE " & _
",IIF(ElgEmployees.VARIABLE_EMPLOYEE = No, DateSerial(" & DatePickerYear & " - 1, 4, 1), " & _
"IIF(PeriodInfo.PRIOR_PERIOD_START IS NOT NULL, DateSerial(" & DatePickerYear & " - 1, DatePart('m', " & _
"PeriodInfo.PRIOR_PERIOD_START), 1), DateSerial(" & DatePickerYear & " - 1, DatePart('m', ElgEmployees.STARTING_HIRE_DATE), 1))) AS START_PERIOD " & _
",IIF(ElgEmployees.VARIABLE_EMPLOYEE = No, DateAdd('d', - 1, DateSerial(" & DatePickerYear & ", 4, 1)), DateAdd('d', - 1, " & _
"IIF(PeriodInfo.PRIOR_PERIOD_START IS NOT NULL, DateSerial(" & DatePickerYear & ", DatePart('m', PeriodInfo.PRIOR_PERIOD_START), 1), " & _
"DateSerial(" & DatePickerYear & ", DatePart('m', ElgEmployees.STARTING_HIRE_DATE), 1)))) AS END_PERIOD " & _
"," & DatePickerYear & " AS PERIOD_YEAR " & _
",PeriodInfo.PERIOD_TYPE_ID " & _
",PeriodInfo.PRIOR_PERIOD_START "
stringSQL_3 = stringSQL_3 & _
"FROM ( " & _
"SELECT Employee.EMPLOYEE_NUMBER " & _
",Employee.FLSA " & _
",Employee.VARIABLE_EMPLOYEE " & _
",IIf(Day(Employee.CURRENT_HIRE_DATE) = 1, Employee.CURRENT_HIRE_DATE, DateSerial(DatePart('yyyy', DateAdd('m', 1, " & _
"Employee.CURRENT_HIRE_DATE)), DatePart('m', DateAdd('m', 1, Employee.CURRENT_HIRE_DATE)), 1)) AS STARTING_HIRE_DATE "
stringSQL_3 = stringSQL_3 & _
"FROM Employee " & _
"WHERE ( " & _
"((Employee.EMPLOYEE_NUMBER)" & EmployeeNumberText & ") " & _
"AND ((IIf(Day(Employee.CURRENT_HIRE_DATE) = 1, Employee.CURRENT_HIRE_DATE, DateSerial(DatePart('yyyy', DateAdd('m', 1, " & _
"Employee.CURRENT_HIRE_DATE)), DatePart('m', DateAdd('m', 1, Employee.CURRENT_HIRE_DATE)), 1))) < DateAdd('y', - 1, DATE ())) " & _
"AND ((Employee.CURRENT_TERMINATION_DATE) IS NULL) " & _
") " & _
") AS ElgEmployees " & _
"INNER JOIN ( " & _
"SELECT Employee.EMPLOYEE_NUMBER " & _
",IIF(Measurement_Period_History.PERIOD_YEAR <> " & DatePickerYear & " - 1, NULL, Measurement_Period_History.PERIOD_YEAR) AS PRIOR_PERIOD_YEAR " & _
",IIF(Measurement_Period_History.PERIOD_YEAR <> " & DatePickerYear & " - 1, IIF(Employee.VARIABLE_EMPLOYEE = No, 2, 0), " & _
"IIF(Employee.VARIABLE_EMPLOYEE = No, 2, IIF(Measurement_Period_History.PERIOD_END < Employee.CURRENT_HIRE_DATE, 0, " & _
"IIF(Measurement_Period_History.AVERAGE_HOURS >= 30, 1, 0)))) AS PERIOD_TYPE_ID " & _
",IIF(Measurement_Period_History.PERIOD_YEAR <> " & DatePickerYear & " - 1, NULL, IIF(Measurement_Period_History.PERIOD_END < " & _
"Employee.CURRENT_HIRE_DATE, NULL, Measurement_Period_History.PERIOD_END)) AS PRIOR_PERIOD_END " & _
",IIF(Measurement_Period_History.PERIOD_YEAR <> " & DatePickerYear & " - 1, NULL, IIF(Measurement_Period_History.PERIOD_END < " & _
"Employee.CURRENT_HIRE_DATE, NULL, Measurement_Period_History.PERIOD_START)) AS PRIOR_PERIOD_START "
stringSQL_3 = stringSQL_3 & _
"FROM Measurement_Period_History " & _
"RIGHT JOIN Employee ON Measurement_Period_History.EMPLOYEE_NUMBER = Employee.EMPLOYEE_NUMBER " & _
"WHERE ((Employee.CURRENT_TERMINATION_DATE) Is Null) AND (Employee.EMPLOYEE_NUMBER" & EmployeeNumberText & ") " & _
"GROUP BY Employee.EMPLOYEE_NUMBER " & _
",IIF(Measurement_Period_History.PERIOD_YEAR <> " & DatePickerYear & " - 1, NULL, Measurement_Period_History.PERIOD_YEAR) " & _
",IIF(Measurement_Period_History.PERIOD_YEAR <> " & DatePickerYear & " - 1, IIF(Employee.VARIABLE_EMPLOYEE = No, 2, 0), " & _
"IIF(Employee.VARIABLE_EMPLOYEE = No, 2, IIF(Measurement_Period_History.PERIOD_END < Employee.CURRENT_HIRE_DATE, 0, " & _
"IIF(Measurement_Period_History.AVERAGE_HOURS >= 30, 1, 0)))) " & _
",IIF(Measurement_Period_History.PERIOD_YEAR <> " & DatePickerYear & " - 1, NULL, IIF(Measurement_Period_History.PERIOD_END < " & _
"Employee.CURRENT_HIRE_DATE, NULL, Measurement_Period_History.PERIOD_END)) " & _
",IIF(Measurement_Period_History.PERIOD_YEAR <> " & DatePickerYear & " - 1, NULL, IIF(Measurement_Period_History.PERIOD_END < " & _
"Employee.CURRENT_HIRE_DATE, NULL, Measurement_Period_History.PERIOD_START)) " & _
") AS PeriodInfo ON ElgEmployees.EMPLOYEE_NUMBER = PeriodInfo.EMPLOYEE_NUMBER " & _
") AS BaseData " & _
") AS MeasurementData ON Payroll_History.EMPLOYEE_NUMBER = MeasurementData.EMPLOYEE_NUMBER " & _
"WHERE ((Payroll_History.PAY_DATE) Is Null) " & _
"OR ( " & _
"(Payroll_History.PAY_DATE) >= MeasurementData.FIRST_PAY_WEEK " & _
"AND (Payroll_History.PAY_DATE) <= MeasurementData.LAST_PAY_WEEK " & _
") " & _
") AS FinalData "
stringSQL_3 = stringSQL_3 & _
"WHERE LAST_PAY_WEEK < Now() " & _
"GROUP BY EMPLOYEE_NUMBER " & _
",FLSA " & _
",STARTING_HIRE_DATE " & _
",VARIABLE_EMPLOYEE " & _
",START_PERIOD " & _
",END_PERIOD " & _
",PERIOD_TYPE_ID " & _
",PRIOR_PERIOD_START " & _
",PERIOD_YEAR " & _
",FIRST_PAY_WEEK " & _
",LAST_PAY_WEEK "
Debug.Print stringSQL_3
Debug.Print Len(stringSQL_3)
dBase.Execute string_3
Isn't your SQL in stringSQL_3? What's in the other string (string_3)? That might the issue, you are trying to execute the wrong string?

I'm trying to UPDATE my table in Access VBA

If Me.dcBox & "" = "" Then
CurrentDb.Execute "UPDATE worklogData " & _
" SET [Submitter] = '" & Me.subBox & "'" & _
", [Section] = '" & Me.secBox & "'" & _
", [Received By] = '" & Me.rbBox & "'" & _
", [Date Received] = """ & _
", [Serial Number] = '" & Me.snBox & "'" & _
", [Problem] = '" & Me.probBox & "'" & _
", [Computer Name] = '" & Me.cnBox & "'" & _
", [MAC] = '" & Me.macBox & "'" & _
", [Solution] = '" & Me.solBox & "'" & _
", [Completed By] = '" & Me.cbBox & "'" & _
", [Date Completed] = """ & _
" WHERE [Order] = " & Me.orderBox.Tag & ""
Else
CurrentDb.Execute "UPDATE worklogData " & _
" SET [Submitter] = '" & Me.subBox & "'" & _
", [Section] = '" & Me.secBox & "'" & _
", [Received By] = '" & Me.rbBox & "'" & _
", [Date Received] = #" & Me.drBox & "#" & _
", [Serial Number] = '" & Me.snBox & "'" & _
", [Problem] = '" & Me.probBox & "'" & _
", [Computer Name] = '" & Me.cnBox & "'" & _
", [MAC] = '" & Me.macBox & "'" & _
", [Solution] = '" & Me.solBox & "'" & _
", [Completed By] = '" & Me.cbBox & "'" & _
", [Date Completed] = #" & Me.dcBox & "#" & _
" WHERE [Order] = " & Me.orderBox.Tag & ""
End If
I'm trying to UPDATE my table in Access VBA, but I found it causes Runtime Error: 3075 'Syntax error in date in query expression '#' when I try to UPDATE [Date Completed] field from what was blank previously.
And I've set Me.probBox as some text value which was previously blank, but [Problem] field doesn't be updated. What is surprising is there is no error coming out for this one.
Try this
CurrentDb.Execute "UPDATE worklogData " & _
" SET [Submitter] = '" & Me.subBox & "'" & _
", [Section] = '" & Me.secBox & "'" & _
", [Received By] = '" & Me.rbBox & "'" & _
", [Date Received] = """"" & _
", [Serial Number] = '" & Me.snBox & "'" & _
", [Problem] = '" & Me.probBox & "'" & _
", [Computer Name] = '" & Me.cnBox & "'" & _
", [MAC] = '" & Me.macBox & "'" & _
", [Solution] = '" & Me.solBox & "'" & _
", [Completed By] = '" & Me.cbBox & "'" & _
", [Date Completed] = """"" & _
" WHERE [Order] = " & Me.orderBox.Tag & ""

problem with VB QUery

Anything wrong with the following query? subquery works fine but not the complete query :(
strNewSql1 = "SELECT tblEventLog.PartNumber & '_' & tblEventLog.PartNumberChgLvl FROM tblEventLog" & _
"WHERE (tblEventLog.PartNumber & '_' & tblEventLog.PartNumberChgLvl) NOT IN " & _
"(SELECT tblEventLog.PartNumber & '_' & tblEventLog.PartNumberChgLvl " & _
"FROM tblEventLog " & _
"WHERE tblEventLog.EventTypeSelected = 'pn REMOVED From Wrapper')" & _
"AND tblEventLog.TrackingNumber = """ & tempTrackingNumber & """"
You are missing a space between tblEventLog and WHERE.
"SELECT tblEventLog.PartNumber & '_' & tblEventLog.PartNumberChgLvl FROM tblEventLog" & _
"<ADD SPACE>WHERE (tblEventLog.PartNumber & '_' & tblEventLog.PartNumberChgLvl) NOT IN " & _