Change a field for all records in a filtered subform. (1 Viewer)

MooTube

Registered User.
Local time
Today, 14:52
Joined
Jul 2, 2015
Messages
31
Hello,

I am currently developing a crude Contact manager database and need a quick way of entering data into a selection of records (around 1000 at a time). At the moment I am manually going through all records and changing the "DateLastEmail" field manually, which can be very tiring.

I was wondering if there is anyway to assign a button to change the field for all records to today's date or something of the like.

Currently I have a form which filters my subform. The subform resides as a seperate query and when the "Apply Filter" button is pressed it requeries with a change of .filter property, so I guess that all that needs to be done is to change all records that exist within the query, but I am stuck on a way to do this without manually entering it.

Any help is much appreciated!
 

Gasman

Enthusiastic Amateur
Local time
Today, 22:52
Joined
Sep 21, 2011
Messages
14,465
I do something very similar with this code.

However it also sends the email.

Basically I ...
Set the filter to Email = "Yes"
Process each record
Release the filter

The only difference is this is all on one form. Whilst I have a subform of the same data in datasheet view, it does not get referenced.

HTH

Code:
Private Sub cmdEmail_Click()
On Error GoTo Err_Handler
' Automate the routine to send notifications of Payments and deposits for clients
Dim strFilter As String
Dim strDate As String
Dim strType As String, strClient As String, str3rdID As String, str3rdParty As String, str3rdPartyType As String, strAmount As String, strRef As String, strMethod As String
Dim strCaseWorker As String, strDatetype As String, strPad As String, strEndPad As String, strPadCol As String, strNotes As String
Dim iColon As Integer
Dim lngCurrentRec As Long
Dim blnDisplayMsg As Boolean
Dim db As Database
Dim rs As DAO.Recordset, rsCW As DAO.Recordset

' Now the Outlook variables
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment
Dim strSigPath As String, strSignature As String, strAttachFile As String
Dim strHeader As String, strFooter As String, strBody As String, strTemplatePath As String, strAppdata As String
Dim intBody As Integer

' Set up HTML tags
strPad = "<tr><td>"
strEndPad = "</td></tr><tr></tr>"
strPadCol = "</td><td>  </td><td>"

On Error GoTo Err_Handler

'Establish all the static Outlook Data

    ' Get appdata path
    strAppdata = Environ("Appdata")
    
    ' Set paths
    strTemplatePath = strAppdata & "\Microsoft\Templates"
    strSigPath = strAppdata & "\Microsoft\Signatures\Ssafa.htm"
    
    
    'Get the signature if it exists
    If Dir(strSigPath) <> "" Then
        strSignature = GetBoiler(strSigPath)
        intBody = InStr(strSignature, "<BODY>")
        strHeader = Left(strSignature, intBody + 5)
        strFooter = Mid(strSignature, intBody + 6)
    End If

    ' Create the Outlook session.
    Set objOutlook = GetObject(, "Outlook.Application")
    'Set objOutlook = New Outlook.Application
    
' Open lookup table for Email CC Name (normally a Case Worker)
    Set db = CurrentDb
    Set rsCW = db.OpenRecordset("SELECT * from Lookups WHERE Type = 'Email'")
' Save the current record position
    lngCurrentRec = Me.CurrentRecord
    
