Access VBA update value in array? - arrays

I have an issue with a project I am working on and updating a string within an array when a loop is initiated.
To provide a little context, the code is to amend errors that user's may have made when entering information and to update all the related tables in the one go.
Option Compare Database
Dim Var1 As Variant, SQL_select As Variant, SQL_update As Variant, newVar1 As String
Dim j As Integer, rsField As Variant, n As Variant
Public Sub amendCust_control()
Dim db As Database, rs As Recordset, cntRec As Integer, i As Integer, SQL_s As String, SQL_u As String
For j = 0 To 3
Var1 = Forms!mm_amendcustomer_temp!Var1
newVar1 = Forms!mm_amendcustomer_temp!Text50
assSelArray
SQL_s = SQL_select(j)
Set db = CurrentDb
Set rs = db.OpenRecordset(SQL_s)
If rs.EOF Then
cntRec = 0
Else
rs.MoveLast
cntRec = rs.RecordCount
End If
If cntRec > 0 Then
ReDim n(0 To (cntRec - 1))
rs.MoveFirst
For i = 0 To (cntRec - 1)
n(i) = rs.Fields(rsField(j))
assignSQLuparray 'populates update field with current value of n(j)
SQL_u = SQL_update(j)
Set SQL_update = Nothing
rs.MoveNext
Next i
Else
Exit Sub
End If
Set rs = Nothing
Set db = Nothing
Next j
Set rs = Nothing
Set db = Nothing
End Sub
I have then split my three string arrays between 2 subs, as below.
Private Sub assSelArray()
SQL_select = Array(("SELECT [Var1] FROM [Customer] WHERE [Var1] = '" & Var1 & "'"), _
("SELECT [transNo] FROM [Transactions] WHERE [Var1] = '" & Var1 & "'"), _
("SELECT [VAR_O] FROM [Overpayment] WHERE [Var1] = '" & Var1 & "'"), _
("SELECT [Access ID] FROM [Audit History] WHERE [Var1] = '" & Var1 & "'"))
rsField = Array("Var1", "transNo", "VAR_O", "[Access ID]")
End Sub
This is the array I am having difficulty with as the values of n(i) do not update on each iteration on the code, i.e. when n(i) changes value in the main sub, it does not do so in the array.
Private Sub assignSQLuparray()
SQL_update = Array(("UPDATE [Customer] SET Var1 = '" & newVar1 & "' WHERE Var1 = '" & n(i) & "'"), _
("UPDATE [Transactions] SET Var1 = '" & newVar1 & "' WHERE transNo = '" & n(i) & "'"), _
("UPDATE [Overpayment] SET Var1 = '" & newVar1 & "' WHERE VAR_O = '" & n(i) & "'"), _
("UPDATE [Audit History] SET Var1 = '" & newVar1 & "' WHERE [Access ID] = '" & n(i) & "'"))
End Sub
I had hoped someone may be able to help me to essentially 'refresh' the array values before calling them. Help?
The output was as below:
n(0) = 5
UPDATE [Audit History] SET Var = newVar1 WHERE [Access ID] = '5'
n(1) = 6
UPDATE [Audit History] SET Var = newVar1 WHERE [Access ID] = '5'
n(2) = 7
UPDATE [Audit History] SET Var = newVar1 WHERE [Access ID] = '5'
n(3) = 8
UPDATE [Audit History] SET Var = newVar1 WHERE [Access ID] = '5'
n(4) = 11
UPDATE [Audit History] SET Var = newVar1 WHERE [Access ID] = '5'
Notice the Access ID does not change as it should to n(0 to 4).

Related

Import from txt (MS Access 2013) to SQL Server 2016 slow

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.

MS Access - how can i aggregate two queries in one recordset?

