ProgressBar for a Huge List of INSERT - UPDATE Queries - mysql

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

Related

Access 2013 Edit Form error 3021

So I've got the code below (not all of it is grey for some reason)
Private Sub CmdEdit_Click()
Me.Tableinform.Requery
Dim rs As DAO.Recordset
Set rs = Me.Tableinform.Form.RecordsetClone
'get data to textbox control
With rs
If .RecordCount > 0 Then
With Me.Tableinform.Form.Recordset
Me.ShowIDbox = .Fields("[ID]").Value
Me.CompbyDD = .Fields("[Received By]")
Me.Date1 = .Fields("[Date Received]")
Me.Date2 = .Fields("[Date Processed]")
Me.ReqType = .Fields("[Request Type]")
Me.InsName = .Fields("[Insured Name]")
Me.RiskNo = .Fields("[Risk Number]")
Me.EndtRef = .Fields("[Endorsement Reference]")
Me.EOCNo = .Fields("[EOC Number]")
Me.Tech = .Fields("[Technician]")
Me.BillIns = .Fields("[Billing Instructions]")
Me.Addrecord.Caption = "Update"
Me.CmdEdit.Enabled = False
Me.cmdDuplicate.Enabled = True
Me.CmdDelete.Enabled = True
End With
End If
End With
End Sub
After one Edit and Update, the edit function throws out Error '3021' No Current Record?
Please show me where I am going wrong with this.
Add/Update button as follows:
If Me.ShowIDbox.Value = "" Then
CurrentDb.Execute "INSERT INTO fmdatatable( [Received By], [Date Received], [Date Processed], [Request Type], [Insured Name], [Risk Number], [Endorsement Reference], [EOC Number], [Technician], [Billing Instructions]) " & _
" VALUES ('" & Me.CompbyDD & "','" & Me.Date1 & "','" & Me.Date2 & "','" & Me.ReqType & "','" & Me.InsName & "','" & Me.RiskNo & "','" & Me.EndtRef & "','" & Me.EOCNo & "','" & Me.Tech & "','" & Me.BillIns & "')"
Else
CurrentDb.Execute "UPDATE fmdatatable SET [Received By]='" & Me.CompbyDD & "'" & _
", [Date Received]='" & Me.Date1 & "'" & _
", [Date Processed]='" & Me.Date2 & "'" & _
", [Request Type]='" & Me.ReqType & "'" & _
", [Insured Name]='" & Me.InsName & "'" & _
", [Risk Number]='" & Me.RiskNo & "'" & _
", [Endorsement Reference]='" & Me.EndtRef & "'" & _
", [EOC Number]='" & Me.EOCNo & "'" & _
", [Technician]='" & Me.Tech & "'" & _
", [Billing Instructions]='" & BillIns & "'" & _
" WHERE [ID]=" & Me.ShowIDbox.Value
End If
Me.CompbyDD = "-Please Select-"
Me.Date1 = ""
Me.Date2 = ""
Me.ReqType = "-Please Select-"
Me.InsName = ""
Me.RiskNo = ""
Me.EndtRef = ""
Me.EOCNo = ""
Me.Tech = ""
Me.BillIns = "-Please Select-"
Me.ShowIDbox = ""
Me.Addrecord.Caption = "Add Record"
Me.CmdEdit.Enabled = True
Me.cmdDuplicate.Enabled = False
Me.CmdDelete.Enabled = False
Tableinform.Form.Requery
This edit works once, then all subsequent later edits are throwing errors out.

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 <> '';"

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

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'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 & ""

End of statement error

