Solved

ActiveX script in DTS package

Posted on 2007-11-14
10
3,416 Views
Last Modified: 2013-11-30
I am using activeX script in a DTS package to send mail to serveral users if thier ID is found in a particular MSSQL view.  The code is added below.  it is only sending one email for the first employee it finds in the view.  Please help me modify the code so that it could send an email for each employee it finds and add the id of each COntent_ID it finds for each employee.
For testing I am only including my email address and not yet getting the email address from the view.  I will modify that latr

Sub Send_Mail(strTo, strFrom, strSubject, strMessage )
      Const cdoSendUsingPickup = 1
      Const cdoSendUsingPort = 2

      set iMsg = CreateObject("CDO.Message")
      set iConf = CreateObject("CDO.Configuration")
      Set Flds = iConf.Fields
      With Flds
                .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") =2 'cdoSendUsingPort
'this parameter below is the valid SMPT Server Name
                .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") ="******.***.com"
                .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 10  
                .Update
      End With

      With iMsg
             Set .Configuration = iConf
            .To = strTo  ' comma separated list of recipients.
            .From = strFrom
            .Subject = strSubject
            .HTMLBody = strMessage
            .Send
      End With

End Sub

Function Main()

dim myConn
dim myRecordset
dim iRowCount
dim rsUnread

' instantiate the ADO objects
      set myConn = CreateObject("ADODB.Connection")
      set myRecordset = CreateObject("ADODB.Recordset")
      
      set rsUnread = CreateObject("ADODB.Recordset")

' set the connection properties to point to the CMS  database  using the ProcessDisplay view

      myConn.Open = "Provider=SQLOLEDB.1;Data Source=******;  Initial Catalog=*******;user id = '********';password='*****'"

' check for unread documents for each emloyee
      mySQLUnread = "Select Emp_ID  from vProcessDisplay"
      rsUnread.Open mySQLUnread, myConn
            
      For xcnt = 0 to rsUnread.fields.count -1
            
            mySQLCmdText = "Select  'rowcount' = Count(*), Contetn_ID, Content_title,Emp_Name, Reading_Group, email  from vProcessDisplay where emp_ID = '" & rsUnread("Emp_ID") & "'  group by  Content_ID, COntent_title, emp_Name, Reading_Group, email"
            myRecordset.Open mySQLCmdText, myConn
            
            set Flds = myRecordset.Fields
            set iRowCount = Flds("rowcount")
      next
            while not myRecordset.eof
                  If iRowCount.Value = 0 then
                         Main = DTSTaskExecResult_Failure
                  else
                        dim unreaddoc
                        unreaddoc =  iRowCount.Value
                        strTo="myname<myemailaddrss@*******.com>"
                        strFrom="Document Central<process@****.******.com>"
                        strSubject="Unread document for -" & Date() & myRecordset("emp_Name")  
'  I would like to have a loop here that adds each content_ID to the message as there may be more than one document for each employee                        
strMessage="There are " & unreaddoc & " documents to be read "                              
                        Send_Mail  strTo, strFrom, strSubject, strMessage
                     Main = DTSTaskExecResult_Success
                  End If
                  myRecordset.movenext
            wend
      Main = DTSTaskExecResult_Success
End Function
0
Comment
Question by:krbnldy
  • 5
  • 3
10 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
I have to start by saying that this is not a good approach.  There are far better approaches than using DTS ActiveX Script to send emails.

Having said that let me list all the problems:
You are opening myRecordset rsUnread.fields.count times.  Surely that is not your intent.
You are setting the Task to Failure in the loop, but not exiting.
Contetn_ID is probably mispelled.
If emp_ID is numeric there is no need for the single quotes.
The two queries would be best combined into one.

As to why it is only firing once, I would suspect that the iRowCount.Value = 0 for all rows after the first.  But this can be best verified with debugging turned on.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
Incidentally iRowCount is only set once before the While ... Wend. Your formatted code shows what I mean:


Function Main()

dim myConn
dim myRecordset
dim iRowCount
dim rsUnread

' instantiate the ADO objects
set myConn = CreateObject("ADODB.Connection")
set myRecordset = CreateObject("ADODB.Recordset")

set rsUnread = CreateObject("ADODB.Recordset")

' set the connection properties to point to the CMS  database  using the ProcessDisplay view

myConn.Open = "Provider=SQLOLEDB.1;Data Source=******;  Initial Catalog=*******;user id = '********';password='*****'"

' check for unread documents for each emloyee
mySQLUnread = "Select Emp_ID  from vProcessDisplay"
rsUnread.Open mySQLUnread, myConn

For xcnt = 0 to rsUnread.fields.count -1
      mySQLCmdText = "Select  'rowcount' = Count(*), Contetn_ID, Content_title,Emp_Name, Reading_Group, email  from vProcessDisplay where emp_ID = '" & rsUnread("Emp_ID") & "'  group by  Content_ID, COntent_title, emp_Name, Reading_Group, email"
      myRecordset.Open mySQLCmdText, myConn

      set Flds = myRecordset.Fields
      set iRowCount = Flds("rowcount")
