dbTalk Databases Forums  

SQL Statement in Active X not working

microsoft.public.sqlserver.dts microsoft.public.sqlserver.dts


Discuss SQL Statement in Active X not working in the microsoft.public.sqlserver.dts forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
javathehutt
 
Posts: n/a

Default SQL Statement in Active X not working - 07-12-2006 , 08:54 PM






I put a SQL Statement in my ActiveX and I can't get the correct
Textbody to be sent. The Sql Statement returns a 1 in query analyzer.
However, when the email is sent from this DTS Task, it returns

"No Errors Found" and there are no file attachements.

Any pointers on where I am going wrong here? Thanks


************************************************** ********************
' Visual Basic ActiveX Script
'************************************************* ***********************

Function Main()

Dim sSQLStatement

' Build new SQL Statement
sSQLStatement = "if exists (select * from table1 (with nolock) where
id = 1011) select 1 else select 0"


Dim yesterday 'As Date
Dim yesterdaystr 'As String
yesterday = DateAdd ("d" , -1 , Now() )
yesterdaystr = Year(yesterday) & "/" & Month(yesterday) & "/"
& Day(yesterday)

sch = "http://schemas.microsoft.com/cdo/configuration/"

Set cdoConfig = CreateObject("CDO.Configuration")

With cdoConfig.Fields
.Item(sch & "sendusing") = 2 ' cdoSendUsingPort
.Item(sch & "smtpserver") = "qarelay1"
.update
End With

Set cdoMessage = CreateObject("CDO.Message")

With cdoMessage
Set .Configuration = cdoConfig
.From = "mongo (AT) mongo (DOT) com"
.To = "mongo (AT) mongo (DOT) com"
.Subject = "daily reports."

'Both files must be in error to send the following text message


If sSQLStatement ="1" then



.TextBody ="Errors Found"
Else



.TextBody = "No Errors Found"

End If

'Attach file only if error found


If sSQLStatement ="1" then


.AddAttachment ("\\archive\Daily\Report.xls")

End If

End With

Set cdoMessage = Nothing
Set cdoConfig = Nothing


Main = DTSTaskExecResult_Success
End Function


Reply With Quote
  #2  
Old   
Frans van Bree
 
Posts: n/a

Default RE: SQL Statement in Active X not working - 07-13-2006 , 07:04 AM






You forgot to run the statement in sSQLStatement against the database. It is
just a string value now!

And, of course, the value "if exists (select * from table1 (with nolock)
where id = 1011) select 1 else select 0" is not 1 or 0

Reply With Quote
  #3  
Old   
javathehutt
 
Posts: n/a

Default Re: SQL Statement in Active X not working - 07-13-2006 , 04:31 PM



Thanks

Is there a resource where I can read up on how to call a sql statement
in VB Script?


Frans van Bree wrote:
Quote:
You forgot to run the statement in sSQLStatement against the database. It is
just a string value now!

And, of course, the value "if exists (select * from table1 (with nolock)
where id = 1011) select 1 else select 0" is not 1 or 0


Reply With Quote
  #4  
Old   
Frans van Bree
 
Posts: n/a

Default Re: SQL Statement in Active X not working - 07-14-2006 , 03:13 AM



I always use an UDL file for my connections. You can create one by creating
an empty text file called myDB.udl and double-clicking it. Rest should be
straightforward. Now some code (adapt it to your own needs) I am doing this
from my head, e.g. fields(0) might be fields(1).

But give it a try, I think it will help you further:

Dim oConn
Dim sSQLStatement

Set oConn = CreateObject("ADODB.Connection")
oConn.Open "File Name=c:\myDB.udl"
sSQLStatement = "if exists (select * from table1 (with nolock) where
id = 1011) select 1 else select 0"
Set oRs = CreateObject("ADODB.Recordset")
oRs.Open sSQL, oConn

If oRs.Fields(0).Value = 1 then
..TextBody ="Errors Found"
Else ...

etc



Reply With Quote
  #5  
Old   
Frans van Bree
 
Posts: n/a

Default Re: SQL Statement in Active X not working - 07-14-2006 , 03:20 AM



oRs.Open sSQL, oConn

should be

oRs.Open sSQLStatement, oConn

and if you don't want to use an udl, look here:
www.connectionstrings.com

Put the appropriate string where it says "c:\myDB.udl"

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.