Excel VBA ADODB SQL connection doesn't run the whole execute command - excel
Hey there I have a weird problem which I can't find its source. I have a messy code below which takes user input from one of the excel pages(kinda like a form) and creates a long sql query.
Everytime SQLstrL command gets updated within the Do While it basically adds another INSERT command to the SQLstrL string.
When I copy the sql query which VBA created via a function and paste on SSMS everything works like a charm. So I know there is nothing wrong with the command text. But when I try to execute from cn.Execute like below, It only executes first 4 INSERT commands of SQLstrL and ignores the rest. I have up to 60 INSERT commands within SQLstrL string.
I have no idea why this bizarre problem occurs. If anyone can help me I'll be most appreciated.
SQLStr = "DECLARE #newdatetime datetime; " & _
"DECLARE #newdatetimelong datetime; " & _
"DECLARE #timeint int; " & _
"DECLARE #newficheno varchar(17); " & _
"DECLARE #newlogref int; " & _
"SET #newficheno=(SELECT REPLICATE('0',LEN(LASTASGND)-LEN(LASTASGND+1))+REPLACE(STR(CONVERT(varchar,LASTASGND)+1),' ','') FROM L_LDOCNUM WHERE FIRMID=500 AND APPMODULE=1 AND DOCIDEN=12); " & _
"UPDATE L_LDOCNUM SET LASTASGND=#newficheno FROM L_LDOCNUM WHERE FIRMID=500 AND APPMODULE=1 AND DOCIDEN=12; " & _
"SET #newdatetime=CONVERT(datetime,'" & DateInp & "',104); " & _
"SET #timeint=LOGODB.dbo.LG_TIMETOINT(DATEPART(hour,GETDATE()),DATEPART(minute,GETDATE()),DATEPART(second,GETDATE())); " & _
"SET #newdatetimelong=#newdatetime+LOGODB.dbo.LG_INTTOTIME(#timeint); " & _
"INSERT INTO LG_500_01_STFICHE " & _
"SELECT 3,12,3, " & _
"#newficheno, " & _
"#newdatetime, " & _
"#timeint, " & _
"'','','','',0,0,0,0,0,0,'',0, " & _
"0,/*SOURCEINDEX*/ " & _
"0,0,0,/*SOURCECSTGRP*/ " & _
"0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'','','','','','',1,0,0,0,0,0,0,1,#newdatetimelong,DATEPART(hour,#newdatetimelong),DATEPART(minute,#newdatetimelong),DATEPART(second,#newdatetimelong),0,NULL,0,0,0,0,0,'','','',/*TRACKNR*/ " & _
"3,0,'',0,0,1,0,0,0,0,0,0,'" & Worksheets("Form").Range("M1").Text & "',/*DOCTRACKINGNR*/ " & _
"0,0,'',0,0,0,'',0,'',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,NULL,0,#newdatetime,#timeint,0,0,0,NEWID(),'',0,0,0,'',0,0,0,0,0,0,NULL,0,0,0,#newdatetime,#timeint, " & _
"0,0,'',0,'','',NULL,0,0,0,'','','','',0,0,'',0,NULL,0,NULL,0,0,0,0,0,0; " & _
"SET #newlogref=(SELECT LOGICALREF FROM LG_500_01_STFICHE WHERE TRCODE=12 AND FICHENO=#newficheno);"
Startsearch = Worksheets("Form").Range("A3").Address
Do While IsNumeric(Worksheets("Form").Range(Startsearch).Value) And Worksheets("Form").Range(Startsearch).Value <> ""
SQLStrL = SQLStrL & " INSERT INTO LG_500_01_STLINE " & _
" SELECT " & _
CDbl(Application.VLookup(Worksheets("Form").Range(Startsearch).Offset(0, 1).Text, Worksheets("Destek").ListObjects("Malzemeler").Range.Offset(0, 2), 5, False)) & "," & _
" 0,0,0,0,12,#newdatetime,#timeint,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,#newlogref, " & _
CDbl(Worksheets("Form").Range(Startsearch).Value) & "," & _
" 0,0,0,0,0,0,0,406,0,0,0,0,0,0,0,'','', " & _
Replace(CDbl(Worksheets("Form").Range(Startsearch).Offset(0, 12).Value), ",", ".") & "," & _
" 0,0,0,0,0,0,1,0,0,0,0,0,'" & Worksheets("Form").Range(Startsearch).Offset(0, 6).Text & "', " & _
CDbl(Application.VLookup(CDbl(Application.VLookup(Worksheets("Form").Range(Startsearch).Offset(0, 1).Text, Worksheets("Destek").ListObjects("Malzemeler").Range.Offset(0, 2), 4, 0)) & "-" & Worksheets("Form").Range(Startsearch).Offset(0, 13).Text, Worksheets("Destek").ListObjects("UnitSet").Range, 5, 0)) & "," & _
CDbl(Application.VLookup(Worksheets("Form").Range(Startsearch).Offset(0, 1).Text, Worksheets("Destek").ListObjects("Malzemeler").Range.Offset(0, 2), 4, False)) & "," & _
" 1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'',0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'','',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, " & _
" DATEPART(month,#newdatetimelong),DATEPART(year,#newdatetimelong), " & _
" 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,NULL,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'',0,0,NEWID(),'',0,0,'','',0,0,0,0,'',0,'','',0,0,'','',0,0,0,0,NULL,0,0,0,0,0,0,'','',0,0,0,0,0,0,NULL,0,0,0,0,0,0,0,0,0,'','','',NULL,0,0,0,0,0,0,0,0,0; " & vbNewLine
Startsearch = Worksheets("Form").Range(Startsearch).Offset(1, 0).Address
Loop
Set cn = New ADODB.Connection
cn.Open "Driver={SQL Server};Server=" & Server_Name & ";Database=" & Database_Name & _
";Uid=" & User_ID & ";Pwd=" & Password & ";"
cn.Execute SQLStr & SQLStrL, , ADODB.ExecuteOptionEnum.adExecuteNoRecords
Related
VBA Adodb : concatenation of values
I would like to use in my query the key like this: '"prod.cd_produit"'||'"/"'||'" & strQ & "'. Here, the values of my variables are: prod.cd_produit= 53 and & strQ & =350, so I would like to have 53/350 as a key. I'm wondering if it's right to write '"prod.cd_produit"'||'"/"'||'" & strQ & "' (I don't want to have any spaces neither at right nor at left). This is a part of my code : Public Sub INFO_PROTO34(ByRef strQ As String) ........................................... " sousc.lp_etat_doss not in ('ANNUL','A30','IMPAY') and sousc.is_produit = prod.is_produit" & _ " and '"prod.cd_produit"'||'"/"'||'" & strQ & "' = proto.cd_protocole ", Thank you very much for your help!
Like this: RECSET.Open " select proto.b_perf_cma as b_perf_cma from db_dossier sousc,db_produit prod, " & _ " db_protocole proto where sousc.no_police = '" & numero_de_police & "' " & _ " and sousc.cd_dossier = 'SOUSC' " & _ " and sousc.lp_etat_doss not in ('ANNUL','A30','IMPAY') " & _ " and sousc.is_produit = prod.is_produit " & _ " and prod.cd_produit||'/'||'" & strQ & "' = proto.cd_protocole ", _ cnn_Pegase, adOpenDynamic, adLockBatchOptimistic helps to spend some time formatting your SQL so it's more readable (for us and you...)
Excel VBA #SQL String filter Multiple LIKE Condition
This code works as intended but I also need to exclude Autoreplies and some other strings in a subject line. I need to know if it's possible to add 2 or 3 more Likes. I tried And and Or conditions but I'm getting a parse error. This code below works. I just need to add another string condition after undeliverable. " Like '%Automatic reply%'". Filter = "#SQL=" & " Not " & _ "urn:schemas:httpmail:subject" & "" & _ " Like '%undeliverable%'" thanks.
OR Should work, The following Example have either %undeliverable% Or %Automatic reply% Filter = "#SQL=" & " Not (urn:schemas:httpmail:subject" & _ " Like '%undeliverable%' Or " & _ "urn:schemas:httpmail:subject" & _ " Like '%Automatic reply%')" To Add one more Filter Filter = "#SQL=" & " Not (urn:schemas:httpmail:subject" & _ " Like '%undeliverable%' Or " & _ "urn:schemas:httpmail:subject" & _ " Like '%Automatic reply%' Or " & _ "urn:schemas:httpmail:subject" & _ " Like '%Bla Bla%')"
Excel VBA #SQL String filter
I have been struggling on making this statement a NOT statement: strfilter = "#SQL=" & Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " like '%undeliverable%'" this strfilter is being used in a items.restrict(strfilter), so obviously it's checking for all emails that contains the word undeliverable. What I need to do is convert this statement so that it will EXCLUDE all emails with the subject containing the Words "undeliverable". I've tried this but it returned a parse error: "strFilter = "#SQL=" & Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " NOT '%undeliverable%'" thanks in advance.
The Correct syntax is Filter = "#SQL=" & " Not " & _ "urn:schemas:httpmail:subject" & "" & _ " Like '%undeliverable%'" Or I prefer this then Like strFilter = "#SQL=" & " Not " & _ "urn:schemas:httpmail:subject" & "" & _ " ci_phrasematch 'undeliverable'"
Excel VBA & Access Query - Insert data into Table from another Table and data from Excel sheet
I am using Excel VBA to transfer data from Access table 1 to table 2 with Insert Into query. Access query below work fine: "INSERT INTO " & _ "tblTable2 ([Col_1], [Col_2], [Col_3]) " & _ "SELECT " & _ "tblTable1.[Data_1], tblTable1.[Data_2], tblTable1.[Data_3] " & _ "FROM " & _ "tblTable1 " & _ "WHERE " & _ "tblTable1.[Data_1] = " & Sheet1.Range("D3").Value Than Table 2 was added new column (Col_4) which the data will be from excel sheet directly. I do written the code (shown below) and it is not work. "INSERT INTO " & _ "tblTable2 ([Col_4], [Col_1], [Col_2], [Col_3]) " & _ "VALUES (" & sheet2.Range("F1").value & ", " & _ "(SELECT " & _ "tblTable1.[Data_1], tblTable1.[Data_2], tblTable1.[Data_3] " & _ "FROM " & _ "tblTable1 " & _ "WHERE " & _ "tblTable1.[Data_1] = " & Sheet1.Range("D3").Value & "))" Is any code above was inaccurate?
Convert strings to booleans
I need to import data from excel to access. The importation is now "working" for the obvious types (string, integers). However, In the excel file i have some strings that i need to convert to boolean in my access tables. The strings can take only 2 values "oui" or "non" (yes or no in french). In my vba code i have the following line: cSQL = "INSERT INTO " & strTable & " ( [N_CLIENT], [NOM_CLI], ) VALUES (" & Chr(34) & .Range("A" & i) & Chr(34) & "," & Chr(34) & .Range("F" & i) & Chr(34) & ");" DoCmd.RunSQL cSQL I would liketo know if i can use an if condition to check the value itself inside the cSQL call and replace it with either true or false. like what follows. cSQL = "INSERT INTO " & strTable & " ( [N_CLIENT], [NOM_CLI], ) VALUES (If(" & Chr(34) & .Range("A" & i) & Chr(34) & " = "oui") then "true" else then "false"," & Chr(34) & .Range("F" & i) & Chr(34) & ");" DoCmd.RunSQL cSQL
You can use VBA-Functions in Access-SQL-Queries: "[...] VALUES(strcomp('" & Range("A" & i) & "','oui', 1)=0 [...]" Regards, AKDA
It looks like you want the Access SQL to evaluate your oui / non into true and false. You could do this using the iif statement http://www.techonthenet.com/access/functions/advanced/iif.php. However, why bother? You could just evaluate the range in vba and pass through the boolean variable. Using something like this: dim result as boolean if sheet1.Range("A" & i) = "oui" then result = true else result = false end if Then just insert that into your SQL: cSQL = "INSERT INTO " & strTable & " ( [N_CLIENT], [NOM_CLI], ) VALUES (If(" & Chr(34) & result & Chr(34) & , & Chr(34) & .Range("F" & i) & Chr(34) & ");"
You could also use the IIF statement in SQL to check the value: http://msdn.microsoft.com/en-us/library/hh213574.aspx
It is easier to use apostrophes rather than Chr(34), and splitting the statement across lines using the line-continuation character '_' helps to read the statement. The IIf() function is also used in the following. Also note that you have an extra comma after the term [NOM_CLI] which shouldn't be there. cSQL = "INSERT INTO " & strTable & "( [N_CLIENT], [NOM_CLI] ) VALUES ('" _ & IIf(StrComp(.Range("A" & i), "oui", 1) = 0, "true", "false") _ & "','" & .Range("F" & i) & "');" This results in a string like this: INSERT INTO ( [N_CLIENT], [NOM_CLI] ) VALUES ('true','hello'); To use the Boolean values (0, -1) change to: cSQL = "INSERT INTO " & strTable & "( [N_CLIENT], [NOM_CLI] ) VALUES (" _ & IIf(StrComp(.Range("A" & i), "oui", 1) = 0, -1, 0) _ & ",'" & .Range("F" & i) & "');" which yields INSERT INTO ( [N_CLIENT], [NOM_CLI] ) VALUES (-1,'hello');