Having issues with how to use ' or " in sql statement and query using concatrelated function - vba

I have the following code using the ConcatRelated function. the current sql works with 1 where condition but I can't figure out how to get the sql to work with 2 conditions. It works when use it in a query. I get issues and the code does not debug.
ItemsShpdInvSQL = "SELECT InvoiceAllShippedItems.ShipmentID, InvoiceAllShippedItems.ShipType, " _
& "InvoiceAllShippedItems.BillTo, InvoiceAllShippedItems.CustID AS Consignee, " _
& "InvoiceAllShippedItems.PayMethod, InvoiceAllShippedItems.PaidInFull, " _
& "First(InvoiceAllShippedItems.Units) AS Units, " _
& "'Freight Charges - Ref. WR: ' & (ConcatRelated('[WR]'," _
& "'[InvoiceAllShippedItems]','[BillTo] =' & [BillTo])) & " _
& "' - (' & Sum([Chargeable]) & ' ' & [Units] & ' " _
& ") - Number of Pieces: ' & [NoPieces] AS InvDetails, " _
& "Sum(InvoiceAllShippedItems.[# of Pieces]) AS NoPieces, " _
& "Sum(InvoiceAllShippedItems.Chargeable) AS Charge, " _
& "CustRate([custid],[ShipType]) AS CustomerRate, " _
& "IIf(([Charge])<6.1,20/[Charge],[CustomerRate]) AS RateMin " _
& "FROM InvoiceAllShippedItems " _
& "GROUP BY InvoiceAllShippedItems.ShipmentID, " _
& "InvoiceAllShippedItems.ShipType, InvoiceAllShippedItems.BillTo, " _
& "InvoiceAllShippedItems.CustID, InvoiceAllShippedItems.PayMethod, " _
& "InvoiceAllShippedItems.PaidInFull, InvoiceAllShippedItems.Units, " _
& "CustRate([custid],[ShipType]) " _
& "ORDER BY InvoiceAllShippedItems.BillTo;"
'& "CustRate([custid],[ShipType]);"
with this it works in a query but not in the code above:
'concatrelated("[WR]","InvoiceAllShippedItems","[BillTo]&[PayMethod] ='" & [BillTo] & [PayMethod] & "'")

Related

add a row to query in MS-ACCESS SQL

I'm trying to add to the following query:
strSQL = "SELECT fldName, blkName, CDbl(fldValue) " & _
"FROM dbSecurities2 as S " & _
"WHERE " & _
"S.isin='" & Code & "' " & _
"AND " & _
"S.fldName='" & fldName & "' "
A row that makes the sum of the fldValue like:
strSQL = "SELECT fldName, blkName, CDbl(fldValue) " & _
"FROM dbSecurities2 as S " & _
"UNION " & _
"SELECT Sum(fldValue) AS fldValue " & _
"WHERE " & _
"S.isin='" & Code & "' " & _
"AND " & _
"S.fldName='" & fldName & "' "
the error is:
Run -time error '3141'. The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect
I found this is working:
strSQL = "SELECT fldName, blkName, CDbl(fldValue) " & _
"FROM dbSecurities2 as S " & _
"WHERE " & _
"S.isin='" & Code & "' " & _
"AND " & _
"S.fldName='" & fldName & "' " & _
"UNION " & _
"SELECT '' AS fldName, 'Total' AS Total, Sum(CDbl(fldValue)) " & _
"FROM dbSecurities2 AS B " & _
"WHERE " & _
"B.isin='" & Code & "' " & _
"AND " & _
"B.fldName='" & fldName & "' "
This should run as expected:
strSQL = "SELECT fldName, blkName, CDbl(fldValue) " & _
"FROM dbSecurities2 AS S " & _
"WHERE " & _
"S.isin='" & Code & "' " & _
"AND " & _
"S.fldName='" & fldName & "' " & _
"UNION ALL " & _
"SELECT TOP 1 "", "Total", Sum(CDbl(fldValue)) " & _
"FROM dbSecurities2"
If you have Null values, use Nz:
strSQL = "SELECT fldName, blkName, CDbl(Nz(fldValue, 0)) " & _
"FROM dbSecurities2 AS S " & _
"WHERE " & _
"S.isin='" & Code & "' " & _
"AND " & _
"S.fldName='" & fldName & "' " & _
"UNION ALL " & _
"SELECT TOP 1 "", "Total", Sum(CDbl(Nz(fldValue, 0))) " & _
"FROM dbSecurities2"

