Solved

ActiveX script in DTS package

Posted on 2007-11-14
10
3,422 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
10 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 20282224
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
ID: 20282258
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
ID: 20282426
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
Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

 

Author Comment

by:krbnldy
ID: 20282432
I meant make it work. :-)
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 20282573
>>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
ID: 20283944
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
ID: 20285783
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
ID: 20285790
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
ID: 20633750
PAQed with points refunded (100)

Computer101
EE Admin
0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

729 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