VBScript-Unable to get connected to database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BezerkRogue
    New Member
    • Oct 2007
    • 68

    VBScript-Unable to get connected to database

    I have created a script that automates the launching of an application based upon a value in the registry. For some reason, it is not connecting to the database to read what application it's supposed to launch. I have combed over this script and can't figure out what the issue is. Please help.

    Code:
    ' VBScript File for synchronizing error proofing applications and launching the correct EP application on station start up.
    
    'Error handling
    On error resume next
    
    'Create variables
     
    Dim objShell 'WScript Shell object
    Dim strReg1  'StationName
    Dim objConn  'Database Connection
    Dim strSQL   'SQL Command String
    Dim objRec   'Recordset
    Dim strConn  'Connection String
    Dim strEPApp 'Error Proofing Application String
    Dim appExec  'SyncToy executable variable
    Dim appExec2 'EP executable variable
    
    'Decalare and open connection string and recordset and set objects
    Set objConn = CreateObject("ADODB.Connection")
    strConn="Provider=SQLOLEDB; Data Source=XXXXXX;Trusted_Connection=Yes;Database=XXX;UID=XXX; PWD=XXX;"
    
    Set objRec = CreateObject("ADODB.Recordset")
    Set objShell = WScript.CreateObject("Wscript.Shell")
    
    'Declare File and Reg variables
    
    strReg1 = "HKEY_CURRENT_USER\Software\VB and VBA Program Settings\Error Proofing Station\Properties\StationName"
    
    'Declare variable for database records by reading registry values into recordset.
    Dim rec1
    
    rec1 = objShell.RegRead(strReg1)
    
    If rec1 = "" then
    	strReg1 = "HKEY_CURRENT_USER\Software\VB and VBA Program Settings\Prepick Error Proofing Station\Properties\StationName"
    	rec1 = objShell.RegRead(strReg1)
    End If
    wscript.echo rec1
    
    'Declare SQL Statements
    strSQL = "Select * from EPApps where StaName ='" & rec1 &"'"
    wscript.echo strSQL
    objRec.Open strSQL, strConn
    strEPApp = objRec.fields(2).value
    
    'Launch Synctoy App
    Dim WShell
    Set WShell = WScript.CreateObject("WScript.Shell")
    Set appExec = WShell.Exec("C:\SyncToy\SyncToy.exe -R")
    Wscript.Sleep 5000
    Set appExec2 = WShell.Exec("C:\Program Files\OnlineErrorProofing\" & strEPApp )
    At this point any help would be appreciated.

    Thanks
  • jeffstl
    Recognized Expert Contributor
    • Feb 2008
    • 432

    #2
    You have to open the connection to your database before you execute SQL on it.

    Code:
    'Declare SQL Statements
    objConn.Open strConn
    
    strSQL = "Select * from EPApps where StaName ='" & rec1 &"'"
    wscript.echo strSQL
    objRec.Open strSQL, strConn
    strEPApp = objRec.fields(2).value
    
    objConn.Close
    I also notice you don't specify the path within the object so your going ot need to do something like this

    Comment

    Working...