VB for MS Access not working when using INNER JOIN

The code isn't working properly, getting a Syntax error in INSERT INTO statement error. Trying to insert data with an inner join from an MS Access form where the user types in responses and selects a button to add the detail to a new table. Based on the information provided I want it to insert the data into another table where it is joined by a table to get the fiscal data.
stSQL = "INSERT INTO PO_Information " & _
"(Partner, PO_Number, PO_Title, Cost_Center, Description, Line_1_Amt, Line_2_Amt, Date_Added, Month_Added, Year_Added, FY, Qtr, FY_Qtr, FW, FWeek)" & _
"Select New_PO_Information.Partner, " & _
"New_PO_Information.PO_Number, " & _
"New_PO_Information.PO_Title, " & _
"New_PO_Information.Cost_Center, " & _
"New_PO_Information.Description, " & _
"New_PO_Information.Line_1_Amt, " & _
"New_PO_Information.Line_2_Amt, " & _
"New_PO_Information.Date_Added, " & _
"New_PO_Information.Month_Added, " & _
"New_PO_Information.Year_Added, " & _
"Fiscal_Calendar.FY, " & _
"Fiscal_Calendar.Qtr, " & _
"Fiscal_Calendar.FY_Qtr, " & _
"Fiscal_Calendar.FW, " & _
"Fiscal_Calendar.FWeek, " & _
"From New_PO_Information INNER JOIN Fiscal_Calendar " & _
"ON New_PO_Information.Date_Added = Fiscal_Calendar.Calendar"
stSQL = stSQL & _
"From New_PO_Information " & _
"Where ((New_PO_Information.Partner)<>'') " & _
"AND ((New_PO_Information.PO_Number)<>'') " & _
"AND ((New_PO_Information.PO_Title)<>'') " & _
"AND ((New_PO_Information.Cost_Center)<>'') " & _
"AND ((New_PO_Information.Description)<>'');"
Can someone tell me where I went wrong and correct the code, please?
It's probably this comma here: "Fiscal_Calendar.FWeek, " & _
There shouldn't be a comma before the FROM statement. And make sure you only have one FROM statement:
stSQL = "INSERT INTO PO_Information " & _
"(Partner, PO_Number, PO_Title, Cost_Center, Description, Line_1_Amt, Line_2_Amt, Date_Added, Month_Added, Year_Added, FY, Qtr, FY_Qtr, FW, FWeek)" & _
"Select New_PO_Information.Partner, " & _
"New_PO_Information.PO_Number, " & _
"New_PO_Information.PO_Title, " & _
"New_PO_Information.Cost_Center, " & _
"New_PO_Information.Description, " & _
"New_PO_Information.Line_1_Amt, " & _
"New_PO_Information.Line_2_Amt, " & _
"New_PO_Information.Date_Added, " & _
"New_PO_Information.Month_Added, " & _
"New_PO_Information.Year_Added, " & _
"Fiscal_Calendar.FY, " & _
"Fiscal_Calendar.Qtr, " & _
"Fiscal_Calendar.FY_Qtr, " & _
"Fiscal_Calendar.FW, " & _
"Fiscal_Calendar.FWeek " & _
"FROM New_PO_Information INNER JOIN Fiscal_Calendar " & _
"ON New_PO_Information.Date_Added = Fiscal_Calendar.Calendar"
stSQL = stSQL & _
" Where ((New_PO_Information.Partner)<>'') " & _
"AND ((New_PO_Information.PO_Number)<>'') " & _
"AND ((New_PO_Information.PO_Title)<>'') " & _
"AND ((New_PO_Information.Cost_Center)<>'') " & _
"AND ((New_PO_Information.Description)<>'');"
If I created an append query, here is the code. Just trying to convert this into my SQL statement that I posted earlier.
INSERT INTO PO_Information ( Partner, PO_Number, PO_TItle, Cost_Center, Description, Line_1_Amt, Line_2_Amt, Date_Added, Month_Added, Year_Added, FY, Qtr, FY_Qtr, FW, FWeek )
SELECT New_PO_Information.Partner, New_PO_Information.PO_Number, New_PO_Information.PO_TItle, New_PO_Information.Cost_Center, New_PO_Information.Description, New_PO_Information.Line_1_Amt, New_PO_Information.Line_2_Amt, New_PO_Information.Date_Added, New_PO_Information.Month_Added, New_PO_Information.Year_Added, Fiscal_Calendar.FY, Fiscal_Calendar.Qtr, Fiscal_Calendar.FY_Qtr, Fiscal_Calendar.FW, Fiscal_Calendar.FWeek
FROM New_PO_Information INNER JOIN Fiscal_Calendar ON New_PO_Information.Date_Added = Fiscal_Calendar.Calendar;