I am trying to assemble an insert statement with multiple line breaks. The insertion includes both numerical as well as textual data. I keep getting an error, and I cannot find the where I am syntactically wrong. Can anyone see me error?
CurrentDb.Execute "INSERT INTO tblCustParts (CustPartNum,CustomerID,Alloy,Temper,Finish,FormType,Gauge,GaugeAimPlus,GaugeAimMinus," & _
"GaugeGuarPlus,GaugeGuarMinus,Width,WidthAimPlus,WidthAimMinus,WidthGuarPlus,WidthGuarMinus,Length,LengthAimPlus,LengthAimMinus," & _
"LengthGuarPlus,LengthGuarMinus,Diameter,DiameterAimPlus,DiameterAimMinus,DiameterGuarPlus,DiameterGuarMinus,CircleShear,IDMin," & _
"ODMin,ODMax,LabelAs,ProduceAs,ShopPaperNotes,EyeOrientation,CoreType,PackingNotes,AQ,PaperInterleave,HeatTreatedSkids,HasRecipeFlag," & _
"QRRExists,TensionLevel,MSKCoreSpec,ChemCertsReq,PhysCertsReq,PhysAndChemCertsReq,AAStandard,ASTM_B209_10)" & _
"VALUES('" & Part & "', " & Me.Customer & ", '" & Alloy & "', '" & Temper & "', '" & Finish & "', '" & FormType & "', " & Me.Gauge & ", "" & _
" & Me.Gauge & ", " & Me.GaugeAimPlus & ", " & Me.GaugeAimMinus & ", " & Me.GaugeGuarPlus & ", " & Me.GaugeGuarMinus & ", "" & _
" & Me.WidthEntry & ", " & Me.WidthAimPlus & ", " & Me.WidthAimMinus & ", " & Me.WidthGuarPlus & ", " & Me.WidthGuarMinus & ", "" & _
" & Me.LengthEntry & ", " & Me.LengthAimPlus & ", " & Me.LengthAimMinus & ", " & Me.LengthGuarPlus & ", " & Me.LengthGuarMinus & ", "" & _
" & Me.Diameter & ", " & Me.DiameterAimPlus & ", " & Me.DiameterAimMinus & ", " & Me.DiameterGuarPlus & ", "" & _
" & Me.DiameterGuarMinus & ", " & Me.CS & ", " & Me.IDMin & ", " & Me.ODMin & ", " & Me.ODMax & ", '" & Me.LabelAs & "', '"" & _
" & Me.ProduceAs & "', '" & Me.ShopPaperNotes & "', '" & Me.EyeOrientation & "', '" & Me.CoreType & "', '" & Me.PackingNotes & "', "" & _
" & Me.AQ & ", " & Me.PaperInterleave & ", " & Me.HeatTreatedSkids & ", " & Me.HasRecipeFlag & ", " & Me.QRRExists & ", "" & _
" & Me.TensionLevel & ", " & Me.MSKCoreSpec & ", " & Me.ChemCertsReq & ", " & Me.PhysCertsReq & ", " & Me.PhysAndChemCertsReq & ", "" & _
" & Me.AAStandard & ", " & Me.ASTM_B209_10 & ")"
I am not sure what the extra quotes and ampersands are for, but they are causing the problem. They occur at the start and end of each Value line. It is best to put your string in a separate variable, and then execute that, it is easier to spot problems. You might also like to consider parameters, they will make your life easier in this case.
ssql = "INSERT INTO tblCustParts (CustPartNum,CustomerID,Alloy,Temper,Finish,FormType,Gauge,GaugeAimPlus,GaugeAimMinus," & _
"GaugeGuarPlus,GaugeGuarMinus,Width,WidthAimPlus,WidthAimMinus,WidthGuarPlus,WidthGuarMinus,Length,LengthAimPlus,LengthAimMinus," & _
"LengthGuarPlus,LengthGuarMinus,Diameter,DiameterAimPlus,DiameterAimMinus,DiameterGuarPlus,DiameterGuarMinus,CircleShear,IDMin," & _
"ODMin,ODMax,LabelAs,ProduceAs,ShopPaperNotes,EyeOrientation,CoreType,PackingNotes,AQ,PaperInterleave,HeatTreatedSkids,HasRecipeFlag," & _
"QRRExists,TensionLevel,MSKCoreSpec,ChemCertsReq,PhysCertsReq,PhysAndChemCertsReq,AAStandard,ASTM_B209_10)" & _
"VALUES('" & Part & "', " & Me.Customer & ", '" & Alloy & "', '" & Temper & "', '" & Finish & "', '" & FormType & "', " & Me.Gauge & ", " & _
Me.Gauge & ", " & Me.GaugeAimPlus & ", " & Me.GaugeAimMinus & ", " & Me.GaugeGuarPlus & ", " & Me.GaugeGuarMinus & ", " & _
Me.WidthEntry & ", " & Me.WidthAimPlus & ", " & Me.WidthAimMinus & ", " & Me.WidthGuarPlus & ", " & Me.WidthGuarMinus & ", " & _
Me.LengthEntry & ", " & Me.LengthAimPlus & ", " & Me.LengthAimMinus & ", " & Me.LengthGuarPlus & ", " & Me.LengthGuarMinus & ", " & _
Me.Diameter & ", " & Me.DiameterAimPlus & ", " & Me.DiameterAimMinus & ", " & Me.DiameterGuarPlus & ", " & _
Me.DiameterGuarMinus & ", " & Me.CS & ", " & Me.IDMin & ", " & Me.ODMin & ", " & Me.ODMax & ", '" & Me.LabelAs & "', '" & _
Me.ProduceAs & "', '" & Me.ShopPaperNotes & "', '" & Me.EyeOrientation & "', '" & Me.CoreType & "', '" & Me.PackingNotes & "', " & _
Me.AQ & ", " & Me.PaperInterleave & ", " & Me.HeatTreatedSkids & ", " & Me.HasRecipeFlag & ", " & Me.QRRExists & ", " & _
Me.TensionLevel & ", " & Me.MSKCoreSpec & ", " & Me.ChemCertsReq & ", " & Me.PhysCertsReq & ", " & Me.PhysAndChemCertsReq & ", " & _
Me.AAStandard & ", " & Me.ASTM_B209_10 & ")"
Dim db As database
Set db = CurrentDB
db.Execute ssql dbFailOnError
Last record inserted ID
Set rs = db.OpenRecordset("select ##identity as id")
LastID = rs("id")
Parameter example:
ssql = "INSERT INTO Table1 (Atext,Anumber) Values (#AText,#Anumber)"
Dim qdf As QueryDef
Set qdf = CurrentDb.CreateQueryDef("", ssql)
qdf.Parameters("#atext") = "abc"
qdf.Parameters("#Anumber") = 1
qdf.Execute dbFailOnError
Last record inserted ID
qdf.SQL = "select ##identity as id"
Set rs = qdf.OpenRecordset
LastID = rs("id")