I have a txt file on my local PC, this has to be check and then line by line uploaded into SQL Server 2016 using a Stored Procedure from MS Access using ADODB. It looks like Access is running always 2 rows fast and then making a short stop.
In MS Access I'm using this function:
Public Function ImportData(FileString As String)
Dim WholeLine As String
Dim cc As Variant
Dim sapPurchaseDocument As String
Dim sapPartNumber As String
Dim sapPartName As String
Dim sapDocumentDate As String
Dim sapSupplier As String
Dim sapPlant As String
Dim sapSLoc As String
Dim sapQuantity As Double
Dim sapUOM As String
Dim sapTargetQuantity As Double
Dim sapDeliveryDate As String
Dim sapPrevQuantity As Double
Dim sapReceivedQuantity As Double
Dim sapIssuedQuantity As Double
Dim sapDeliveredQuantity As Double
Dim sapPurchaseRequisition As String
Dim sapPurchaseRequisitionItem As String
Dim sapCreationIndicatior As String
Dim sapNoOfPositions As Double
Dim totalCount As Integer
Dim sapPurchaseDocumentItem As String
Dim rs As New ADODB.Recordset
Call GetConnection
Set rs.ActiveConnection = myCN
If Right(FileString, 3) = "txt" Then
totalCount = GetRowCount(FileString)
Open FileString For Input As #1
i = 0
Do While Not EOF(1)
Line Input #1, WholeLine
If Left(WholeLine, 3) = "| 4" Then
'Debug.Print WholeLine
cc = Split(WholeLine, "|")
sapPurchaseDocument = Trim(cc(1))
sapPartNumber = Trim(Replace(cc(2), ".", ""))
sapPartName = Trim(Replace(cc(3), "'", ""))
sapDocumentDate = Right(cc(4), 4) & "-" & Mid(cc(4), 4, 2) & "-" & Left(cc(4), 2)
sapSupplier = cc(5)
sapPlant = cc(6)
sapSLoc = cc(7)
sapQuantity = Replace(cc(8), ",", "")
sapUOM = Trim(cc(9))
sapTargetQuantity = Replace(cc(10), ",", "")
sapDeliveryDate = Right(cc(11), 4) & "-" & Mid(cc(11), 4, 2) & "-" & Left(cc(11), 2)
sapPrevQuantity = cc(12)
sapReceivedQuantity = Replace(cc(13), ",", "")
sapIssuedQuantity = Replace(cc(14), ",", "")
sapDeliveredQuantity = Replace(cc(15), ",", "")
sapPurchaseRequisition = Trim(cc(16))
sapPurchaseRequisitionItem = Trim(cc(17))
sapCreationIndicatior = cc(18)
sapNoOfPositions = cc(19)
sapPurchaseDocumentItem = Trim(cc(20))
strSQL = "spInsertUpdateSAPME2M '" & sapPurchaseDocument & "', '" & sapPartNumber & "', '" & sapPartName & "', '" & _
sapDocumentDate & "', '" & sapSupplier & "', '" & sapPlant & "', '" & sapSLoc & "', " & _
sapQuantity & ", '" & sapUOM & "', " & sapTargetQuantity & ", '" & sapDeliveryDate & "', " & _
sapPrevQuantity & ", " & sapReceivedQuantity & ", " & sapIssuedQuantity & ", " & _
sapDeliveredQuantity & ", '" & sapPurchaseRequisition & "', '" & sapPurchaseRequisitionItem & "', '" & _
sapCreationIndicatior & "', '" & sapNoOfPositions & "', '" & sapPurchaseDocumentItem & "'"
rs.Open (strSQL)
DoEvents
End If
i = i + 1
Debug.Print i
Forms!frm_Overview.lblStatus.Caption = "Record " & i & " of " & totalCount & " loaded. Please wait!"
DoEvents
'Refresh
Loop
MsgBox "Import done"
End If
Close #1
End Function
And on SQL Server I have a stored procedure which looks like this:
USE [MOBILEPRINT]
GO
/****** Object: StoredProcedure [dbo].[spInsertUpdateSAPME2M] Script Date: 5/25/2020 11:39:31 AM ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CHANGE NO ACTION
ALTER PROCEDURE [dbo].[spInsertUpdateSAPME2M]
-- Add the parameters for the stored procedure here
#sapPurchaseDocument varchar(50),
#sapPartNumber varchar(50),
#sapPartName varchar(300),
#sapDocumentDate date,
#sapSupplier varchar(50),
#sapPlant varchar(100),
#sapSLoc varchar(50),
#sapQuantity float,
#sapUOM varchar(50),
#sapTargetQuantity float,
#sapDeliveryDate date,
#sapPrevQuantity float,
#sapReceivedQuantity float,
#sapIssuedQuantity float,
#sapDeliveredQuantity float,
#sapPurchaseRequisition varchar(50),
#sapPurchaseRequisitionItem varchar(50),
#sapCreationIndicatior varchar(50),
#sapNoOfPositions varchar(50),
#sapPurchaseDocumentItem varchar(50)
AS
BEGIN TRANSACTION
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE #RESULT int
DECLARE #UPDATE_CHECK int
DECLARE #UpdateDate datetime = GetDate()
BEGIN
SELECT #RESULT = COUNT(sapPurchaseDocument) FROM SAP_ME2M WHERE sapPurchaseDocument = #sapPurchaseDocument AND sapPartNumber = #sapPartNumber
IF ISNULL(#RESULT,0) = 0
BEGIN
INSERT INTO SAP_ME2M (
sapPurchaseDocument,
sapPartNumber,
sapPartName,
sapDocumentDate,
sapSupplier,
sapPlant,
sapSLoc,
sapQuantity,
sapUOM,
sapTargetQuantity,
sapDeliveryDate,
sapPrevQuantity,
sapReceivedQuantity,
sapIssuedQuantity,
sapDeliveredQuantity,
sapPurchaseRequisition,
sapPurchaseRequisitionItem,
sapCreationIndicatior,
sapNoOfPositions,
ChangeDate,
sapPurchaseDocumentItem)
VALUES
(#sapPurchaseDocument, #sapPartNumber, #sapPartName, #sapDocumentDate, #sapSupplier, #sapPlant,
#sapSLoc, #sapQuantity, #sapUOM, #sapTargetQuantity, #sapDeliveryDate, #sapPrevQuantity,
#sapReceivedQuantity, #sapIssuedQuantity, #sapDeliveredQuantity, #sapPurchaseRequisition,
#sapPurchaseRequisitionItem, #sapCreationIndicatior, #sapNoOfPositions, #UpdateDate, #sapPurchaseDocumentItem)
END
ELSE
SELECT #UPDATE_CHECK = COUNT(*) FROM SAP_ME2M WHERE
sapPurchaseDocument = #sapPurchaseDocument AND
sapPartNumber = #sapPartNumber AND
sapPartName = #sapPartName AND
sapDocumentDate = #sapDocumentDate AND
sapSupplier = #sapSupplier AND
sapPlant = #sapPlant AND
sapSLoc = #sapSLoc AND
sapQuantity = #sapQuantity AND
sapUOM = #sapUOM AND
sapTargetQuantity = #sapTargetQuantity AND
sapDeliveryDate = #sapDeliveryDate AND
sapPrevQuantity = #sapPrevQuantity AND
sapReceivedQuantity = #sapReceivedQuantity AND
sapIssuedQuantity = #sapIssuedQuantity AND
sapDeliveredQuantity = #sapDeliveredQuantity AND
sapPurchaseRequisition = #sapPurchaseRequisition AND
sapPurchaseRequisitionItem = #sapPurchaseRequisitionItem AND
sapCreationIndicatior = #sapCreationIndicatior AND
sapNoOfPositions = #sapNoOfPositions AND
sapPurchaseDocumentItem = #sapPurchaseDocumentItem
IF #UPDATE_CHECK = 0
BEGIN
UPDATE SAP_ME2M SET
sapPartName = #sapPartName ,
sapDocumentDate = #sapDocumentDate ,
sapSupplier = #sapSupplier ,
sapPlant = #sapPlant ,
sapSLoc = #sapSLoc ,
sapQuantity = #sapQuantity ,
sapUOM = #sapUOM ,
sapTargetQuantity = #sapTargetQuantity ,
sapDeliveryDate = #sapDeliveryDate ,
sapPrevQuantity = #sapPrevQuantity ,
sapReceivedQuantity = #sapReceivedQuantity ,
sapIssuedQuantity = #sapIssuedQuantity ,
sapDeliveredQuantity = #sapDeliveredQuantity ,
ChangeDate = #UpdateDate
WHERE
sapPartNumber = #sapPartNumber AND
sapPartName = #sapPartName AND
sapDocumentDate = #sapDocumentDate AND
sapSupplier = #sapSupplier AND
sapPlant = #sapPlant AND
sapSLoc = #sapSLoc AND
sapPurchaseDocumentItem = #sapPurchaseDocumentItem
END
END
COMMIT TRANSACTION WITH (DELAYED_DURABILITY = ON);
I have to upload around 30000 Records which takes more then an hour at the moment.
If you have suggestions, please let me know.
For fast data transfer, use a disconnected recordset with batch operations enabled.
Dim conn As ADODB.Connection
Call GetConnection
Set conn = myCN
Dim rs As New ADODB.Recordset
rs.CursorLocation = adUseClient
rs.Open "Table1", conn, adOpenForwardOnly, adLockBatchOptimistic
'Disconnect
Set rs.ActiveConnection = Nothing
Dim i As Long
For i = 1 To 3000
rs.AddNew
rs.Fields(1) = i
Next
'Reconnect
Set rs.ActiveConnection = conn
'Batch insert
rs.UpdateBatch
Debug.Print Now()
For me, this executes in 2 seconds, but it highly depends on the location of SQL server.
Then, process further on the data set when uploaded. Processing on a per-record basis is usually going to be slow.
We have a VBScript that downloads chunks of data from an SAP Business Object database into so-called slices, which are basically .csv files. The script worked perfectly so far, I haven't really had to look into it at all. But the failure now is this:
The script file section this error refers to is the dbConn.Execute(strSQL) line in the below code (5th from below).
What I tried so far, was to add these commands but they don't seem to solve anything:
'dbConn.ConnectionTimeout = 100
'dbConn.CommandTimeout = 100
The script itself (not all of it, I'm not sure the rest is needed):
Sub subRunFilesInFolder(strFolder)
Dim FSO, objFolder, objFiles
Dim i, intTS, intTS_file_start, ts, tsKillBefore, TS_file_start, strModelName
Dim dbConn, RST, RST2, strSQL
Dim strVBSmodel
Dim blRunIt
'INIs
strModelName = "bo_vbs_runner_1.5 "
strConn = "DRIVER={SQL Server};SERVER=EUBASEURCIREP01;UID=ser_login;PWD=ser_login;DATABASE=ser"
strComputer = FunstrComputerName
strBORunner = "\\Eubaseurcirep01\reporting\DEVELOPMENT\BO\Automation\Models\BO_auto_run.xlsb"
'Sets
Set dbConn = CreateObject("ADODB.Connection")
Set RST = CreateObject("ADODB.RecordSet")
Set RST2 = CreateObject("ADODB.RecordSet")
Set WshShell = WScript.CreateObject("WScript.Shell")
Set FSO = Wscript.CreateObject("Scripting.FileSystemObject")
Set objFolder = FSO.GetFolder(strFolder)
Set objFiles = objFolder.Files
Set appExcel = CreateObject("Excel.Application")
'dbConn.ConnectionTimeout = 100
'dbConn.CommandTimeout = 100
strVBSmodel = strModelName & strComputer & " " & FunstrUserName & " " & funCurrProcessId & " " & FunGetProcessIDCurrentOfExcel(strComputer)
appExcel.Application.Visible = False
appExcel.Displayalerts = False
Set objBORunner = appExcel.Workbooks.Open(strBORunner)
dbConn.Open strConn
ts = FunGetServerNow(dbConn,RST)
tsKillBefore = DateAdd("N", -15, ts)
intTS = funTimeStampToInteger(FunGetServerNow(dbConn, RST))
'Get ReportDate
strSQL = "SELECT yyyymmdd FROM map.reportdate WHERE dtAct=cast(GETDATE() as DATE);"
RST.Open strSQL, dbConn
If RST.EOF Then
strReportDate="99991231"
Else
strReportDate=RST.fields(0).value
End If
RST.close
'Kill stucked excel and vbs processes
strSQL = "SELECT distinct * FROM [ser].[bo].[_log] WHERE [proc]='BO VBS' AND result_text='started' AND end_timestamp<" & funTimeStampToInteger(tsKillBefore) & _
" AND lower(model) like '% " & LCase(strComputer) & " %';"
RST.Open strSQL,dbConn
If RST.EOF Then 'Nothing to kill
Else
Do While Not RST.EOF
strOldVBS = split(RST.fields("model"), " ")(3)
strOldExcel = split(RST.fields("model"), " ")(4)
Call SubKillProcessIDOnstrComputer(strComputer, strOldVBS)
Call SubKillProcessIDOnstrComputer(strComputer, strOldExcel)
strSQL = "UPDATE [ser].[bo].[_log] SET result_text='stopped', end_timestamp='" & funTimeStampToInteger(FunGetServerNow(dbConn,RST2)) & "' " & _
"WHERE [proc]='BO VBS' AND result_text='started' AND model='" & RST.fields("model").value & "' AND parameters='" & _
RST.fields("parameters").value & "';"
dbConn.Execute(strSQL)
RST.MoveNext
Loop
End If
RST.close
To Decode 0x8004nnnn Errors
HResults with facility code 4 means the HResult contains OLE errors (0x0 =
to 0x1ff) while the rest of the range (0x200 onwards) is component =
specific errors so 20e from one component will have a different meaning =
to 20e from another component.
You are lucky as your component is telling you it's OLDB with it's error - TIMEOUT
I have a dilemma when reading from a very large look-up table (1000+ lines) that contains challenge phrases to determine if a file name (obtained from another delimited list) contains 3 of my 6 phrases. My If statement works only if it match's the challenge but I also need it to work if it only meets 1 specific phrase also from the look-up table. The look-up table example shows possible destination for only 1 user where I will have 100+ users to check against. I can get the correct result if it matches all of my challenges but if it does not it cycles through the rest of the lines containing the same user. Any suggestions to create a break would be much appreciated.
Below is the code I'm working with right now.
'Flags for files
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Dim strFDate: strFDate = DatePart("yyyy", Now) & Right("0" & DatePart("m",Date), 2) & Right("0" & DatePart("d",Date), 2) 'Formats date to YYYYMMDD
Dim strDate: strDate = Date 'Date with DD/MM/YYYY format
Set fso = CreateObject("Scripting.FileSystemObject")
Set fso2 = WScript.CreateObject("Scripting.Filesystemobject")
'Check for %Client%.csv then record file data
Dim strTriggerName: strTriggerName = InputBox("Enter the User name for Row Count, ie. test", "Enter File Path and File Name")
Dim FileDest: FileDest = "C:\Test" 'Destination for moves and extracts must be in "C:\Test" format not ending with "\"
'Check if output.csv exist if not create it
Set objFSO1 = CreateObject("Scripting.FileSystemObject")
If objFSO1.FileExists(srtMovePath & "\output.csv") Then
'Msgbox srtMovePath & "\output.csv Exists"
Else
Set objTXTFILE = objFSO1.CreateTextFile(srtMovePath & "\output.csv")
'Msgbox srtMovePath & "\output.csv Created"
objTXTFILE.close
End If
Set f = fso2.OpenTextFile("c:\output.csv", 2)
Set objFSO2 = CreateObject("Scripting.FileSystemObject")
Set objTextTrigger = objFSO2.OpenTextFile("d:\File_Receipt_Triggers\" & strTriggerName & ".trigger", 1)
If (fso.fileexists("d:\File_Receipt_Triggers\" & strTriggerName & ".trigger")) Then
'Read csv for Client Name, File Name and File Path
Set objFSO2 = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO2.OpenTextFile("d:\File_Receipt_Triggers\" & strTriggerName & ".csv", 1)
Do until objTextFile.AtEndOfStream
Dim arrStr: arrStr = Split(objTextFile.ReadLine, "|")
Dim strClient: strClient = arrStr(0)
Dim strFileName: strFileName = arrStr(1)
Dim strFullPath: strFullPath = arrStr(2)
'Compare file name to lookup table If strFileName contains (User & (C1 or C2) & (C3 or C4) & (C5 or C6)) then Destination
Set objFSO8 = CreateObject("Scripting.FileSystemObject")
Set objMoveTable = objFSO8.OpenTextFile("d:\Move_Lookup_Table.csv", 1)
Do until objMoveTable.AtEndOfStream
Dim arrStr2: arrStr2 = Split(objMoveTable.ReadLine, ",")
Dim User: User = arrStr2(0)
Dim C1: C1 = arrStr2(1)
Dim C2: C2 = arrStr2(2)
Dim C3: C3 = arrStr2(3)
Dim C4: C4 = arrStr2(4)
Dim C5: C5 = arrStr2(5)
Dim C6: C6 = arrStr2(6)
Dim Client: Client = arrStr2(7)
Dim Bank: Bank = arrStr2(8)
Dim OP: OP = arrStr2(9)
Dim Month: Month = arrStr2(10)
Dim Year: Year = arrStr2(11)
Dim strDestination: strDestination = arrStr2(12)
'ISSUE AREA
If ((strTriggerName) = User ) and (Instr( Ucase(strFileName), C1 ) <> 0 or Instr( Ucase(strFileName), C2 ) <> 0) and (Instr( Ucase(strFileName), C3 ) <> 0 or Instr( Ucase(strFileName), C4 ) <> 0) and (Instr( Ucase(strFileName), C5 ) <> 0 or Instr( Ucase(strFileName), C6 ) <> 0) Then
'msgbox strFileName & " is Good"
'<Add code here>
msgbox strFileName & " Should = " & " User: " & User & " and " & C1 & " or " & C2 & " and " & C3 & " or " & C4 & " and " & C5 & " or " & C6 & " Client: " & Client & " Bank: " & Bank & " OP: " & OP & " Month: " & Month & " Year: " & Year & " Dest: " & "\\nas2\BAIArchive\BAI" & strDestination & Bank
ElseIf ((strTriggerName) = User ) and Not
'msgbox strFileName & " does not match"
'<Add code to here>
End If
Loop
Loop
End If
An Example of my look-up table is here:
User,Contains1,Contains2,Contains3,Contains4,Contains6,Contains5,strClient,StrBank,strOP,strMonth,strYear,strDestination
test,DPB,DB,5,May,2013,13,BAI,TEST,DPB,May,2013,\2013May\DPB\
test,DPB,DB,11,Nov,2013,13,BAI,TEST,DPB,Nov,2013,\2013Nov\DPB\
test,DPB,DB,5,May,2014,14,BAI,TEST,DPB,May,2014,\2014May\DPB\
test,DPB,DB,11,Nov,2014,14,BAI,TEST,DPB,Nov,2014,\2014Nov\DPB\
test,DPB,DB,5,May,2015,15,BAI,TEST,DPB,May,2015,\2015May\DPB\
test,DPB,DB,11,Nov,2015,15,BAI,TEST,DPB,Nov,2015,\2015Nov\DPB\
test,NII,CTS,5,May,2013,13,BAI,TEST,NII,May,2013,\2013May\NII\
test,NII,CTS,11,Nov,2013,13,BAI,TEST,NII,Nov,2013,\2013Nov\NII_CTS\
test,NII,CTS,5,May,2014,14,BAI,TEST,NII,May,2014,\2014May\NIICTS\
test,NII,CTS,11,Nov,2014,14,BAI,TEST,NII,Nov,2014,\2014Nov\NIICTS\
test,NII,CTS,5,May,2015,15,BAI,TEST,NII,May,2015,\2015May\NIICTS\
test,NII,CTS,11,Nov,2015,15,BAI,TEST,NII,Nov,2015,\2015Nov\NIICTS\
test,SBB,SB,5,May,2013,13,BAI,TEST,SBB,May,2013,\2013May\SBB\
test,SBB,SB,11,Nov,2013,13,BAI,TEST,SBB,Nov,2013,\2013Nov\SBB\
test,SBB,SB,5,May,2014,14,BAI,TEST,SBB,May,2014,\2014May\SBB\
test,SBB,SB,11,Nov,2014,14,BAI,TEST,SBB,Nov,2014,\2014Nov\SBB\
test,SBB,SB,5,May,2015,15,BAI,TEST,SBB,May,2015,\2015May\SBB\
test,SBB,SB,11,Nov,2015,15,BAI,TEST,SBB,Nov,2015,\2015Nov\SBB\