Find and insert e-mail from other tables

Hello,

I have been using this code for a few days now and it seems to work well except under one situation.

If I create a new product record and do not enter a Product Manager then I get the error shown in the image attached when I update the subform (and attempt to automatically send the e-mail saying "No PM")

The line the debugger stops at and highlights is: If Nz(rst![work e-mail], "") = "" Then

This only happens when a new product record is created and I try to update the subform for it right away. If the product record ever had a name for Product Manager, meaning there was one but at some point it was removed, then there is no error and I do get the correct "No PM" message.

The complete code is:
Option Compare Database

Private Sub Form_AfterUpdate()
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String
Dim strEmailAddress As String
Dim mail
Dim TestInput As Variant
Dim PMName As Variant
Dim ProjectName As Variant
Dim email As String

'find the comment that is getting updated to send it
Me.Comment.SetFocus
TestInput = Me.Comment.Text

'Find the PM whose project this is
[Forms]![Project Main]![Product Manager].SetFocus
PMName = [Forms]![Project Main]![Product Manager].Text
'MsgBox PMName ' This would show a box with the PM Name

'Find the Project Name this e-mail refers to
[Forms]![Project Main]![Project Name].SetFocus
ProjectName = [Forms]![Project Main]![Project Name].Text

'Find the e-mail address of that PM
strSQL = "SELECT [Work e-mail] FROM [Product Managers] WHERE Name='" & [Forms]![Project Main]![Product Manager] & "';"
' MsgBox strSQL ' This would show a box with the code to find the PM

' Open pointer to current database
Set dbs = CurrentDb()

' Create recordset based on SQL
Set rst = dbs.OpenRecordset(strSQL)

If Nz(rst![work e-mail], "") = "" Then
strEmailAddress = "miki@gmail.com"
TestInput = "No PM"
Else
strEmailAddress = rst![work e-mail]
End If

' If Not rst.EOF And Not rst.BOF Then
' 'Code Assumes only 1 match will be found.
' '.MoveFirst
' strEmailAddress = rst![work e-mail]
' Else
' strEmailAddress = "mfrontera@hobbico.com"
' End If

' MsgBox strEmailAddress 'This whould show the e-mail address the e-mail is being sent to

rst.Close
dbs.Close
Set rst = Nothing
Set dbs = Nothing

Set mail = Nothing

' Send by connecting to port 25 of the SMTP server.
Dim iMsg
Dim iConf
Dim Flds
Dim strHTML
Dim strEmailTo

Const cdoSendUsingPort = 2

Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")

Set Flds = iConf.Fields

' Set the CDOSYS configuration fields to use port 25 on the SMTP server.

With Flds
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
'ToDo: Enter name or IP address of remote SMTP server.
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "10.1.10.63"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 10
.Update
End With

' Build HTML for message body.
strHTML = "<HTML>"
strHTML = strHTML & "<HEAD>"
strHTML = strHTML & "<BODY>"
strHTML = strHTML & "<b> </b></br>"
strHTML = strHTML & "</BODY>"
strHTML = strHTML & "</HTML>"

' Apply the settings to the message.
With iMsg
Set .Configuration = iConf
.To = strEmailAddress
.cc = "mfrontera@hobbico.com"
.From = "Airplane R&D Database" 'MailFrom 'this should be OK
.Subject = ProjectName
.HTMLBody = TestInput 'this should be OK
.Send 'this should be OK
End With

' Clean up variables.
Set iMsg = Nothing
Set iConf = Nothing
Set Flds = Nothing

'MsgBox "Mail Sent!"

End Sub

What is happening? Should I set a default value of "" for that field? Something else?

mafhobb
 

Attachments

  • error.JPG
    error.JPG
    9.7 KB · Views: 77
Sounds like the recordset isn't returning any values, so you need to get around it some way, such as:

Code:
If rst.EOF Then
  Msgbox "Blah"
  Exit Sub
Else
  'your code here 
End If

I would suggest adding an error trap, putting your cleanup code in the exit portion of it, and going there instead of exiting the sub above.
 
I have added an error trap that takes care of the problem.

I'd like to know what the code you suggested does, though;

If rst.EOF Then 'I am not sure what this line means
Msgbox "Blah"
Exit Sub
Else
'your code here 'is this were the error handling should be?
End If

Thank you again. Your help is invaluable to those of us with limited Access skills!

mafhobb
 
Sorry, since you had it elsewhere (though commented out) I assumed you knew what it meant. If the SQL the recordset is based on does not return any records, EOF will be true as soon as the recordset is opened. Testing for it allows you to gracefully handle the lack of records.
 
I now understand. Thanks for your help again.
mafhobb
 
hi! thanks so much! i was having the same problem that's why i searched through the internet. good thing i saw this forum.
 

Users who are viewing this thread

Back
Top Bottom