Results 1 to 4 of 4
  1. #1
    runbear is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Apr 2011
    Posts
    11

    Question " End if without a block If"?

    Why am I getting an error " End if without a block If"? Thanks for any help.

    Private Sub BtnTestEM_Click()


    Dim rs As DAO.Recordset
    Dim fld As Field
    Dim rc As Integer 'record counter'
    Dim em As String ' email address'
    Dim MsgTxt As String 'text of message'
    Dim MsgSbj As String 'Subject of message'
    Dim appOutLook As Outlook.Application
    Dim MailOutLook As Outlook.MailItem






    Set rs = CurrentDb.OpenRecordset("qryMbrEmail")

    If rs.RecordCount <> 0 Then

    With rs
    .MoveFirst
    rc = 1
    MsgTxt = Me.tbMsgTxt
    MsgSbj = Me.tbSubject
    While Not .EOF
    em = rs.Fields("Email")

    If Len(em) > 0 Then
    em = rs.Fields("FirstName") & " " & rs.Fields("LastName") & " " & rs.Fields("Email")
    Set appOutLook = CreateObject("Outlook.Application")
    Set MailOutLook = appOutLook.CreateItem(olMailItem)
    With MailOutLook
    .BodyFormat = olFormatRichText
    .To = em
    .Subject = MsgSbj
    .HTMLBody = MsgTxt
    .Send
    End If 'end of If Len(em) > 0 Then


    rc = rc + 1
    .MoveNext

    Wend ' end While Not .EOF

    MsgBox "Sending e-mail is complete " & rc & " messages sent", vbInformation

    End With 'end With rs

    End If 'If rs.RecordCount <> 0


    rs.Close
    Set rs = Nothing

    End Sub

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    I am going to guess it is because your Wend (Wend ' end While Not .EOF) is out of scope. Maybe get the with above your If Len(em) > 0 Then

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    Post code between CODE tags and indentation will be retained for easier readability. Need an End With below .Send.
    Code:
     
    Private Sub BtnTestEM_Click()Dim rs As DAO.Recordset
    Dim fld As Field
    Dim rc As Integer 'record counter'
    Dim em As String ' email address'
    Dim MsgTxt As String 'text of message'
    Dim MsgSbj As String 'Subject of message'
    Dim appOutLook As Outlook.Application
    Dim MailOutLook As Outlook.MailItem
    Set rs = CurrentDb.OpenRecordset("qryMbrEmail")
    If rs.RecordCount <> 0 Then
       With rs
           .MoveFirst
           rc = 1
           MsgTxt = Me.tbMsgTxt
           MsgSbj = Me.tbSubject
           While Not .EOF
               em = rs.Fields("Email")
               If Len(em) > 0 Then
                   em = rs.Fields("FirstName") & " " & rs.Fields("LastName") & " " & rs.Fields("Email")
                   Set appOutLook = CreateObject("Outlook.Application")
                   Set MailOutLook = appOutLook.CreateItem(olMailItem)
                   With MailOutLook
                      .BodyFormat = olFormatRichText
                      .To = em
                      .Subject = MsgSbj
                      .HTMLBody = MsgTxt
                      .Send
                   End With
               End If 'end of If Len(em) > 0 Then
               rc = rc + 1
               .MoveNext
           Wend ' end While Not .EOF
           MsgBox "Sending e-mail is complete " & rc & " messages sent", vbInformation
        End With 'end With rs
    End If 'If rs.RecordCount <> 0
    rs.Close
    Set rs = Nothing
    End Sub
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    runbear is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Apr 2011
    Posts
    11
    Many thanks!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 07-10-2015, 06:33 AM
  2. Replies: 1
    Last Post: 09-03-2014, 03:27 AM
  3. Complie error "End If without Block If
    By justair07 in forum Access
    Replies: 8
    Last Post: 12-13-2013, 09:03 AM
  4. Export "Query or Report" to a "Delimited Text File"
    By hawzmolly in forum Import/Export Data
    Replies: 3
    Last Post: 08-31-2012, 08:00 AM
  5. Replies: 11
    Last Post: 03-29-2012, 02:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums