Solved I was only searching for VB.NET but i tried to search C# and i found a solution and just converted to VB.NET
Sorry and thanks for everyone!
I'm going to INSERT & UPDATE about 600 queries contains update and insert so I want to have a progressbar to show how long it will take for that this is the exp of the query I will be using :
Dim sqlTexts = {
"UPDATE " & Form3.WorkTableName.Text & " Set `stat_value1` = `stat_value1` " & USEMULTIPLE.Text & " " & Stamina.Text & " WHERE stat_type1 = 7;",
"UPDATE " & Form3.WorkTableName.Text & " Set `stat_value2` = `stat_value2` " & USEMULTIPLE.Text & " " & Stamina.Text & " WHERE stat_type2 = 7;",
"UPDATE " & Form3.WorkTableName.Text & " Set `stat_value3` = `stat_value3` " & USEMULTIPLE.Text & " " & Stamina.Text & " WHERE stat_type3 = 7;",
"UPDATE " & Form3.WorkTableName.Text & " Set `stat_value4` = `stat_value4` " & USEMULTIPLE.Text & " " & Stamina.Text & " WHERE stat_type4 = 7;",
"UPDATE " & Form3.WorkTableName.Text & " Set `stat_value5` = `stat_value5` " & USEMULTIPLE.Text & " " & Stamina.Text & " WHERE stat_type5 = 7;",
"UPDATE " & Form3.WorkTableName.Text & " Set `stat_value6` = `stat_value6` " & USEMULTIPLE.Text & " " & Stamina.Text & " WHERE stat_type6 = 7;",
"UPDATE " & Form3.WorkTableName.Text & " Set `stat_value7` = `stat_value7` " & USEMULTIPLE.Text & " " & Stamina.Text & " WHERE stat_type7 = 7;",
"UPDATE " & Form3.WorkTableName.Text & " Set `stat_value8` = `stat_value8` " & USEMULTIPLE.Text & " " & Stamina.Text & " WHERE stat_type8 = 7;",
"UPDATE " & Form3.WorkTableName.Text & " Set `stat_value9` = `stat_value9` " & USEMULTIPLE.Text & " " & Stamina.Text & " WHERE stat_type9 = 7;",
"UPDATE " & Form3.WorkTableName.Text & " SET `stat_value10` = `stat_value10` " & USEMULTIPLE.Text & " " & Stamina.Text & " WHERE stat_type10 = 7;"
}
Using _
conn As New MySqlConnection("server=" & Form2.hostname.Text & ";Port=" & Form2.portid.Text & "; user id=" & Form2.hostuser.Text & "; password=" & Form2.ascentpass.Text & ";SslMode = none; database=" & Form2.databasename.Text & ""),
command As New MySqlCommand With {
.CommandType = CommandType.Text,
.Connection = conn
}
conn.Open()
For Each sql As String In sqlTexts
Try
ProgressBar1.Maximum = sql.Count
For i As Integer = 0 To sql.Count - 1
command.CommandText = sql
command.ExecuteNonQuery()
If True Then
ProgressBar1.Value = i + 1
End If
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Next
End Using
You can increment a variable on for each cicle after command.ExecuteNonQuery() and you you mast set a max value of progressbar at number of queries
Dim sqlTexts = {
"UPDATE " & Form3.WorkTableName.Text & " Set `stat_value1` = `stat_value1` " & USEMULTIPLE.Text & " " & Stamina.Text & " WHERE stat_type1 = 7;",
"UPDATE " & Form3.WorkTableName.Text & " Set `stat_value2` = `stat_value2` " & USEMULTIPLE.Text & " " & Stamina.Text & " WHERE stat_type2 = 7;",
"UPDATE " & Form3.WorkTableName.Text & " Set `stat_value3` = `stat_value3` " & USEMULTIPLE.Text & " " & Stamina.Text & " WHERE stat_type3 = 7;",
"UPDATE " & Form3.WorkTableName.Text & " Set `stat_value4` = `stat_value4` " & USEMULTIPLE.Text & " " & Stamina.Text & " WHERE stat_type4 = 7;",
"UPDATE " & Form3.WorkTableName.Text & " Set `stat_value5` = `stat_value5` " & USEMULTIPLE.Text & " " & Stamina.Text & " WHERE stat_type5 = 7;",
"UPDATE " & Form3.WorkTableName.Text & " Set `stat_value6` = `stat_value6` " & USEMULTIPLE.Text & " " & Stamina.Text & " WHERE stat_type6 = 7;",
"UPDATE " & Form3.WorkTableName.Text & " Set `stat_value7` = `stat_value7` " & USEMULTIPLE.Text & " " & Stamina.Text & " WHERE stat_type7 = 7;",
"UPDATE " & Form3.WorkTableName.Text & " Set `stat_value8` = `stat_value8` " & USEMULTIPLE.Text & " " & Stamina.Text & " WHERE stat_type8 = 7;",
"UPDATE " & Form3.WorkTableName.Text & " Set `stat_value9` = `stat_value9` " & USEMULTIPLE.Text & " " & Stamina.Text & " WHERE stat_type9 = 7;",
"UPDATE " & Form3.WorkTableName.Text & " SET `stat_value10` = `stat_value10` " & USEMULTIPLE.Text & " " & Stamina.Text & " WHERE stat_type10 = 7;"
}
Dim ProgressBar1 As ProgressBar
ProgressBar1.Location = New Point(10, 10)
ProgressBar1.Maximum = "getNumOfQuery()"
Using _
conn As New MySqlConnection("server=" & Form2.hostname.Text & ";Port=" & Form2.portid.Text & "; user id=" & Form2.hostuser.Text & "; password=" & Form2.ascentpass.Text & ";SslMode = none; database=" & Form2.databasename.Text & ""),
command As New MySqlCommand With {
.CommandType = CommandType.Text,
.Connection = conn
}
conn.Open()
For Each sql As String In sqlTexts
Try
command.CommandText = sql
command.ExecuteNonQuery()
ProgressBar1.Value=ProgressBar1.Value+1
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Next
End Using
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))
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?
When I click on save, I get the message:
You have an error in your SQL syntax;Check the manual that corresponds to your mySQL Server version for the right Syntax to use near 'idDrug=21' at line 21
Here is my code:
Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
Dim i As Integer
Dim xid As Integer
Dim xQTY(0) As Integer
Dim xQTY_ID(0) As Integer
Dim xCount As Integer
Dim xCounter_ID(0) As Integer
'Dim sqlstrx(5) As String
xid = 0
If lstitems.Items.Count > 0 Then
If Split(Me.Text, " - ")(1) = "Add" Then
sqlSTR = "INSERT INTO orders (CustID, Cust_Name, order_date) " & _
"VALUES (" & txtcustid.Text & ", " _
& "'" & txtcustname.Text & "', " _
& "'" & Format(dttoday.Value, "yyyy-MM-dd") & "')"
ExecuteSQLQuery(sqlSTR)
sqlSTR = "SELECT * FROM orders ORDER BY order_no DESC"
ExecuteSQLQuery(sqlSTR)
xid = sqlDT.Rows(0)("order_no")
For i = 0 To lstitems.Items.Count - 1
sqlSTR = "INSERT INTO orders_detail (order_no, idDrug, DrugName, Unit_Cost, qty, totalcost) " & _
"VALUES (" & xid & ", " _
& lstitems.Items(i).Text & ", " _
& "'" & lstitems.Items(i).SubItems(1).Text & "', " _
& "'" & lstitems.Items(i).SubItems(2).Text & "', " _
& lstitems.Items(i).SubItems(3).Text & ", " _
& lstitems.Items(i).SubItems(4).Text & ")"
ExecuteSQLQuery(sqlSTR)
'UPDATE STOCKS
sqlSTR = "UPDATE stockbalances SET ItemQuantity = ItemQuantity -" & CDbl(lstitems.Items(i).SubItems(3).Text) & _
"WHERE idDrug =" & lstitems.Items(i).Text
ExecuteSQLQuery(sqlSTR)
Next
Else
'delete first
For i = 0 To UBound(deleteID)
ExecuteSQLQuery("DELETE FROM Orders_detail WHERE order_no =" & txtorderno.Text & " AND idDrug =" & deleteID(i))
sqlSTR = "UPDATE stockbalances SET ItemQuantity = ItemQuantity +" & Delete_QTY(i) & _
" WHERE idDrug =" & deleteID(i)
ExecuteSQLQuery(sqlSTR)
Next
For i = 0 To UBound(deleteID)
ReDim deleteID(i)
deleteID(i) = 0
Next
del = 0
'--
'If lstitems.Items.Count > 0 Then
For i = 0 To lstitems.Items.Count - 1
'MsgBox(stockID & " " & lstitems.Items(i).Text)
sqlSTR = "SELECT * FROM orders_detail WHERE order_no =" & stockID & " AND idDrug =" & lstitems.Items(i).Text
ExecuteSQLQuery(sqlSTR)
If sqlDT.Rows.Count > 0 Then
ReDim Preserve xQTY(i), xQTY_ID(i)
xQTY(i) = sqlDT.Rows(0)("QTY")
'xQTY_ID(i) = sqlDT.Rows(0)("Item_ID")
End If
'MsgBox(sqlDT.Rows(0)("QTY"))
Next
For i = 0 To lstitems.Items.Count - 1
'MsgBox(xQTY_ID(i))
If lstitems.Items(i).Index <= (UBound(xQTY)) Then
If CDbl(lstitems.Items(i).SubItems(4).Text) < xQTY(i) Then
'MsgBox(xQTY(i) - CDbl(lstitems.Items(i).SubItems(4).Text))
If xQTY(i) > 0 Then
sqlSTR = "UPDATE orders_detail SET qty =" & lstitems.Items(i).SubItems(4).Text & ", " _
& "totalcost =" & lstitems.Items(i).SubItems(3).Text * lstitems.Items(i).SubItems(4).Text & _
" WHERE Order_no =" & stockID & " AND idDrug=" & lstitems.Items(i).Text
ExecuteSQLQuery(sqlSTR)
'UPDATE STOCKS
sqlSTR = "UPDATE stockBalances SET ItemQuantity = ItemQuantity + " & (xQTY(i) - CDbl(lstitems.Items(i).SubItems(4).Text)) & _
" WHERE idDrug =" & lstitems.Items(i).Text
ExecuteSQLQuery(sqlSTR)
End If
ElseIf CDbl(lstitems.Items(i).SubItems(4).Text) > xQTY(i) Then
If xQTY(i) > 0 Then
sqlSTR = "UPDATE orders_detail SET qty =" & lstitems.Items(i).SubItems(4).Text & ", " _
& "totalcost =" & lstitems.Items(i).SubItems(3).Text * lstitems.Items(i).SubItems(4).Text & _
" WHERE order_no =" & stockID & " AND idDrug=" & lstitems.Items(i).Text
ExecuteSQLQuery(sqlSTR)
'UPDATE STOCKS
sqlSTR = "UPDATE Stockbalances SET ItemQuantity = ItemQuantity - " & (CDbl(lstitems.Items(i).SubItems(4).Text) - xQTY(i)) & _
" WHERE idDrug =" & lstitems.Items(i).Text
ExecuteSQLQuery(sqlSTR)
End If
End If
End If
Next
' End If
'search for new item
sqlSTR = "SELECT * FROM orders_detail WHERE order_no =" & stockID & " ORDER BY Order_Dtl ASC"
ExecuteSQLQuery(sqlSTR)
xCount = sqlDT.Rows.Count
For i = 0 To sqlDT.Rows.Count - 1
ReDim Preserve xCounter_ID(i)
xCounter_ID(i) = sqlDT.Rows(i)("idDrug")
' xCount = i + 1
Next
'check
If lstitems.Items.Count > xCount Then
For i = 0 To lstitems.Items.Count - 1
If i > UBound(xCounter_ID) Then
'MsgBox(lstitems.Items(i).Text)
sqlSTR = "INSERT INTO orders_detail (order_no, idDrug, DrugName, price, qty, totalcost) " & _
"VALUES (" & txtorderno.Text & ", " _
& lstitems.Items(i).Text & ", " _
& "'" & lstitems.Items(i).SubItems(0).Text & "', " _
& "'" & lstitems.Items(i).SubItems(1).Text & "', " _
& lstitems.Items(i).SubItems(2).Text & ", " _
& lstitems.Items(i).SubItems(3).Text & ", " _
& lstitems.Items(i).SubItems(4).Text & ")"
ExecuteSQLQuery(sqlSTR)
'UPDATE STOCKS
sqlSTR = "UPDATE stockbalances SET ItemQuantity = ItemQuantity -" & CDbl(lstitems.Items(i).SubItems(4).Text) & _
"WHERE idDrug =" & lstitems.Items(i).Text
ExecuteSQLQuery(sqlSTR)
End If
Next
End If
End If
Else
MsgBox("Can't save without details !!", MsgBoxStyle.Exclamation, xTitlename)
Exit Sub
End If
MsgBox("Record has been saved !!", MsgBoxStyle.Information, xTitlename)
sqlSTR = "SELECT distinct orders.order_no AS 'Order No.', Cust_Name as 'Customer Name', order_date AS 'Date', sum(totalcost) AS 'TOTAL DUE' FROM orders_detail " & _
"INNER JOIN orders ON orders_detail.order_no = orders.order_no " & _
"WHERE order_date ='" & Format(dttoday.Value, "yyyy-MM-dd") & "' GROUP BY orders.order_no, Cust_Name, order_date"
FillListView(ExecuteSQLQuery(sqlSTR), FrmORDERLIST.lstorder, 0)
Me.Close()
End Sub
It is impossible to fix all of your code and to be precise what is the exact cause of your error.
Suffice to say that you never should do a database application using that kind of string concatenation approach. Any of your text fields could cause the error because it contains single quotes or any of your date items could cause the error because it is not formatted how the database requires. (Same for decimals and other floating point values).
To solve this problems (and to avoid the dangerous Sql Injection scenarion) exists the parameterized queries approach.
So, just an example on what you need to do for every line of your sql commands
sqlSTR = "INSERT INTO orders_detail (order_no, idDrug, DrugName, " & _
"Unit_Cost, qty, totalcost) VALUES (" & _
"#id, #iddrug, #dname,#unit, #qty, #total)"
Using cmd = new MySqlCommand(sqlStr, connection)
cmd.Parameters.AddWithValue("#id", xid)
cmd.Parameters.AddWithValue("#idrug", Convert.ToInt32(lstitems.Items(i).Text))
cmd.Parameters.AddWithValue("#dname", lstitems.Items(i).SubItems(1).Text)
cmd.Parameters.AddWithValue("#unit", lstitems.Items(i).SubItems(2).Text)
cmd.Parameters.AddWithValue("#qty", Convert.ToDecimal(lstitems.Items(i).SubItems(3).Text ))
cmd.Parameters.AddWithValue("#total", Convert.ToDecimal(lstitems.Items(i).SubItems(4).Text))
cmd.ExecuteNonQuery()
End Using
Notice how, in a parameterized query, you could specify the datatype of the value passed for the parameter using the Convert.ToXXXXX method. In this way the framework code could prepare the appropriate formatting of your values to be passed to the database engine.
Not to mention the now readable query text.
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 & ""