Hi,
I have 3 tables, ClientList, AccountList, LogHistory. The ClientList has the client's resident address. The AccountList has the client's mailing address, if it is different, if not, that field is left blank. The LogHistory will record what was sent to the client and the address it was sent to. This is where I want to add the new record to.
I created a query that contains the client's name, address, mailing address and any other relevant information (for this purpose it is not necessary to list all of them). I also have an unbound form that contains a "Print" button and a "Cancel" button. If the query has results on any given day, the unbound form opens based on this code.
Code:
If DCount("*", "NameOfQuery") > 0 Then
DoCmd.OpenForm "NameOfPopUpForm"
End If
If the user clicks "Print" then I want the client's corresponding reports to print (which I am having no problem doing) and I want the LogHistory to have a new record added showing the client's information and the appropriate address (resident or mailing) in the appropriate fields in the LogHistory table. The below code is what I have that adds a new record based on the current forms information.
Code:
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb()
Set rs = db.OpenRecordset("log", dbOpenDynaset, dbAppendOnly)
rs.AddNew
rs!accountid = Me.Id
rs!addysent = IIf(IsNull(Me.mailaddy), Me.addy, Me.mailaddy)
rs.Update
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
I also have this code that I use to send emails to the clients and update fields in the query.
Code:
Dim rst As Object
Dim myOlApp As Object
Dim myNameSpace As Object
Dim myFolder As Object
Dim myItem As Object
Dim myAttachments, myRecipient As Object
Dim recipient As String
Dim file_name As String
Dim mySubject As Object
Dim dbs As Object
strSql = "PRIMARYNAMEq" 'Select the Query where you want your information to be drawn from
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(strSql)
rst.MoveFirst
While Not rst.EOF
recipient = "D" 'This is the email address that you corresponds to your recipient
Set myOlApp = CreateObject("Outlook.Application")
Set myItem = myOlApp.CreateItem(olMailItem)
Set myAttachments = myItem.Attachments.Add(Attachment)
Set myRecipient = myItem.Recipients.Add(recipient)
'"Message Subject String Here"
myItem.Subject = "XXXXXX"
'"Put Message Body Text Here"
myItem.Body = "Hi"
myItem.Display
rst.Edit
rst!Sent = True
rst.Update
rst.MoveNext
Wend
'DoCmd.Close acForm, "XXXXX" 'Closes the form
'DoCmd.OpenForm "EmailConfirmation" 'Opens Email Confirmation Form
Set myRecipient = Nothing
Set myAttachments = Nothing
Set myItem = Nothing
Set myOlApp = Nothing
Set rst = Nothing
This code moves through the query until EOF. I thought I would be able to integrate parts of the two codes to get it to work but I can't figure it out since the email code does not add any new records to a third table.
I have looked around and tried a few things, but can't seem to get it to work how I want it to. Any suggestions?