problem with VB QUery - ms-access

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

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

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))

MSAccess: Null vs '' - driving me crazy

I have a form with 2 embedded subforms.
The on-click event on subform_1 sets the recordsource of subform_2 referencing 5 attrs from the selected subform_1 record (all TEXT fields).
strSQL = "SELECT Table_1.* FROM Table_1 " & _
"WHERE (((Table_1.Attr_A)= '" & Forms![mainform]![subform_1]![attr1] & "') " & _
"AND ((Table_1.Attr_B) = '" & Forms![mainform]![subform_1]![attr2] & "') " & _
"AND ((Table_1.Attr_C) = '" & Forms![mainform]![subform_1]![attr3] & "') " & _
"AND ((Table_1.Attr_D) = '" & Forms![mainform]![subform_1]![attr4] & "') " & _
"AND ((Table_1.Attr_E) = '" & Forms![mainform]![subform_1]![attr5] & "'));"
Forms![mainform]![subform_2].Form.RecordSource = strSQL
My issue is that some records may have a NULL value among the 5 required attrs, which is a valid condition. The surrounding '' when the subform_1 value is NULL is resulting in (0) records in the collection.
Any suggestions to effectively handle the NULL condition in subform_1?
If you were considering only Table_1.Attr_A, I think you're saying you want this ...
"SELECT t1.* FROM Table_1 AS t1 " & _
"WHERE (t1.Attr_A & '') = '" & Forms![mainform]![subform_1]![attr1] & "'"
If that is correct, add an AND for the next condition based on Attr_B.
"SELECT t1.* FROM Table_1 AS t1 " & _
"WHERE " & _
"(t1.Attr_A & '') = '" & Forms![mainform]![subform_1]![attr1] & "'" & _
" AND (t1.Attr_B & '') = '" & Forms![mainform]![subform_1]![attr2] & "'"
And continue from there by adding the remaining conditions.
I believe the Nz function can also be of use:
"SELECT t1.* FROM Table_1 AS t1 " & _
"WHERE Nz(t1.Attr_A) = '" & Forms![mainform]![subform_1]![attr1] & "'"

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