That is some nice, common sense error handling. I realize that it might help if I showed the larger code sequence I have. The original was just the subroutine for getting the email addresses. I separated that out to troubleshoot it and now that it's working I simply added it to a larger subroutine.
First here is the code that gets the CA value:
Code:
Private Sub Form_Open(Cancel As Integer)
strCA = InputBox("Enter the CA for the paricipants you wish to email" & vbCr & vbCr & _
Chr(9) & "VP, LM, KR, SH, or JS" & vbCr & vbCr & "Or press * to email all participants", "Email All/Filter")
End Sub
Since the strCA is declared as Public for the entire form, I figured I should be okay using a Private subroutine to get the data. Alright, now the big ugly stuff for the "Send Email" button.
Code:
Private Sub btnGandH_Click()
Dim EmailType As String, strSQL As String, strWHERE As String, strSEASON As String
Dim db As DAO.Database, rst As DAO.Recordset
Dim ThisYear As Integer
Dim SpringStart As Date, SummerStart As Date, FallStart As Date, FallEnd As Date
On Error GoTo ErrHandler
' Ensuring all the necessary selections are made before this code runs
If Me.cboAwType = "" Then
MsgBox "You must select and Award Type.", vbInformation, "Award Type Needed"
Exit Sub
End If
'Selecting the text of the email to be sent based on award type
If Me.cboAwType.Value = "RC" Then
EmailType = GHRCText
ElseIf Me.cboAwType.Value = "RT" Then
EmailType = GHRTText
End If
'Ensuring the user has selected Season
If Me.cboSeason.Value = "" Then
MsgBox "Please Select a graduating Season from the menu on the left.", vbInformation, "Season Needed"
Exit Sub
End If
ThisYear = Year(Date)
'Setting dates for the "season" variable
SpringStart = DateSerial(ThisYear, 1, 1)
SummerStart = DateAdd("m", 6, SpringStart)
FallStart = DateAdd("m", 2, SummerStart)
FallEnd = DateAdd("yyyy", 1, SpringStart)
' selecting the SEASON SQL statement based on the Season variable
If Forms!frmBulkEmails.Season = "Spring" Then
strSEASON = "AND PartInfo.[Degree Grad Date] < '" & SummerStart & "';"
ElseIf Forms!frmBulkEmails.Season = "Summer" Then
strSEASON = "AND PartInfo.[Degree Grad Date] >= '" & SummerStart & "' AND PartInfo.[Degree Grad Date] < '" & FallStart & "';"
ElseIf Forms!frmBulkEmails.Season = "Fall" Then
strSEASON = "AND PartInfo.[Degree Grad Date] > '" & FallStart & "';"
End If
Debug.Print strSEASON
If Me.cboSeason.Value = "" Then
MsgBox "Please Select a graduating Season from the menu on the left.", vbInformation, "Season Needed"
Exit Sub
End If
' Selecting the emails by CA and Awardee Type. Note that by using the INNER JOIN properties this SQL statement restricts
' the email collection to those participants who are on the Grad and Hire tracker as well as CA and Awardee Type.
' Retrieving the CA value from the Open Form event
Debug.Print Forms!frmBulkEmails.strCA
strSQL = "SELECT [Email Address] FROM PartInfo INNER JOIN GandHTracker ON PartInfo.[SMART Id] = GandHTracker.[SMART ID]"
strWHERE = " WHERE PartInfo.CA Like '" & strCA & "' AND PartInfo.[Awardee Type] Like '" & Me.cboAwType & "' "
Set db = CurrentDb
Set rst = db.OpenRecordset(strSQL & strWHERE & strSEASON)
Debug.Print strSQL & strWHERE & strSEASON
Do While Not rst.EOF
EmailList = EmailList & "; " & rst![Email Address]
rst.MoveNext
Loop
Debug.Print EmailList
EmailList = Right(EmailList, Len(EmailList) - 1)
Debug.Print EmailList
Debug.Print rst.RecordCount
Set rst = Nothing
Set db = Nothing
' Sending the actual email with the email addresses in the BCC section
DoCmd.SendObject acSendNoObject, , , , , "'" & EmailList & "'", "Graduation and Hiring Information", EmailType
Exit Sub
ErrHandler:
If Err.Number = 2501 Then
MsgBox "This email was not sent.", , "No Emails for You!"
Else
MsgBox Err.Number & ": " & Err.Description
End If
End Sub
As you can see, I have some basic error handling at the bottom there but I'm still experiencing the loss of the CA data.