next
while not myRecordset.eof
      If iRowCount.Value = 0 then
            Main = DTSTaskExecResult_Failure
      else
            dim unreaddoc
            unreaddoc =  iRowCount.Value
            strTo="myname<myemailaddrss@*******.com>"
            strFrom="Document Central<process@****.******.com>"
            strSubject="Unread document for -" & Date() & myRecordset("emp_Name")  
            '  I would like to have a loop here that adds each content_ID to the message as there may be more than one document for each employee                        
            strMessage="There are " & unreaddoc & " documents to be read "                              
            Send_Mail  strTo, strFrom, strSubject, strMessage
            Main = DTSTaskExecResult_Success
      End If
      myRecordset.movenext
wend

Main = DTSTaskExecResult_Success
End Function
0
 

Author Comment

by:krbnldy
Comment Utility
I am not too familiar with this but how do I modify the code to make it worse

ALso,  what other approach do  you recommend,  can you give me examples.  I am using a dts package as I want to schedule it to run every tuesday at a specific time.

All suggestions and help aprreciated
0
 

Author Comment

by:krbnldy
Comment Utility
I meant make it work. :-)
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
>>I am not too familiar with this but how do I modify the code to make it worse<<
Without having access to your data, I have no idea what is going wrong.

>>ALso,  what other approach do  you recommend,  can you give me examples.<<
If you are bound and determined to use DTS than I would:
1. Create a one query instead of two.
2. I would use the Execute SQL task to return the result in a Rowset. See here for an example:
How to loop through a global variable Rowset
http://www.sqldts.com/298.aspx
3. To actually send the email I would use something like:
XPSMTP.DLL - SQL Server SMTP Mail XP
http://www.sqldev.net/xp/xpsmtp.htm
0
 

Author Comment

by:krbnldy
Comment Utility
the following is my solution and it works great.  I did not go with using just one query as I had more than one view I needed to pull data from

dim myConn
dim myRecordset
dim iRowCount
dim rsUnread
dim btnID
btnID = "1"
dim ID

' instantiate the ADO objects
      set myConn = CreateObject("ADODB.Connection")
      set myRecordset = CreateObject("ADODB.Recordset")
      
      set rsUnread = CreateObject("ADODB.Recordset")

' set the connection properties to point to the CMS  database  using the vProcessDisplay view

      myConn.Open = "Provider=SQLOLEDB.1;Data Source=***;  Initial Catalog=***_***;user id = ***;password='*****"

' check for unread documents for each emloyee
      mySQLUnread = "Select distinct Emp_ID  from vProcessDisplay"
      rsUnread.Open mySQLUnread, myConn
      
while not rsUnread.eof      
      For xcnt = 0 to rsUnread.fields.count -1
            
            mySQLCmdText = "Select  Emp_Name, Reading_Group, email  from vAdherenceProcessDisplay where emp_ID = '" & rsUnread("Emp_ID") & "'"
                  
            set myrecordset = myConn.Execute(mySQLCmdText)
            
            set Flds = myRecordset.Fields
            
      next
            while not myRecordset.eof

                        dim ggroup
                         ggroup = myRecordset("reading_group")                        
                        
                        strTo="*****<******@***.******.com>"
                        strFrom="Process Central<*********@--******.com>"
                        strSubject="Unread document for -" & Date() & myRecordset("emp_Name")  
                  strMessage= myRecordset("Emp_Name") & "<br /><br />"      
                        strMessage= strMessage & "You have  {count} documents that you have not yet read .<br /><br />"      
                        strMessage = strMessage & " Please click on the url below to access the following .  Please read carefully and afterwards acknowledge that youve read this document.<br /><br />"
                                         
                        sqlSend = "Select * from vDocsnotRead where emp_id = '" & rsUnread("Emp_ID") & "'"

                        set rsSend = MyConn.Execute(sqlSend)

                        dim totalDocs            
                        totalDocs = 0
                  while not rsSend.eof
                        ID = rsSend("Content_ID")
                        strMessage = strMessage &  "<a href='http://**********.com/*****/****.aspx?ID=" & ID & "&btnID=" & btnID & "&gGroup=" & gGroup  & " '>" & rsSend("Content_title") & "</a><br /><br />"
                        
                        totalDocs = totalDocs + 1
                        rsSend.movenext
                        wend
                        strMessage = strMessage &  "Thank You"      
                                     
                  
                  myRecordset.movenext
      
                  wend
            Send_Mail  strTo, strFrom, strSubject,  replace(strMessage, "{count}", totalDocs)
      rsUnread.movenext
      wend
      Main = DTSTaskExecResult_Success
End Function
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
I still do not know why you are opening the recordset myrecordset so many times.  But whatever works for you...
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
P.S.  You should also get in the habit of closing the recordset and setting it to Nothing when you no longer need it,
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
Comment Utility
PAQed with points refunded (100)

Computer101
EE Admin
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
table fragmentation 40 73
Microsoft SQL Queries 11 48
Group by and order by clause 28 35
BULK INSERT most recent CSV 19 17
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now