0

I have a vbscript that calls a second vbscript and runs a function. This second function returns a value. But I can't figure out how to get this value, as the result of the first function returns the status code.

original call: fileCASTRING(12345678)

vbscript 1

function fileCASTRING(varRAW)
lresult = CreateObject("WScript.Shell").Run ("c:\windows\syswow64\cscript.exe C:\ERMXData\Config\query-castring.vbs " & varRAW,0,true)
fileCASTRING=1
end function

query-castring.vbs

doctype=WScript.Arguments.Item(0)
Dim strCon
strCon = "DSN=*****; " & _
         "uid=*****;pwd=*****;"
Dim oCon: Set oCon = WScript.CreateObject("ADODB.Connection")
Dim oRs: Set oRs = WScript.CreateObject("ADODB.Recordset")
oCon.Open strCon
Set oRs = oCon.Execute("select ESBLINK_ADMR_CODE from ESBLINK where ESBLINK_DTYP_CODE like '%" + doctype + "%'"")
queryB=oRs.Fields(0).Value
oCon.Close
Set oRs = Nothing
Set oCon = Nothing

I have to do it like this because the program that runs vbscript 1 runs in 64 bit mode and the code in query-castring.vbs needs to run in 32bit mode in order for it to work. How can I get the queryB value back to the original caller? I am trying to not have to write the value to a file.

2 Answers 2

1

The only easy way to communicate between two command line processes is via StdOut.

(Be aware the code is not tested but should get you into the right direction.)


VBScript 1

Option Explicit

' ...

Function fileCASTRING(varRAW)
    Dim program, script, cmdline, output

    program = "c:\windows\syswow64\cscript.exe /nologo"
    script = "C:\ERMXData\Config\query-castring.vbs"
    cmdLine = program & " " script & " """ & varRAW & """"
    output = ""

    With CreateObject("WScript.Shell").Exec(cmdLine)
        While Not .StdOut.AtEndOfStream
            output = output & .StdOut.ReadAll
        Wend
    End With

    fileCASTRING = output
End Function

see the documentation of the WshScriptExec object


query-castring.vbs

Option Explicit

Dim doctype: doctype = WScript.Arguments.Item(0)
Dim strCon: strCon = "DSN=*****;uid=*****;pwd=*****;"
Dim strSql: "select ESBLINK_ADMR_CODE from ESBLINK where ESBLINK_DTYP_CODE like '%' + ? + '%'"

Dim oCon: Set oCon = WScript.CreateObject("ADODB.Connection")
Dim oCmd: Set oCmd = WScript.CreateObject("ADODB.Command")

oCon.Open strCon

With WScript.CreateObject("ADODB.Command")
    Set .ActiveConnection = oCon
    .CommandText = strSql
    .Parameters.Add(.CreateParameter)
    .Parameters(0).Value = doctype
    With .Execute
        If Not .EOF Then
            WScript.Echo .Fields("ESBLINK_ADMR_CODE").Value
        End If
    End With
End With

oCon.Close

See the documentation of the ADODB Command and Parameter objects. Don't build SQL from strings.

Also, look into "integrated security" connection strings - do not store plain text passwords in in code files. ADODB can easily use the security context of the account that runs the script, if you tell it to.

Sign up to request clarification or add additional context in comments.

Comments

0

VBScript 1

  Function getADMRCODE(varRAW)
        Dim program, script, cmdline, output
        program = "c:\windows\syswow64\cscript.exe /nologo"
        script = "C:\ERMXData\Config\common_app\queries\admrcode.vbs"
        cmdLine = program & " " & script & " """ & varRAW & """"
        output = ""

        With CreateObject("WScript.Shell").Exec(cmdLine)
            While Not .StdOut.AtEndOfStream
                output = output & .StdOut.ReadAll
            Wend
        End With
        getADMRCODE = output
    End Function

query-castring.vbs

Dim doctype: doctype = WScript.Arguments.Item(0)
Dim strCon
strCon = "DSN=*****; " & _
         "uid=*****;pwd=*****;"
Dim oCon: Set oCon = WScript.CreateObject("ADODB.Connection")
Dim oRs: Set oRs = WScript.CreateObject("ADODB.Recordset")
oCon.Open strCon
Set oRs = oCon.Execute("select ESBLINK_ADMR_CODE from ESBLINK where ESBLINK_DTYP_CODE LIKE '%" + doctype + "%'")
 WScript.Echo oRs.Fields(0).Value
oCon.Close
Set oRs = Nothing
Set oCon = Nothing

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.