My query works in VBA code, but I will have a lot of this kind of queries, so I don't want to create a new "rst" section every time. (recordset).
The code I created is below.
Private Sub wpr_krotkaNazwaProjektu_AfterUpdate()
Dim rst4 As DAO.Recordset
Dim rst5 As DAO.Recordset
Dim strSql4 As String
Dim strSql5 As String
Dim krotkaNazwaProjektu4 As String
Dim krotkaNazwaProjektu5 As String
krotkaNazwaProjektu4 = wpr_krotkaNazwaProjektu.Text
krotkaNazwaProjektu5 = wpr_krotkaNazwaProjektu.Text
strSql4 = "SELECT Ewidencje.E_dataRozpoczeciaProjektu from Ewidencje INNER JOIN KP_KartyProjektow on Ewidencje.ID_kartyProjektu = KP_KartyProjektow.ID_kartyProjektu WHERE KP_KartyProjektow.KP_krotkaNazwaProjektu = '" & (krotkaNazwaProjektu4) & "' "
strSql5 = "SELECT Ewidencje.E_dataPlanowaneZakonczenieProjektu from Ewidencje INNER JOIN KP_KartyProjektow on Ewidencje.ID_kartyProjektu = KP_KartyProjektow.ID_kartyProjektu WHERE KP_KartyProjektow.KP_krotkaNazwaProjektu = '" & (krotkaNazwaProjektu5) & "' "
Set rst4 = CurrentDb.OpenRecordset(strSql4)
Set rst5 = CurrentDb.OpenRecordset(strSql5)
przypisanie4 = rst4!E_dataRozpoczeciaProjektu
przypisanie5 = rst5!E_dataPlanowaneZakonczenieProjektu
rst4.Close
Set rst4 = Nothing
rst5.Close
Set rst5 = Nothing
wpr_planowanaDS.Value = przypisanie4
wpr_planowanaDZ.Value = przypisanie5
End Sub
I don't want to open "rst" every time, whether such a query can be modified in such a way that it won't create many variables in ten queries.
I don't know if I understand you correctly.
Using the next code you should be able to do the same just using one variable for each thing:
Private Sub wpr_krotkaNazwaProjektu_AfterUpdate()
Dim rst As DAO.Recordset
Dim strSql As String
Dim krotkaNazwaProjektu As String
krotkaNazwaProjektu = wpr_krotkaNazwaProjektu.Text
strSql = "SELECT Ewidencje.E_dataRozpoczeciaProjektu from Ewidencje INNER JOIN KP_KartyProjektow on Ewidencje.ID_kartyProjektu = KP_KartyProjektow.ID_kartyProjektu WHERE KP_KartyProjektow.KP_krotkaNazwaProjektu = '" & krotkaNazwaProjektu & "' "
Set rst = CurrentDb.OpenRecordset(strSql)
przypisanie = rst!E_dataRozpoczeciaProjektu
rst.Close
wpr_planowanaDS.Value = przypisanie
strSql = "SELECT Ewidencje.E_dataPlanowaneZakonczenieProjektu from Ewidencje INNER JOIN KP_KartyProjektow on Ewidencje.ID_kartyProjektu = KP_KartyProjektow.ID_kartyProjektu WHERE KP_KartyProjektow.KP_krotkaNazwaProjektu = '" & krotkaNazwaProjektu & "' "
Set rst = CurrentDb.OpenRecordset(strSql)
przypisanie = rst!E_dataPlanowaneZakonczenieProjektu
rst.Close
wpr_planowanaDZ.Value = przypisanie
Set rst = Nothing
End Sub
The use of non-English names makes it impossible to figure out what your data is about but you could try something like this:
"SELECT IIF(KP_KartyProjektow.KP_krotkaNazwaProjektu = '" & (krotkaNazwaProjektu4) & "',Ewidencje.E_dataRozpoczeciaProjektu,NULL) AS KP_krotkaNazwaProjektu
IIF(KP_KartyProjektow.KP_krotkaNazwaProjektu = '" & (krotkaNazwaProjektu5) & "',Ewidencje.E_dataPlanowaneZakonczenieProjektu,NULL) AS E_dataPlanowaneZakonczenieProjektu
FROM Ewidencje
INNER JOIN KP_KartyProjektow on Ewidencje.ID_kartyProjektu = KP_KartyProjektow.ID_kartyProjektu
WHERE KP_KartyProjektow.KP_krotkaNazwaProjektu = '" & (krotkaNazwaProjektu4) & "' OR KP_KartyProjektow.KP_krotkaNazwaProjektu = '" & (krotkaNazwaProjektu5) & "'"
It combines your two queries into one. It can probably be simplified quite a bit if I understood what it is about and which field names are unique between the two linked tables and there may be a quote missing here or there that you may have to troubleshoot.

MSACCESS CODE convert to SQL

hi i would like to ask for your Help i have an MSACCESS code and i want to convert it to SQL (SSMS) please see code below.
Private Sub updateDuplicateNetwork()
Dim rstTemp As DAO.Recordset
Dim rst As DAO.Recordset
Dim strPIN As String
Dim strContract As String
Dim strNetwork As String
Set rstTemp = CurrentDb.OpenRecordset("qry_HR_temp_Consultant_Data_DuplicateNetwork_EachValue")
Set rst = CurrentDb.OpenRecordset("tbl_HR_Contract_Detail")
If Not rstTemp.EOF Then
rstTemp.MoveFirst
Do While Not rstTemp.EOF
If rstTemp!strNetwork <> strNetwork Or rstTemp!strNetwork <> strNetwork Or rstTemp!strNetwork <> strNetwork Then
strPIN = rstTemp!strPIN
strContract = rstTemp!strContract
strNetwork = rstTemp!strNetwork
rst.FindFirst ("[SAP_PIN] = '" & strPIN & "' AND [Contract] = '" & strContract & "' AND [Network] = '" & strNetwork & "'")
If Not rst.NoMatch Then
rst.Edit
rst!Allocated_Hrs = rstTemp!F38
rst.Update
End If
Else
rst.FindNext ("[SAP_PIN] = '" & strPIN & "' AND [Contract] = '" & strContract & "' AND [Network] = '" & strNetwork & "'")
If Not rst.NoMatch Then
rst.Edit
rst!Allocated_Hrs = rstTemp!F38
rst.Update
End If
End If
rstTemp.MoveNext
Loop
End If
Set rst = Nothing
Set rstTemp = Nothing
End Sub
i really need help thank you
Is this what you are doing?
UPDATE qry_HR_temp_Consultant_Data_DuplicateNetwork_EachValue
INNER JOIN tbl_HR_Contract_Detail ON
qry_HR_temp_Consultant_Data_DuplicateNetwork_EachValue.SAP_PIN = tbl_HR_Contract_Detail.SAP_PIN
AND qry_HR_temp_Consultant_Data_DuplicateNetwork_EachValue.Contract = tbl_HR_Contract_Detail.Contract
AND qry_HR_temp_Consultant_Data_DuplicateNetwork_EachValue.Network = tbl_HR_Contract_Detail.Network
SET tbl_HR_Contract_Detail.Allocated_Hrs = qry_HR_temp_Consultant_Data_DuplicateNetwork_EachValue.F38;
You set Allocated_HrstoF38 where SAP_PIN,Contract and Network are equal?
SAP_PIN,Contract and Network are combined unique in qry_HR_temp_Consultant_Data_DuplicateNetwork_EachValue?

Script fails with error code 80040E31

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

Break statement for If contains and/or when reading from a lookup table

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\

Resources