jlcannon
asked on
Email from MS Access
I have an Access database that stores names of documents that I use as newsletters. I keep them all as I want an archive that folks can go to and pick one that they want to see. I also once a month send one in the list out to a table of subscribers. The issue is even though I have tried to mimic a random choice it seem to already duplicated my choices after only 3 months while there are 28 files to choose from. My current code is as follows:
My initial thought was to create another table called "UsedNews" and append the table with the one that is used each time the code is run but I am unclear how exactly to add that code here of if that is possible so that next time I run it, it checks that table fist and if the newsletter has been used don't use it this time.
Sub SendEmail()
Dim appOutlook As New Outlook.Application
Dim qdf1 As DAO.QueryDef
Dim rs1 As DAO.Recordset
Dim qdf2 As DAO.QueryDef
Dim rs2 As DAO.Recordset
Dim strSql As String
Dim strEmailDistro As String
Dim strSubject As String
Dim strHeader As String
Dim strContent As String
Dim strFooter As String
Dim strBody As String
Dim blnSuccessful As Boolean
Dim strHTML As String
Dim objEmail As Outlook.MailItem
'simple query to grab the emails from the table. modify this as you need to. since you are linking to SP list, just point your query there.
strSqlEmail = "SELECT tblSubscribers.Email FROM tblSubscribers ;"
'run the query and set it as the recordset to cycle through
Set qdf1 = CurrentDb.CreateQueryDef("", strSqlEmail)
Set rs1 = qdf1.OpenRecordset
'create outlook session
Set objEmail = appOutlook.CreateItem(olMailItem)
'start building the email distro list
A = 1
Do Until rs1.EOF
strEmail = rs1![Email] & "; "
strEmailDistro = strEmail + strEmailDistro
A = A + 1
rs1.MoveNext
Debug.Print strEmailDistro
Loop
'put whatever error trapping code you have here, this just looks to make sure that the email list is not null
If IsEmpty(strEmailDistro) Then
Set rs1 = Nothing
Set qdf1 = Nothing
Exit Sub
End If
strSqlFact = "SELECT DatabaseName from tblFiles ORDER BY rnd(ID);"
'run the query and set it as the recordset to cycle through
Set qdf2 = CurrentDb.CreateQueryDef("", strSqlFact)
Set rs2 = qdf2.OpenRecordset
strFact = rs2![DatabaseName] & "; "
strLink = strFact
'put whatever error trapping code you have here, this just looks to make sure that the email list is not null
If IsEmpty(strLink) Then
Set rs2 = Nothing
Set qdf2 = Nothing
Exit Sub
End If
'set what you want the subject of the email to be
strSubject = "Newsletter of the Month!"
strEmailBody = "Greeting from your Newsletter team. Below you will find this months single slide safety fact."
strEmailBody = strEmailBody & vbLf & "\\frz38\News\Data\Mnthly\Newsletter\Publish\" & strLink
Debug.Print strHTML
'send the email out!
With objEmail
.To = strEmailDistro
.Subject = strSubject
.Body = strEmailBody
.Send
End With
Set rs1 = Nothing
Set qdf1 = Nothing
If appOutlook Is Nothing Then Else Set appOutlook = Nothing
End Sub
My initial thought was to create another table called "UsedNews" and append the table with the one that is used each time the code is run but I am unclear how exactly to add that code here of if that is possible so that next time I run it, it checks that table fist and if the newsletter has been used don't use it this time.
ASKER
the only issue I have with that one is right now there is a list of these files that are updated as new ones are added. Once a month I run a routine in access that imports that list from excel in recreated the tblFiles table in access so it completely overwrites the last iteration of the table. This is why I thought as I used a file I would add it to a a second table called tblUsed and then make my statement something like
but I am not sure how to append the name to the tblFileUsed as it is used. I tried to use:
using the variable I built in code above it
SELECT DatabaseName
FROM tblFiles
LEFT JOIN tblUsed ON DatabaseName = tblFIles.DatabaseName
WHERE tblUsed.DatabaseName IS NULL
but I am not sure how to append the name to the tblFileUsed as it is used. I tried to use:
dbs.Execute "INSERT INTO tblFilesUsed DatabaseName [strFact];"
using the variable I built in code above it
strFact = rs2![DatabaseName] & "; "
strLink = strFact
Try:
»bp
dbs.Execute "INSERT INTO tblFilesUsed (DatabaseName) VALUES ('" & strFact & "');"
»bp
Hold on, I think I misunderstood the columns in the new table, what are they?
»bp
»bp
ASKER
the columns names are the same in both tables:
File Name, WebName, DatabaseName
File Name, WebName, DatabaseName
So it seems to me you have two choices. You either populate both tables with ALL the database names, and then use one of the other columns in the new table to indicate when a database is used. If you go that route though I would say change the columns of the new table to be the Database and then a LastUsed column.
The other approach is to only use the new table to hold a record for a recently used Database name. So it starts empty, and then as you select a random name, you add it to the table so you can not re-use it. If it were me I'd probably still use a LastUsed column with the date it is used. That way you don't have to worry about purging it, you just skip the ones more recent than a certain threshold.
How do those other two columns factor in (FileName, WebName)?
»bp
The other approach is to only use the new table to hold a record for a recently used Database name. So it starts empty, and then as you select a random name, you add it to the table so you can not re-use it. If it were me I'd probably still use a LastUsed column with the date it is used. That way you don't have to worry about purging it, you just skip the ones more recent than a certain threshold.
How do those other two columns factor in (FileName, WebName)?
»bp
And why do you reload the whole original table all the time? Are the same set of database names always there, or do they change every time? Trying to understand this better so I can best advise.
»bp
»bp
ASKER
so basically I created a batch file that gets a directory listing of the folder where the newsletters are stored I then store it as a .csv file. then I have an excel sheet with a sub routine that copies the whole list of file names from the .CSV file in to the File Name column and then the other 2 colums are just the same File Name but with special formatting for the need of use specified by the column name.
Okay, I want go down that path...
I guess my example INSERT statement above should give you and idea on adding to that table.
»bp
I guess my example INSERT statement above should give you and idea on adding to that table.
»bp
ASKER
well the insert statement above stops at error 424 Object Required
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Excellent, yes this did work. I was not familiar with the dbs.execute but I read that was the proper way to do it.
Great.
»bp
»bp
Then the logic should be fairly straight forward, when you use an item for the random send, update it's datetime field with current date. And then when selecting a random in the future, add that to the WHERE clause to exclude recently used items.
»bp