Results 1 to 8 of 8
  1. #1
    lewis1682 is offline Advanced Beginner
    Windows 7 32bit Access 2003
    Join Date
    Aug 2013
    Posts
    37

    Running code in function if checkbox is ticked


    I have a form called "Filter_Contacts" with a subform called "View_Contacts_subform" I have some code so these results are filtered... Not the problem. I have buttons which save the results to a table called "Filter_Results" again not a problem. However on the subform I have a unbound checkbox called Include. I want the records to be added to the table only if the record is ticked in the Include checkbox here is the code I am using in the Public function:
    Code:
    Public Function addRecords()
    
    DoCmd.OpenQuery "Filter_Results_Delete" 'Deleting old results
    
    
    'Dim dbThisDB As DAO.Recordset
    
    
    Dim rcdNew As DAO.Recordset
    
    
    'Set dbThisDB = CurrentDb
    Set db = CurrentDb
    Set rcdNew = _
        db.OpenRecordset("Filter_Results", dbOpenDynaset)   'This is your Email Table recordset
    
    
    Set rcd = Me.View_Contacts_subform.Form.RecordsetClone     'This is your master recordset
    
    
    Dim intCount As Integer
    intCount = rcd.RecordCount
    
    
    
    
    
    
    
    
    'you need to loop through each filtered record, collect the data
    'and insert that data into your Filter_Results table
    
    
    'declare data types for your variables
    'FirstName, LastName, Company, County, Category
    Dim strFirstName As String
    Dim strLastName As String
    Dim strCompany As String
    Dim strCounty As String
    Dim strCategory As String 'Consider using long or integer data type if possible here
    Dim strTitle As String
    Dim strAddressLine1 As String
    Dim strAddressLine2 As String
    Dim strPostalCode As String
    Dim strTown As String
    Dim strEmail As String
    Dim numID As Integer
    strFirstName = ""
    strLastName = ""
    strCompany = ""
    strCounty = ""
    strCategory = ""
    strTitle = ""
    strAddressLine1 = ""
    strAddressLine2 = ""
    strPostalCode = ""
    strTown = ""
    strEmail = ""
    
    
    
    
    rcd.MoveFirst   'Move to the first record in your filtered set
    
    
    '********************************************
    'You also need to make sure there is data to assign to the
    'strings you declared. For example, I did not include error trap
    'here for instances where the field LastName
    'does not contain data.
    '********************************************
    
    
    Do Until rcd.EOF 'EOF is the last record and contains no data
    
    
    If Not IsNothing(rcd![FirstName]) Then
    strFirstName = rcd![FirstName]
    End If
    
    
    If Not IsNothing(rcd![LastName]) Then
    strLastName = rcd![LastName]
    End If
    
    
    If Not IsNothing(rcd![Company]) Then
    strCompany = rcd![Company]
    End If
    
    
    If Not IsNothing(rcd![County]) Then
    strCounty = rcd![County]
    End If
    
    
    If Not IsNothing(rcd![Category]) Then
    strCategory = rcd![Category]
    End If
    
    
    If Not IsNothing(rcd![Title]) Then
    strTitle = rcd![Title]
    End If
    
    
    If Not IsNothing(rcd![AddressLine1]) Then
    strAddressLine1 = rcd![AddressLine1]
    End If
    
    
    If Not IsNothing(rcd![AddressLine2]) Then
    strAddressLine2 = rcd![AddressLine2]
    End If
    
    
    If Not IsNothing(rcd![Town]) Then
    strTown = rcd![Town]
    End If
    
    
    If Not IsNothing(rcd![Email]) Then
    strEmail = rcd![Email]
    End If
    
    
    If Not IsNothing(rcd![PostalCode]) Then
    strPostalCode = rcd![PostalCode]
    End If
    
    
    rcd.MoveNext 'Move to the next Record in your Filtered set
    
    
                rcdNew.AddNew   'Create a new record in your email table
                rcdNew![FirstName] = strFirstName   'populate the fields
                rcdNew![LastName] = strLastName
                rcdNew![Company] = strCompany
                rcdNew![County] = strCounty
                rcdNew![ID] = numID
                rcdNew![Category] = strCategory
                rcdNew![Title] = strTitle
                rcdNew![AddressLine1] = strAddressLine1
                rcdNew![AddressLine2] = strAddressLine2
                rcdNew![Town] = strTown
                rcdNew![Email] = strEmail
                rcdNew![PostalCode] = strPostalCode
                rcdNew.Update   'Save the record
                
                Loop
                
    'tidy things up
    Set rcd = Nothing
    rcdNew.Close
    Set rcdNew = Nothing
    Set db = Nothing
    End Function
    Thank you.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    This checkbox is bound to a field? If so, this code can be significantly reduced.

    CurrentDb.Execute "INSERT INTO Filter_Results(FirstName, LastName, etc) SELECT FirstName, LastName, etc FROM tablename WHERE Include = True"

    now clean up:

    CurrentDb.Execute "UPDATE Contacts SET Include = False"

    Me.Refresh

    Why do you even bother saving to another table when you can easily use a query of filtered records?
    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.

  3. #3
    lewis1682 is offline Advanced Beginner
    Windows 7 32bit Access 2003
    Join Date
    Aug 2013
    Posts
    37
    This include checkbox cannot be bound to a table. And I cannot use a query because the user chooses how to filter it with a range of options. When I use to use a qurey to do this it would not return results if some were blank. And in this database some fields may be left blank. I need a way say like a if statement to check if the checkbox is ticked in the recordset then send all the data to the form. Else miss it.
    Last edited by lewis1682; 09-21-2013 at 02:21 PM. Reason: Typo

  4. #4
    Dal Jeanis is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742

    Cleaned up version with notes

    The simplest way to deal with it is to add a line of code to your function which says, in effect, if the checkbox isn't checked, then exit the function.

    Assuming that your function is behind the form, and the checkbox is named checkbox22 and is on either the form or the subform, that line would be one of these two, respectively.
    Code:
    If NOT Me.checkbox22 Then Exit Function
    If NOT Me.lnkToSubform.Form.Checkbox22 Then Exit Function
    
    If the function is in a general module, then you'll need to establish addressability to the form first, something like this
    Code:
    If NOT Forms!MyForm.Checkbox22 Then Exit Function
    If NOT Forms!MyForm.lnkToSubForm.Form.Checkbox22 Then Exit Function
    
    You can see this page http://access.mvps.org/access/forms/frm0031.htm for a handy guide to how to refer to a particular control, based upon where you are.


    QUICK REWRITE AND CLEANUP

    I took the liberty of straightening up your function while I was looking at it. You weren't clearing out the values after each record, so if the second lastname was NULL, you were loading the first record's lastname value into the second record's lastname field, and so on.
    Code:
    Public Function addRecords()
    
       Dim db As Object
       Dim rcd As object
       Dim rcdNew As DAO.Recordset
       Dim intCount As Integer
    
    'Table Variables
       Dim strFirstName As String
       Dim strLastName As String
       Dim strCompany As String
       Dim strCounty As String
       Dim strCategory As String 
       Dim strTitle As String
       Dim strAddressLine1 As String
       Dim strAddressLine2 As String
       Dim strPostalCode As String
       Dim strTown As String
       Dim strEmail As String
       Dim numID As Integer
    
    ' skip the whole routine if the box isn't checked
       If NOT Me.checkbox22 Then Exit Function
    
    'Delete old results
       DoCmd.OpenQuery "Filter_Results_Delete" 
    'Address current database
       Set db = CurrentDb
    'Open Email Table recordset
       Set rcdNew = db.OpenRecordset("Filter_Results", dbOpenDynaset)   
    'Open your master recordset 
       Set rcd = Me.View_Contacts_subform.Form.RecordsetClone     
    
    ' Exit the routine if there are no records to process.
       intCount = rcd.RecordCount
       If intCount = 0 Then Goto addRecords_Complete
    
    'Move to the first record in your filtered set
       rcd.MoveFirst  
       numID = 0
    
    '********************************************
    ' Copy each record in the filtered master set 
    ' to the results set
    '********************************************
       Do Until rcd.EOF 
          numID = numID + 1
       ' For each field in the record, we take the input and 
       ' concatenate it to the empty string "".  That way, 
       ' if the old input is Null or empty, then the new 
       ' field string will at least be a valid empty string.
          strFirstName = "" & rcd![FirstName]
          strLastName = "" & rcd![LastName]
          strCompany = "" & rcd![Company]
          strCounty = "" & rcd![County]
          strCategory = "" & rcd![Category]
          strTitle = "" & rcd![Title]
          strAddressLine1 = "" & rcd![AddressLine1]
          strAddressLine2 = "" & rcd![AddressLine2]
          strTown = "" & rcd![Town]
          strEmail = "" & rcd![Email]
          strPostalCode = "" & rcd![PostalCode]
       'Create a new record in your email table
          rcdNew.AddNew   
       'populate the fields   
          rcdNew![FirstName] = strFirstName   
          rcdNew![LastName] = strLastName
          rcdNew![Company] = strCompany
          rcdNew![County] = strCounty
          rcdNew![ID] = numID
          rcdNew![Category] = strCategory
          rcdNew![Title] = strTitle
          rcdNew![AddressLine1] = strAddressLine1
          rcdNew![AddressLine2] = strAddressLine2
          rcdNew![Town] = strTown
          rcdNew![Email] = strEmail
          rcdNew![PostalCode] = strPostalCode
       'Save the record
          rcdNew.Update  
       'Move to the next Record in your Filtered set
          rcd.MoveNext 
                
       Loop
                
       'tidy things up
    addRecords_Complete:
       Set rcd = Nothing
       rcdNew.Close
       Set rcdNew = Nothing
       Set db = Nothing
    End Function
    Various notes -

    For each field in the record, we take the input and concatenate it to the empty string "". That way, if the old filtered input is Null or empty, then the new field string will at least be a valid empty string.

    Since intCount isn't being used anywhere, it was probably intended to exit the routine if there were no records to process. So I coded that.

    Since numID never got a value, I just coded it as sequential from 1 by 1.

    I moved the rcd.MoveNext line after you complete processing the prior record. It's not a good idea to do it in the other order, since when you come back to support the code in a year, you might forget the record isn't there anymore and try to do something with the record that was already left behind...

    The EOF property being true means there are no more records to process. You shouldn't think of that has having a record with all NULL values, so much as having no record at all.


    In general, I would prefer to test that checkbox's value before calling the routine, but if that's not practical, then the way it's coded here should work. However, it's all aircode, so be sure to back up your database before using this code, and be very skeptical until you've proved to yourself that it works. Then back up the databse again. "Too many backups" is a nonsense phrase.

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    I don't understand the checkbox. You stated 'only if the record is ticked in the Include checkbox'. If the checkbox is unbound then the checkbox value will be the same in all records on the form.
    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.

  6. #6
    lewis1682 is offline Advanced Beginner
    Windows 7 32bit Access 2003
    Join Date
    Aug 2013
    Posts
    37
    Okay so i have added the include field to the table, so i how would i skip the record when adding them to a table like you can see above if the records checkbox is unticked?

  7. #7
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    If Me.checkboxname Then
    'code to export record
    End If
    rcd.MoveNext
    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.

  8. #8
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by lewis1682 View Post
    Okay so i have added the include field to the table, so i how would i skip the record when adding them to a table like you can see above if the records checkbox is unticked?
    You are placing your filtered results into a table so you can email the data. It seems as though you are using this new temp table as a launching pad to send these emails the user selected. Have you considered making this temp table a permanent table of email logs?


    If you distinguished your table as tblEmailsLog, you could include fields such as "Active", "UserID", "ContactID", "InfamousCheckBox".

    After forwarding this information to the log table you could transfer the form's recordsource or even launch a new form to query the tblEmailsLog and relative data. The user could then create a subset or subfiltered set of records by updating the Boolean for your "include" checkbox. This eliminates multiple users editing the original contacts table. After successfully completing the emails you can update the user session as complete or not active. Plus, no need to duplicate all of the string variables.

    Just a thought I had.

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

Similar Threads

  1. Replies: 4
    Last Post: 06-24-2013, 03:36 AM
  2. If Checkbox is ticked, Data Must be entered
    By DTK0902 in forum Access
    Replies: 8
    Last Post: 12-01-2012, 04:32 PM
  3. Open report if checkbox is ticked
    By Patience in forum Reports
    Replies: 3
    Last Post: 06-23-2010, 08:34 AM
  4. running a function after update
    By jamin14 in forum Programming
    Replies: 9
    Last Post: 04-06-2010, 09:40 AM
  5. Running a VB function in a Macro
    By JuuJuu in forum Access
    Replies: 1
    Last Post: 10-27-2009, 02:50 AM

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