How to overcome automatic new line break in long VBA string? - ms-access
I am using VBA in building a long Query in MS Access. My target is to produce Query as quoted below, which works fine when tried in Access query view (no need to scrutinize the quotes since it worked for my purpose):
However, due to its unusual length, VBA seems to have broken it automatically into 8 segments, which makes the query not executable when calling DoCmd.RunSQL PlySQL (PlySQL is the string variable to store the query below. The line breaker appeared in between a specific terms so to make query meaningless.)
I have tried to remove the newline breaker by Replace functions like below, but neither worked which makes me think it is a fundamentally data structure issue that I am facing. Any idea how to work around this? Thanks so much.
PlySQL = Replace(PlySQL, vbCrLf, "")
PlySQL = Replace(PlySQL, vbNewLine, "")
SELECT [199504_hdb].Timestamp AS MTimestamp , [199504_hdb].Board_Card_1 AS BoardCard1 , [199504_hdb].Board_Card_2 AS BoardCard2 , [199504_hdb].Board_Card_3 AS BoardCard3 , [199504_hdb].Board_Card_4 AS BoardCard4 , [199504_hdb].Board_Card_5 AS BoardCard5 , [199504_hdb].No_Of_Players_Dealt_Cards AS No_of_P_DC , [199504_hdb].Players_See_Flop AS No_of_P_SeeFlop , [199504_hdb].Players_See_Turn AS No_of_P_SeeTurn , [199504_hdb].Players_See_River AS No_of_P_SeeRiver , [199504_hdb].Players_At_Showdown AS No_of_P_AtSD , [199504_hdb].Pot_BGN_Flop AS Pot_BGN_Flop , [199504_hdb].Pot_BGN_Turn AS Pot_BGN_Turn , [199504_hdb].Pot_BGN_River AS Pot_BGN_River , [199504_hdb].Pot_At_Showdown AS Pot_AtSD , [199504_hroster].Player_NickNm_1 AS Player1Nm , [199504_hroster].Player_NickNm_2 AS Player2Nm , [199504_hroster].Player_NickNm_3 AS Player3Nm , [199504_hroster].Player_NickNm_4 AS Player4Nm , [199504_hroster].Player_NickNm_5 AS Player5Nm , [199504_hroster].Player_NickNm_6 AS Player6Nm , [199504_hroster].Player_NickNm_7 AS Player7Nm , [199504_hroster].Player_NickNm_8 AS Player8Nm , [199504_hroster].Player_NickNm_9 AS Player9Nm , [199504_hroster].Player_NickNm_10 AS Player10Nm , [199504_hroster].Player_NickNm_11 AS Player11Nm , [199504_hroster].Player_NickNm_12 AS Player12Nm , [199504_pdb_Jime_xlsx].Betting_On_Flop AS P1_BetOnFlop , [199504_pdb_Jime_xlsx].Betting_On_River AS P1_BetOnRiver , [199504_pdb_Jime_xlsx].Betting_On_Turn AS P1_BetOnTurn , [199504_pdb_Jime_xlsx].Betting_Pre_Flop AS P1_BetPreFlop , [199504_pdb_Jime_xlsx].Player_Bankroll_BGN AS P1_Bankroll_BGN , [199504_pdb_Jime_xlsx].Player_Total_Betting_During_Hand AS P1_TotalBetting , [199504_pdb_Jime_xlsx].Player_Pocket_Card_1 AS P1_Card1 , [199504_pdb_Jime_xlsx].Player_Pocket_Card_2 AS P1_Card2 , [199504_pdb_Quick_xlsx].Betting_On_Flop AS P2_BetOnFlop , [199504_pdb_Quick_xlsx].Betting_On_River AS P2_BetOnRiver , [199504_pdb_Quick_xlsx].Betting_On_Turn AS P2_BetOnTurn , [199504_pdb_Quick_xlsx].Betting_Pre_Flop AS P2_BetPreFlop , [199504_pdb_Quick_xlsx].Player_Bankroll_BGN AS P2_Bankroll_BGN , [199504_pdb_Quick_xlsx].Player_Total_Betting_During_Hand AS P2_TotalBetting , [199504_pdb_Quick_xlsx].Player_Pocket_Card_1 AS P2_Card1 , [199504_pdb_Quick_xlsx].Player_Pocket_Card_2 AS P2_Card2 , [199504_pdb_Schween_xlsx].Betting_On_Flop AS P3_BetOnFlop , [199504_pdb_Schween_xlsx].Betting_On_River AS P3_BetOnRiver , [199504_pdb_Schween_xlsx].Betting_On_Turn AS P3_BetOnTurn , [199504_pdb_Schween_xlsx].Betting_Pre_Flop AS P3_BetPreFlop , [199504_pdb_Schween_xlsx].Player_Bankroll_BGN AS P3_Bankroll_BGN , [199504_pdb_Schween_xlsx].Player_Total_Betting_During_Hand AS P3_TotalBetting , [199504_pdb_Schween_xlsx].Player_Pocket_Card_1 AS P3_Card1 , [199504_pdb_Schween_xlsx].Player_Pocket_Card_2 AS P3_Card2 , [199504_pdb_^^^^^^_xlsx].Betting_On_Flop AS P4_BetOnFlop , [199504_pdb_^^^^^^_xlsx].Betting_On_River AS P4_BetOnRiver , [199504_pdb_^^^^^^_xlsx].Betting_On_Turn AS P4_BetOnTurn , [199504_pdb_^^^^^^_xlsx].Betting_Pre_Flop AS P4_BetPreFlop , [199504_pdb_^^^^^^_xlsx].Player_Bankroll_BGN AS P4_Bankroll_BGN , [199504_pdb_^^^^^^_xlsx].Player_Total_Betting_During_Hand AS P4_TotalBetting , [199504_pdb_^^^^^^_xlsx].Player_Pocket_Card_1 AS P4_Card1 , [199504_pdb_^^^^^^_xlsx].Player_Pocket_Card_2 AS P4_Card2 , [199504_pdb_aida_xlsx].Betting_On_Flop AS P5_BetOnFlop , [199504_pdb_aida_xlsx].Betting_On_River AS P5_BetOnRiver , [199504_pdb_aida_xlsx].Betting_On_Turn AS P5_BetOnTurn , [199504_pdb_aida_xlsx].Betting_Pre_Flop AS P5_BetPreFlop , [199504_pdb_aida_xlsx].Player_Bankroll_BGN AS P5_Bankroll_BGN , [199504_pdb_aida_xlsx].Player_Total_Betting_During_Hand AS P5_TotalBetting , [199504_pdb_aida_xlsx].Player_Pocket_Card_1 AS P5_Card1 , [199504_pdb_aida_xlsx].Player_Pocket_Card_2 AS P5_Card2 , [199504_pdb_argh_xlsx].Betting_On_Flop AS P6_BetOnFlop , [199504_pdb_argh_xlsx].Betting_On_River AS P6_BetOnRiver , [199504_pdb_argh_xlsx].Betting_On_Turn AS P6_BetOnTurn , [199504_pdb_argh_xlsx].Betting_Pre_Flop AS P6_BetPreFlop , [199504_pdb_argh_xlsx].Player_Bankroll_BGN AS P6_Bankroll_BGN , [199504_pdb_argh_xlsx].Player_Total_Betting_During_Hand AS P6_TotalBetting , [199504_pdb_argh_xlsx].Player_Pocket_Card_1 AS P6_Card1 , [199504_pdb_argh_xlsx].Player_Pocket_Card_2 AS P6_Card2 , [199504_pdb_brillo_xlsx].Betting_On_Flop AS P7_BetOnFlop , [199504_pdb_brillo_xlsx].Betting_On_River AS P7_BetOnRiver , [199504_pdb_brillo_xlsx].Betting_On_Turn AS P7_BetOnTurn , [199504_pdb_brillo_xlsx].Betting_Pre_Flop AS P7_BetPreFlop , [199504_pdb_brillo_xlsx].Player_Bankroll_BGN AS P7_Bankroll_BGN , [199504_pdb_brillo_xlsx].Player_Total_Betting_During_Hand AS P7_TotalBetting , [199504_pdb_brillo_xlsx].Player_Pocket_Card_1 AS P7_Card1 , [199504_pdb_brillo_xlsx].Player_Pocket_Card_2 AS P7_Card2 , [199504_pdb_jaxter_xlsx].Betting_On_Flop AS P8_BetOnFlop , [199504_pdb_jaxter_xlsx].Betting_On_River AS P8_BetOnRiver , [199504_pdb_jaxter_xlsx].Betting_On_Turn AS P8_BetOnTurn , [199504_pdb_jaxter_xlsx].Betting_Pre_Flop AS P8_BetPreFlop , [199504_pdb_jaxter_xlsx].Player_Bankroll_BGN AS P8_Bankroll_BGN , [199504_pdb_jaxter_xlsx].Player_Total_Betting_During_Hand AS P8_TotalBetting , [199504_pdb_jaxter_xlsx].Player_Pocket_Card_1 AS P8_Card1 , [199504_pdb_jaxter_xlsx].Player_Pocket_Card_2 AS P8_Card2 , [199504_pdb_rimedio_xlsx].Betting_On_Flop AS P9_BetOnFlop , [199504_pdb_rimedio_xlsx].Betting_On_River AS P9_BetOnRiver , [199504_pdb_rimedio_xlsx].Betting_On_Turn AS P9_BetOnTurn , [199504_pdb_rimedio_xlsx].Betting_Pre_Flop AS P9_BetPreFlop , [199504_pdb_rimedio_xlsx].Player_Bankroll_BGN AS P9_Bankroll_BGN , [199504_pdb_rimedio_xlsx].Player_Total_Betting_During_Hand AS P9_TotalBetting , [199504_pdb_rimedio_xlsx].Player_Pocket_Card_1 AS P9_Card1 , [199504_pdb_rimedio_xlsx].Player_Pocket_Card_2 AS P9_Card2 FROM((((((((((199504_hroster INNER JOIN [199504_hdb] ON [199504_hroster].Timestamp = [199504_hdb].Timestamp) INNER JOIN [199504_pdb_Jime_xlsx] ON [199504_hroster].Timestamp = [199504_pdb_Jime_xlsx].Timestamp) INNER JOIN [199504_pdb_Quick_xlsx] ON [199504_hroster].Timestamp = [199504_pdb_Quick_xlsx].Timestamp) INNER JOIN [199504_pdb_Schween_xlsx] ON [199504_hroster].Timestamp = [199504_pdb_Schween_xlsx].Timestamp) INNER JOIN [199504_pdb_^^^^^^_xlsx] ON [199504_hroster].Timestamp = [199504_pdb_^^^^^^_xlsx].Timestamp) INNER JOIN [199504_pdb_aida_xlsx] ON [199504_hroster].Timestamp = [199504_pdb_aida_xlsx].Timestamp) INNER JOIN [199504_pdb_argh_xlsx] ON [199504_hroster].Timestamp = [199504_pdb_argh_xlsx].Timestamp) INNER JOIN [199504_pdb_brillo_xlsx] ON [199504_hroster].Timestamp = [199504_pdb_brillo_xlsx].Timestamp) INNER JOIN [199504_pdb_jaxter_xlsx] ON [199504_hroster].Timestamp = [199504_pdb_jaxter_xlsx].Timestamp) INNER JOIN [199504_pdb_rimedio_xlsx] ON [199504_hroster].Timestamp = [199504_pdb_rimedio_xlsx].Timestamp);
Use the line continue character:
PlySQL = _
"First long line " & _
"Second long line " & _
"Last long line"
Related
Trying out this query in MS Access - Please let me who where i am wrong
The aim is to join the same table and retreive two different columns using where clause SELECT a.analyst_assigned , count(a.case_id) AS Pending , count(b.case_id) AS Completed , sum(b.total_errors) AS Errors FROM cases_main as a WHERE a.team_qc1_qa2 = "qa" and a.q_status in ("Not Started","Under Review","Follow-up") and a.p_date_worked between #03/08/2021# and #03/14/2021# INNER JOIN cases_main as b ON a.analyst_assigned = b.analyst_assigned WHERE b.team_qc1_qa2 = "qa" and b.q_status in ("Completed: No Error","Completed: Feedback") and b.p_date_worked between #03/08/2021# and #03/14/2021# GROUP BY b.analyst_assigned;
Try this simpler approach: SELECT a.analyst_assigned , abs(sum(q_status Like "Completed*")) AS Pending , abs(sum(q_status Not Like "Completed*")) AS Completed , sum(b.total_errors) AS Errors FROM cases_main as a WHERE a.team_qc1_qa2 = "qa" and a.p_date_worked between #03/08/2021# and #03/14/2021# GROUP BY a.analyst_assigned
MySQL query return different results every time
I have the query SELECT g.f_id as f_code , ss.f_name as f_storage , g.f_name as f_goods , g.f_scancode , s.f_price , SUM(s.f_qty*s.f_type) as f_qty , SUM(s.f_qty*s.f_type)*g.f_saleprice as f_totalsale FROM a_store s JOIN c_goods g ON g.f_id = s.f_goods JOIN c_storages ss ON ss.f_id = s.f_store JOIN a_header h ON h.f_id = s.f_document WHERE h.f_date <= '2020-06-22' AND h.f_state = 1 GROUP BY g.f_id , ss.f_name , g.f_name , g.f_scancode , s.f_price HAVING SUM(s.f_qty*s.f_type) <> 0 ORDER BY SUM(s.f_qty*s.f_type) asc Every time this query returns different result and different rows count without the changes in the database. I cant understand, why.
I was change the version of mysql from 5.7 to 8.0 and problem was gone. Why it worked wrong on version 5.7 remains a mystery.
Joining on different data types
I'm attempting to do an inner join on CardID, which is present in 2 tables: PCARDS_ILL_DBO_CARD and tExceptionsAll2. In PCARDS_ILL_DBO_CARD it is a 'Number' and in tExceptionsAll2 it is 'Short Text.' I've looked at answers to similar questions posted on Stackoverflow and implemented them, but I'm still getting an error. I am unable to change any formatting in PCARDS_ILL_DBO_CARD; several other queries in different databases rely on tExceptionsAll2 having the current formatting and data types it has. Here's what I have: SELECT tExceptionsAll2.ID , tExceptionsAll2.CardholderName , PCARDS_ILL_DBO_CARD.PERSON_ID , tExceptionsAll2.CardType , tExceptionsAll2.Duration , tExceptionsAll2.ExceptionType , tExceptionsAll2.STL AS [Exp STL] , tExceptionsAll2.CL AS [Exp CL] , PCARDS_ILL_DBO_CARD.TRANS_LIMIT_AMT AS [Card STL] , PCARDS_ILL_DBO_CARD.MONTH_LIMIT_AMT AS [Card CL] , tExceptionsAll2.TerminationDate FROM PCARDS_ILL_DBO_CARD INNER JOIN tExceptionsAll2 ON (PCARDS_ILL_DBO_CARD.CARD_ID = CAST (tExceptionsAll2.CardID AS INT)) AND (PCARDS_ILL_DBO_CARD.PERSON_ID = tExceptionsAll2.CardholderUIN) WHERE (((tExceptionsAll2.STL)>0) AND ((tExceptionsAll2.CL)>0) AND ((PCARDS_ILL_DBO_CARD.TRANS_LIMIT_AMT)<>[tExceptionsAll2].[STL]) AND ((PCARDS_ILL_DBO_CARD.MONTH_LIMIT_AMT)<>[tExceptionsAll2].[CL]) AND ((tExceptionsAll2.TerminationDate) Is Null)); Here's the error:
How optimize the performance of SerialNumbers by SalesInvoicesExploded?
I have the following query: select sie.invoicedate sie_invoicedate , sie.Silitem sle_item , sie.Silitemcode sle_itemcode , sie.Silitemdescription sle_itemdescription , sie.Silnetprice sle_netprice , sie.Silquantity sle_quantity , sie.Silunitprice sle_unitprice , ctr.ctr_code ctr_code , ctr.ctr_name ctr_name , ctr.parent_code parent_code , ctr.parent_name parent_name , gdlsn.ssrserialnumber serialnumber from SalesInvoicesExploded sie join customers#inmemorystorage ctr on ctr.ctr_id = sie.invoiceto join GoodsDeliveryLineSerialNumbers gdlsn on gdlsn.salesorderlineid = sie.silid where sie.invoicedate >= '2016-01-01' and sie.invoicedate < '2016-01-03' order by sie.invoicedate How can I get the serial numbers only from the date range? In the debugger I see a lot of requests to Exact Online.
For now, there isn't a very good filter possibility to get the result you want. The problem is that there is no way to perform the gdlsn.salesorderlineid = sie.silid filter on the data set unless the data sets have been fetched from the other side. Only specific filters are executed server-side (like your invoicedate >= '2016-01-01'). This is quite a hard nut to crack from the program side. It would work if you can specify a filter that can be determined on beforehand, like that the date in GoodsDeliveryLineSerialNumbers.Created always comes after the invoicedate. It would mean a significant performance improvement if you can narrow down the set based on that date. I suggest to use something like this, if possible: select sie.invoicedate sie_invoicedate , sie.Silitem sle_item , sie.Silitemcode sle_itemcode , sie.Silitemdescription sle_itemdescription , sie.Silnetprice sle_netprice , sie.Silquantity sle_quantity , sie.Silunitprice sle_unitprice , ctr.ctr_code ctr_code , ctr.ctr_name ctr_name , ctr.parent_code parent_code , ctr.parent_name parent_name , gdlsn.ssrserialnumber serialnumber from SalesInvoicesExploded sie join customers#inmemorystorage ctr on ctr.ctr_id = sie.invoiceto join GoodsDeliveryLineSerialNumbers gdlsn on gdlsn.salesorderlineid = sie.silid where sie.invoicedate >= '2016-01-01' and sie.invoicedate < '2016-01-03' -- add the following line, use a date that for sure will yield the rows: and gdlsn.created >= '2015-12-01' -- order by sie.invoicedate
getting duplicates after using distinct
I have three tables, one is mapping tale and I am using distinct keyword even though getting duplicate values SELECT mer.store_name, mpr.`merchant_code` , mpr.`terminal_num` , mpr.`rec_fmt` , mpr.`bat_nbr` , mpr.`Card_Type` , mpr.`card_num` , mpr.`transaction_date` , mpr.`settle_date` , mpr.`approval_code`, mpr.`intnl_amt` , mpr.`domestic_amt` , mpr.`transid` , mpr.`upvalue` , mpr.`merchant_trackid` , mpr.`MSF` , mpr.`service_tax` , mpr.`edu_cess` , mpr.`net_amount` , mpr.`debit_credit_type`, mpr.`UDF1` , mpr.`UDF2` , mpr.`UDF3` , mpr.`UDF4` , mpr.`UDF5` , mpr.`seq_num` , mpr.`arn_no` FROM `mpr_reports` mpr, merchantreports mer, storename_tid sid WHERE ( mer.Store_Name = sid.Store_Name AND sid.terminal_num = mpr.terminal_num AND mpr.`rec_fmt` = 'cvd' OR mpr.`rec_fmt` = 'bat' )
Consider amending the published query to something like this... SELECT mer.store_name , mpr.merchant_code , mpr.terminal_num , mpr.rec_fmt , mpr.bat_nbr , mpr.Card_Type , mpr.card_num , mpr.transaction_date , mpr.settle_date , mpr.approval_code , mpr.intnl_amt , mpr.domestic_amt , mpr.transid , mpr.upvalue , mpr.merchant_trackid , mpr.MSF , mpr.service_tax , mpr.edu_cess , mpr.net_amount , mpr.debit_credit_type , mpr.UDF1 , mpr.UDF2 , mpr.UDF3 , mpr.UDF4 , mpr.UDF5 , mpr.seq_num , mpr.arn_no FROM mpr_reports mpr JOIN storename_tid sid ON sid.terminal_num = mpr.terminal_num JOIN merchantreports mer ON mer.Store_Name = sid.Store_Name WHERE mpr.rec_fmt IN ('cvd','bat') ;
About SQL joins, you may have some fun here... It's the kind of thing we learn once, to use during our entire life. About your specific query, test this first example, SELECT mer.store_name, mpr.`merchant_code`, mpr.`terminal_num`, mpr.`rec_fmt`, mpr.`bat_nbr`, mpr.`Card_Type`, mpr.`card_num`, mpr.`transaction_date`, mpr.`settle_date`, mpr.`approval_code`, mpr.`intnl_amt`, mpr.`domestic_amt`, mpr.`transid`, mpr.`upvalue`, mpr.`merchant_trackid`, mpr.`MSF`, mpr.`service_tax`, mpr.`edu_cess`, mpr.`net_amount`, mpr.`debit_credit_type`, mpr.`UDF1`, mpr.`UDF2`, mpr.`UDF3`, mpr.`UDF4`, mpr.`UDF5`, mpr.`seq_num`, mpr.`arn_no` FROM merchantreports mer INNER JOIN storename_tid sid ON (sid.Store_Name = mer.Store_Name) INNER JOIN `mpr_reports` mpr ON (sid.terminal_num = mpr.terminal_num) WHERE mpr.rec_fmt IN ('cvd','bat'); Just a humble advice, consider using foreign keys to join your tables. Indexing columns "Store_Name" and "terminal_num" is also an option, but it'll be rather more efficient to follow what's written in the scriptures (see sec. 5.4.5).