Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

VBscript - Empty SQL Record Set Value (CM12795), Date Format (CM12574)

$
0
0

Good day all,

I have created a VBscript that will be given a variable from a document capturing application, which is then used to lookup metadata in a SQL table.

I have the following two questions:

  1. I get the following error message when record set tries to read the metadata field which has no data in the database: Script - ExecuteScript error. Type mismatch: 'Metadata.SetValues' (Internal Line: 158, [ call Metadata.SetValues("DELIVERYNOTE", RS("OwnDelNote"))])
  2. I need the script to pull in the date format from the RS in the following format: YYYY-MM-DD hh:mm:ss. What is the correct way of coding this for a SQL statement in a VBscript?

Please see my code below:

'This script is used to index metadata from documents by searching for text within the page using OCR results, by performing the following: 
 '1) Perform OCR Scripting to find the Invoice/Credit Note Number 
'2) Lookup the QSA SQL DB to gather the remaining Credit Note metadata 
'3) Gather the Customer Name 
'Ver. 1.2 
 'Changes: 
 'Ver. 1.2 
'1) Change scripting for Creditor name 
 'Ver. 1.1: (This version works) 
'1) OCR Scripting will search for "Invoice Nr: CR" and pull the numerics and add the "CR" 
 '1) Perform OCR Scripting to find the Invoice Number 
 OCRText = Metadata.Values("OCRTEXT") 