' Now set the filter to get just the rows we want
    strFilter = "Yes"
    
    Me.Filter = "EmailStatus = """ & strFilter & """"
    Me.FilterOn = True

' Make sure we save any changed data and then get recordset
If Me.Dirty Then Me.Dirty = False

Set rs = Me.RecordsetClone

' Now walk through each record
Do While Not rs.EOF

        strDate = TransactionDate
        strType = TranType
        strClient = Client
        str3rdParty = ThirdParty
        strAmount = Format(Amount, "Currency")
        strRef = Reference
        strMethod = Method
        ' Need to get the Case Worker name from table'
        If CaseWorker > 0 Then
            rsCW.FindFirst "[ID] = " & CaseWorker
            strCaseWorker = rsCW!Data
        Else
            strCaseWorker = ""
        End If
        strNotes = Nz(Notes, "")
        ' Update the record
        rs.Edit
        rs!EmailStatus = "Sent"
        'rs.EmailStatus = "Sent"
        rs!EmailDate = Date
        rs.Update
        
        ' Now populate the outlook fields
        ' Create the message.
        'Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
        
        Set objOutlookMsg = objOutlook.CreateItemFromTemplate(strTemplatePath & "\SSAFA Email.oft")
    
        With objOutlookMsg
            ' Add the To recipient(s) to the message.
            Set objOutlookRecip = .Recipients.Add("**** - SSAFA Swansea")
            objOutlookRecip.Type = olTo
    
            ' Add the CC recipient(s) to the message.
            Set objOutlookRecip = .Recipients.Add("** SSAFA West Glamorgan Branch")
            objOutlookRecip.Type = olCC
            
            If strCaseWorker <> "" Then
                Set objOutlookRecip = .Recipients.Add(strCaseWorker)
                objOutlookRecip.Type = olCC
            End If
    
            ' Set the Format, Subject, Body, and Importance of the message.
            .BodyFormat = olFormatHTML
            strDatetype = "Date "
            If strType = "Payment" Then
                .Subject = " Payment Made - " & strClient
                str3rdPartyType = "Recipient: "
                strDatetype = strDatetype & "Paid: "
            Else
                .Subject = "Deposit Received - " & strClient
                str3rdPartyType = "Received From: "
                strDatetype = strDatetype & "Received: "
    
            End If
            
            ' Now build the body of the message
            
            ' Make sure we have a colon in client, else use whole field
            iColon = InStr(strClient, ":")
            If iColon = 0 Then iColon = Len(strClient) + 1
            
            .HTMLBody = strHeader & "<table><tr>"
            .HTMLBody = .HTMLBody & "<td>" & "Client: " & strPadCol & Left(strClient, iColon - 1) & strEndPad
            .HTMLBody = .HTMLBody & strPad & str3rdPartyType & strPadCol & str3rdParty & strEndPad
            .HTMLBody = .HTMLBody & strPad & strDatetype & strPadCol & strDate & strEndPad
            .HTMLBody = .HTMLBody & strPad & "Payment Method: " & strPadCol & strMethod & strEndPad
            .HTMLBody = .HTMLBody & strPad & "Reference: " & strPadCol & strRef & strEndPad
            .HTMLBody = .HTMLBody & strPad & "Payment Amount: " & strPadCol & strAmount & strEndPad
            ' Add any notes if they exist
            If Len(strNotes) > 0 Then
                .HTMLBody = .HTMLBody & strPad & "Notes: " & strPadCol & strNotes & strEndPad
            End If
            .HTMLBody = .HTMLBody & "</table>" & strFooter
            '.Importance = olImportanceHigh  'High importance
            'Debug.Print strHeader
            'Debug.Print .htmlbody
            'Debug.Print strFooter
            ' Resolve each Recipient's name.
            For Each objOutlookRecip In .Recipients
                objOutlookRecip.Resolve
            Next
    
            ' Should we display the message before sending?
            If blnDisplayMsg Then
                .Display
            Else
                .Save
                .Send
            End If
        End With
'    End If

' Now get the next record
    
        rs.MoveNext

Loop


Proc_Exit:
    ' Switch off the filter and release recordset object, and go back to record we were on
    Me.FilterOn = False
    DoCmd.GoToRecord , , acGoTo, lngCurrentRec
    Set rs = Nothing
    Set rsCW = Nothing
    Set db = Nothing
    Exit Sub
    
Err_Handler:
    MsgBox Err.Number & " " & Err.Description
    Resume Proc_Exit



End Sub
 

MooTube

Registered User.
Local time
Today, 14:52
Joined
Jul 2, 2015
Messages
31
Ah, the "'Make sure we save any changed data and then get recordset" part of your code really helped!

I got it working with this code, a lot less intense than yours, but I don't need to send emails with the code!

Code:
Private Sub SaveNClose_Click()

    DoCmd.Save

    With [Forms]![BenSearchForm]![BenSearchSub].[Form].RecordsetClone
        Do Until .EOF
            .Edit
            !LastEmail = Me.EmailID
            .Update
            .MoveNext
        Loop
            MsgBox "A Total Of " & .RecordCount & "Emails Were Sent"
    End With
    
    DoCmd.Save
    DoCmd.Close

    DoCmd.OpenForm ("BenSearchForm")
    [Forms]![BenSearchForm].[FilterOn] = True
    
End Sub

When the pop-up form is closed, it updates all records that are in the Filtered subform. It saves at the beginning so that the EmailID can be set to something (otherwise the value is NULL). It then opens the form again with the filter reapplied as a basic level of requery so that you can see nay changes made.

I used this thread to help me:

http://forums.devarticles.com/micro...cords-in-subform-from-parent-form-444719.html
 

Gasman

Enthusiastic Amateur
Local time
Today, 22:52
Joined
Sep 21, 2011
Messages
14,465
That is how most of my code is constructed, cobbled together from other snippets. :)
 

Users who are viewing this thread

Top Bottom