ms access 2007 update error run time error 3464, data type mismatch in criteria expression

i cannot update the data, it says that data type mismatch in criteria experession
CurrentDb.Execute "UPDATE PC_Details " & _
" SET Serial_No=" & Me.txtNo & _
", PC_Brand='" & Me.txtBrand & "'" & _
", PC_Model='" & Me.txtModel & "'" & _
", Status='" & Me.txtStatus & "'" & _
", Description='" & Me.txtDesc & "'" & _
", Staff_ID='" & Me.txtID & "'" & _
" WHERE Serial_No=" & Me.txtNo.Tag
Do this to debug:
Dim SQL As String
SQL = "UPDATE PC_Details " & _
" SET Serial_No=" & Me.txtNo & _
", PC_Brand='" & Me.txtBrand & "'" & _
", PC_Model='" & Me.txtModel & "'" & _
", Status='" & Me.txtStatus & "'" & _
", Description='" & Me.txtDesc & "'" & _
", Staff_ID='" & Me.txtID & "'" & _
" WHERE Serial_No=" & Me.txtNo.Tag
Debug.Print SQL
CurrentDb.Execute SQL
Then study the resulting SQL and post it here.
If all values are present, one or more is text but is handled as number or vice versa.

VB.NET Remainder Function

What I am trying to do is make a line of text appear every 50 strings read. I was trying to find a reaminder function to use on the GlobalVariables.TransferTracker interger, but I couldn't find anything online. Is there such a funciton? Or a differen't/better way to do this? If it helps here is my code:
Do While TransferRecord.Read()
'Start of writing to the SQL server.
SQLServerConnection.Open()
'SQL statement to transfer all the data that fits the requirements to the SQL server.
Dim SQLCommand1 As New SqlCommand("INSERT INTO dbo.b_Pulp_PI_Forte (" & _
"mill, " & _
"keyprinter_datetime, " & _
"bale_line_num, " & _
"pulp_line_id, " & _
"bale_id, " & _
"drop_datetime, " & _
"layboy_position, " & _
"bale_gross_weight, " & _
"gross_value_flag, " & _
"bale_airdry_pct, " & _
"airdry_value_flag, " & _
"sheets_per_bale, " & _
"grader_test_flag, " & _
"dropped_num, " & _
"created_by, " & _
"CreatedDateTime, " & _
"Who_did_it, " & _
"Last_change_datetime) " & _
"VALUES (" & _
"'850', " & _
"'" & ProdDate & "', " & _
"'" & BaleLineNum & "', " & _
"'" & BaleLine & "', " & _
"'" & BaleNumber & "', " & _
"'" & ProdDate & "', " & _
"'0', " & _
"'" & GrossWeight & "', " & _
"'" & GrossWeightFlag & "', " & _
"'" & AirDry & "', " & _
"'" & AirDryFlag & "', " & _
"'0', " & _
"'N', " & _
"'0', " & _
"'BaleTrac', " & _
"'" & Date.Now & "', " & _
"'BaleTrac', " & _
"'" & Date.Now & "')")
'If DisplayCode is checked this will be printed to the screen.
If ApplicationPropertiesWindow.DisplayCodechkbx.Checked = True Then
MainTextBox.AppendText(Environment.NewLine & SQLCommand1.CommandText)
GlobalVariables.DisplayCode = True
End If
'Executing the SQL statement.
SQLCommand1.Connection = SQLServerConnection
SQLCommand1.ExecuteNonQuery()
SQLServerConnection.Close()
GlobalVariables.TransferTracker = GlobalVariables.TransferTracker + 1
'This is where I would like to have the remainder function.
'Making message to show that program is still running.
If GlobalVariables.TransferTracker = 50 Then
MainTextBox.AppendText(Environment.NewLine & "50 records transferred.")
End If
Loop
Right now I just have it set up so it will fire at 50 records, because I couldn't find the function.
The remainder is an operator in VB, Mod:
If GlobalVariables.TransferTracker Mod 50 = 0 Then …
As a general advice, don’t write … = True in your conditions. Its redundancy is redundant.

Insert from Two tables to One while duplicates are ignored

I am trying to insert from two MS Access tables into one ms access table.
In one of the two tables there is a column that contains a duplicate value.
What I have at the moment is:
Dim MySQL As String = "INSERT INTO XML_TEST_CASE (" & _
"TCParmId, " & _
"TestCase, " & _
"MessageType, " & _
"FileNo, " & _
"InstructionNo, " & _
"TransactionNo, " & _
"ElementNo, " & _
"MessageSection, " & _
"ElementLevel, " & _
"FullElementPath, " & _
"ElementValue, " & _
"ElementValueNew, " & _
"NameSpace, " & _
"NameSpaceValue, " & _
"Attribute, " & _
"AttributeValue, " & _
"TestCaseDescription) " & _
"SELECT " & _
"P.TCParmId, " & _
"P.TestCase, " & _
"P.MessageType, " & _
"P.FileNo, " & _
"P.InstructionNo, " & _
"P.TransactionNo, " & _
"S.ElementNo, " & _
"S.MessageSection, " & _
"S.ElementLevel, " & _
"S.ElementPath + S.Element, " & _
"S.ElementValue, " & _
"S.ElementValue, " & _
"S.NameSpace, " & _
"S.NameSpaceValue, " & _
"S.Attribute, " & _
"S.AttributeValue, " & _
"P.TestCaseDescription " & _
"FROM XML_TEST_CASE_PARAMETER P, XML_MESSAGE_STRUCTURE S " & _
"WHERE S.MessageType = P.MessageType " & _
"AND P.TestCase = '" & MyTestCase & "' " & _
"AND P.MessageType = '" & MyMessageType & "' " & _
"AND P.FileNo = " & MyFileNo & " " & _
"AND P.InstructionNo = " & MyInstructionNo & " " & _
"AND P.TransactionNo = " & MyTransactionNo & ";"
The problem is that in the XML_MESSAGE_STRUCTURE table it happens that S.ElementNo sometimes appreas more than once which then cuasing that I get also more than one records in my destination table for the specific ElementNo. This I do not want.
Any help is appreaciated.
Rico
Instead of SELECT DISTINCT, you could group by all of the fields except ElementNo. For ElementNo, you would select Max(ElementNo):
Dim MySQL As String = "INSERT INTO XML_TEST_CASE (" & _
"TCParmId, " & _
"TestCase, " & _
"MessageType, " & _
"FileNo, " & _
"InstructionNo, " & _
"TransactionNo, " & _
"ElementNo, " & _
"MessageSection, " & _
...
"SELECT " & _
"P.TCParmId, " & _
"P.TestCase, " & _
"P.MessageType, " & _
"P.FileNo, " & _
"P.InstructionNo, " & _
"P.TransactionNo, " & _
"Max(S.ElementNo), " & _
"S.MessageSection, " & _
...
"FROM XML_TEST_CASE_PARAMETER P, XML_MESSAGE_STRUCTURE S " & _
"WHERE S.MessageType = P.MessageType " & _
"AND P.TestCase = '" & MyTestCase & "' " & _
"AND P.MessageType = '" & MyMessageType & "' " & _
"AND P.FileNo = " & MyFileNo & " " & _
"AND P.InstructionNo = " & MyInstructionNo & " " & _
"AND P.TransactionNo = " & MyTransactionNo & _
"GROUP BY " & _
"P.TCParmId, " & _
"P.TestCase, " & _
"P.MessageType, " & _
"P.FileNo, " & _
"P.InstructionNo, " & _
"P.TransactionNo, " & _
"S.MessageSection, " & _
...
";"