OCRText = Replace(OCRText," ","") 
OCRText = Replace(OCRText,"""","") 
 InvoiceNo = GetFirstMatch(".*(InvoiceNr:CR)(([0-9]){4})", OCRText,1)  
Metadata.Values("INVOICENO") = InvoiceNo 
 FaktuurNrWCHar = GetFirstMatch(".*(InvoiceNr.Cr)(([0-9]){4})", OCRText,1) 
Metadata.Values("FAKTUURNRWCHAR") = FaktuurNrWCHar 
 FaktuurNrWSCHar = GetFirstMatch(".*(InvoiceNr.CR)(([0-9]){4})", OCRText,1) 
Metadata.Values("FAKTUURNRWSCHAR") = FaktuurNrWSCHar 
'RegExp that have been used: 
 '[^A-Za-z0-9_] = no results 
' Get the first objRE  submatch from the string 
' Returns empty string if not found, otherwise returns the matched string 
 Function GetFirstMatch(PatternToMatch, StringToSearch, SubmatchNo) 
 Dim objRE , CurrentMatch, objMatch 
  Set objRE  = New RegExp 
 objRE.Pattern = PatternToMatch 
 objRE.IgnoreCase = True 
 objRE.Global = False 
  Set objMatch = objRE.Execute(StringToSearch) 
 GetFirstMatch = "" 
 ' We should get only 1 match since the Global property is FALSE 
 If objMatch.Count = 1 Then 
  ' Item(0) is the (first and only) matching invoice no part, 
  ' Submatches(1) is the substring between the second set of 
  ' parentheses (all indexes are zero based) 
  GetFirstMatch = objMatch.Item(0).Submatches(SubmatchNo) 
 Else 
  GetFirstMatch = "NOMATCH" 
 End If 
 Set objRE  = Nothing 
End Function 
'Checks if results are numeric, therefore it has found a match as an Invoice Number. 
 if (IsNumeric(Metadata.Values("FAKTUURNRWCHAR"))) = true then 
Metadata.Values("INVOICENUMBER") = Metadata.Values("FAKTUURNRWCHAR") 
 elseif (IsNumeric(Metadata.Values("INVOICENO"))) = true then 
Metadata.Values("INVOICENUMBER") = Metadata.Values("INVOICENO") 
 elseif (IsNumeric(Metadata.Values("FAKTUURNRWSCHAR"))) = true then 
Metadata.Values("INVOICENUMBER") = Metadata.Values("FAKTUURNRWSCHAR") 
end if 
 'Test Data 
'INVOICENUMBER = "CR0129" 
 'Data from Script 
INVOICENUMBER = Metadata.Values("INVOICENUMBER") 
 'Add "CR" to the Invoice Number numerics and set the metadata value. 
 INVOICENUMBER = "CR" + INVOICENUMBER 
Metadata.Values("INVOICENUMBER") = INVOICENUMBER 
 '2) Lookup the QSA SQL DB to gather the remaining Credit Note metadata 
 'Use the Invoice Number Metadata to pull the remaining metadata from the DB 
 'declare the variables 
 Dim Connection 
Dim Recordset 
Dim SQL 
 TimeStamp = Metadata.Values("YY") + Metadata.Values("MM") + Metadata.Values("DD") + "_" + Metadata.Values("HH24") + Metadata.Values("NN") + Metadata.Values("SS") + Metadata.Values("ZZZ") 
 'declare the SQL statement that will query the database 
 'Correct Query from MySql 
'SQL = "select date_format (invdate,'%d/%m/%y'), taxinvnumber, drname from hfrsched.tblloadschedule where invnumber = '" + INVOICENUMBER + "'" 
 'Original Query for MySQL 
'SQL = "Select * From hfrsched.tblloadschedule WHERE InvNumber = '" + INVOICENUMBER + "'" 
 'Query for QSA SQL 
'SQL = "Select DebtorNr, Trxdate, CustOrdNr, OwnDelNote, LoadORder, DocNr From FinanceSalesJournal WHERE DocNr = '" + INVOICENUMBER + "'" 
 'Query for QSA SQL 
SQL = "Select * From FinanceSalesJournal WHERE DocNr = '" + INVOICENUMBER + "'" 
 'create an instance of the ADO connection and recordset objects 
Set Connection = CreateObject("ADODB.Connection") 
'open the connection to the database 
 'Connection string used in MySQL 
'Connection.open = "..... 
 'Connection string used for QSA SQL 
Connection.open = ".... 
 If INVOICENUMBER = "" Then 
 Metadata.Values("OUTPUTFOLDER") = "C:\Scans\M-Files Integrate\HFR\Credit Note Rejects" 
 Metadata.Values("FILENAME") = "Rejected_Scan_" + TimeStamp 
 Metadata.Values("INVOICENUMBER") = "Failed at check if Invoice Numb is empty" 
else 
 'Open the recordset object executing the SQL statement and return records 
 Set RS = Connection.Execute(SQL) 
  IF NOT RS.EOF THEN 
  Metadata.Values("CUSTOMERNUMBER") = RS("DebtorNr") 
  'Metadata.Values("INVOICEDATE") = RS("date_format (invdate,'%d/%m/%y')")) 
  Metadata.Values("INVOICEDATE") = RS("Trxdate") 
  Metadata.Values("ACCOUNTNUMBER") = RS("DebtorNr") 
  Metadata.Values("ORDERNUMBER") = RS("CustOrdNr") 
  Metadata.Values("DELIVERYNOTE") = RS("OwnDelNote") 
  Metadata.Values("LOADNUMBER") = RS("LoadORder")   
  Metadata.Values("OUTPUTFOLDER") = "C:\Scans\M-Files Integrate\HFR\Credit Note\" 
  Metadata.Values("FILENAME") = INVOICENUMBER 
 ELSE 
  'wscript.echo ("Nothing") 
  Metadata.Values("CUSTOMERNAME") = "NOT_FOUND" 
  Metadata.Values("INVOICEDATE") = "NOT_FOUND" 
  Metadata.Values("OUTPUTFOLDER") = "C:\Scans\M-Files Integrate\HFR\Credit Note Rejects" 
  Metadata.Values("FILENAME") = "Rejected_Scan_" + TimeStamp 
 END IF 
RS.Close 
END IF 
 'Test Data 
'CUSTOMERNUMBER = "DAN002" 
 CUSTOMERNUMBER = Metadata.Values("CUSTOMERNUMBER") 
 '3) Gather the Creditor Name 
 'Query for QSA SQL Creditor Name in Creditor Table, which is quite empty 
'SQLCustName = "Select CredNr, CredName From FinanceCreditorMaster where CredNr = '" + CUSTOMERNUMBER + "'" 
 'Query for QSA SQL Creditor Name in Debtor Table 
SQLCustName = "Select DebName, DebNr From FinanceDebtorMaster where DebNr = '" + CUSTOMERNUMBER + "'" 
 'Open the recordset object executing the SQL statement and return records 
 Set RS = Connection.Execute(SQLCustName) 
  IF NOT RS.EOF THEN 
  Metadata.Values("CUSTOMERNAME") = RS("DebName") 
 ELSE 
  'wscript.echo ("Nothing") 
  Metadata.Values("CUSTOMERNAME") = "NOT_FOUND" 
 END IF 
RS.Close 
Set RS=nothing 
Connection.Close 
Set Connection=nothing

Thank you,

Regards,


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>