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 <> '';"
Related
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))
So I'm trying to teach myself VBA again and I'm having a cople of troubles. I'm trying to add new users to a table but keep getting the above error when I click my "Update" button. The text field will be in the form of 2 letters and 5 numbers. XX11111 for example.
Private Sub cmdAdd_Click()
'when we click on button Add there are two options
'1. for insert
'2. for update
If Me.txtLoginName.Tag & "" = "" Then
'add data to table
CurrentDb.Execute "INSERT INTO tblUsers(LoginName,UserName,Rank) " & _
" VALUES('" & Me.txtLoginName & "','" & Me.txtUsername & "','" & Me.cboRank & "')"
Else
CurrentDb.Execute "UPDATE tblUsers " & _
"set LoginName=" & Me.txtLoginName & "'" & _
", UserName='" & Me.txtUsername & "'" & _
", Rank='" & Me.cboRank & "'" & _
" WHERE LoginName=" & Me.txtLoginName.Tag
End If
'clear form
cmdClear_Click
'refresh data in list on form
frmModifyUsersSub.Form.Requery
End Sub
you are missing a ' in this line:
" set LoginName=" & Me.txtLoginName & "'" & _
change it to
" set LoginName='" & Me.txtLoginName & "'" & _
and :
" WHERE LoginName=" & Me.txtLoginName.Tag
to
" WHERE (LoginName='" & Me.txtLoginName.Tag & "')" ' and I don't know if this your intended where condition.
The error pretty much gives you the answer. You need 2 parameters for the function it is failing on. One thing to try is to change
CurrentDb.Execute "INSERT INTO tblUsers(LoginName,UserName,Rank) " & _
" VALUES('" & Me.txtLoginName & "','" & Me.txtUsername & "','" & Me.cboRank & "')"
and
CurrentDb.Execute "UPDATE tblUsers " & _
"set LoginName=" & Me.txtLoginName & "'" & _
", UserName='" & Me.txtUsername & "'" & _
", Rank='" & Me.cboRank & "'" & _
" WHERE LoginName=" & Me.txtLoginName.Tag
To
CurrentDb.Execute "INSERT INTO tblUsers(LoginName,UserName,Rank) " & _
" VALUES('" & Me.txtLoginName & "','" & Me.txtUsername & "','" & Me.cboRank & "')",dbFailOnError
and
CurrentDb.Execute "UPDATE tblUsers " & _
"set LoginName='" & Me.txtLoginName & "'" & _
", UserName='" & Me.txtUsername & "'" & _
", Rank='" & Me.cboRank & "'" & _
" WHERE LoginName='" & Me.txtLoginName.Tag & "'", dbFailOnError
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 & ""
I'm trying to do an insert on a button click event and I keep getting a runtime error of missing operator in query expression my query looks like the following. Any ideas?
Private Sub CmdAdd_Click()
Dim strSql As String
strSql = "INSERT INTO Current_Costs(PO_Number,Lineitemid,Capital_detail,CapitalID,GL_Number,Cost_Type,Cost_Center,Cost_cat,Item_Cost,PO_Date)" & _
" VALUES (" & Me.txtPONum & "','" & _
Me.cmbCapDetail & "','" & _
Me.cmbCapDetail.Column(1) & "','" & _
Me.txtCapID & "','" & _
Me.txtGLNum & "','" & _
Me.cmbCostType & "','" & _
Me.txtCostCen & "','" & _
Me.cmbCostCat & "','" & _
Me.txtCost & "','" & _
Me.TxtPODate & "')"
DoCmd.RunSQL strSql
i have a similar query that has the same issue and i cant see the problem
CurrentDb.Execute ("UPDATE Current_Costs " & _
"SET PO_Number='" & Me.txtPONum & "'" & _
",Lineitemid='" & Me.cmbCapDetail & "'" & _
",Capital_detail='" & Me.cmbCapDetail.Column(1) & "'" & _
",CapitalID='" & Me.txtCapID & "'" & _
",GL_Number='" & Me.txtGLNum & "'" & _
",Cost_Type='" & Me.cmbCostType & "'" & _
",Cost_Center='" & Me.txtCostCen & "'" & _
",Cost_cat='" & Me.cmbCostCat & "'" & _
",Item_Cost='" & Me.txtCost & "'" & _
",PO_Date='" & Me.TxtPODate & "'" & _
"WHERE LineItemPOID=" & Me.txtID.Tag)
edit solved
This
" VALUES (" & Me.txtPONum & "','" & _
Is short one quote, it should be
" VALUES ('" & Me.txtPONum & "','" & _
Write your sql in a string, it makes it easier to see the problems:
strSql = "UPDATE Current_Costs " & _
"SET PO_Number='" & txtPONum & "'" & _
",Lineitemid='" & cmbCapDetail & "'" & _
",Capital_detail='" & cmbCapDetail.Column(1) & "'" & _
",CapitalID='" & txtCapID & "'" & _
",GL_Number='" & txtGLNum & "'" & _
",Cost_Type='" & cmbCostType & "'" & _
",Cost_Center='" & txtCostCen & "'" & _
",Cost_cat='" & cmbCostCat & "'" & _
",Item_Cost='" & txtCost & "'" & _
",PO_Date='" & TxtPODate & "'" & _
" WHERE LineItemPOID=" & txtID
Dim db As database
Set db = CurrentDB
db.Execute strsql dbFailOnError
You were missing a space before WHERE and you had an unmatched parenthesis.
Consider using parameters: End of statement error
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 " & _