Results 1 to 6 of 6
  1. #1
    neuk is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    May 2019
    Posts
    36

    Update Checkbox After Form Closes


    When a form closes, the following code is supposed to test whether any field in each record is null on a table (“tbl3PartN”). If a record has a null field, it is then supposed to check a box in the field(“valid”). The code that I have is as follows:

    Code:
    Private Sub Command46_Click()
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim fld As DAO.Field
     
    Set db = CurrentDb
    Set rs = db.OpenRecordset("tbl3PartN")
     
    With rs
        Do While Not .EOF
            For Each fld In .Fields
                If IsNull(fld.Value) Then
                    [Valid] = False
                End If
            Next
            .MoveNext
        Loop
    End With
    I tested the code a number of times and it goes through each record to check if it is null, but I can’t get it to update the field “valid”. Any ideas?

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Within the If/Then block try

    rs.Edit
    rs![Valid] = False
    rs.Update
    Exit For

    Do you really need to check every record every time? I'd check the record on the form.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    neuk is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    May 2019
    Posts
    36
    Thanks. That works, however, it's very slow. I think that is why you recommend checking the record on the form. That was my original design, which looked something like this:

    Code:
    Private Sub Command46_Click()
       Dim ctrl As Control
        For Each ctrl In Me.Section(acDetail).Controls
            Select Case ctrl.ControlType
            Case acTextBox, acComboBox
                If IsNull(ctrl.Value) Then
                    Me.chkValid = False
                    Exit Sub
                End If
            End Select
        Next
        Me.chkValid = True
    End Sub
    The problem I had with this is that it only checks the record that is selected. To give some additional context on what I'm doing, I have a combobox (cboFltrValid) that I manually assigned row source as follows:
    -1;"Valid";0;"Non-Valid";1;"All"

    When id -1 is selected, the afterupdate event will trigger the form to filter all records with a checkmark (chkValid), if id 0 is selected the form will filter all records with no checkmark, if id 1 is selected, the form will filter both checkmarks and non-checkmarks. While that is fairly easy to do, the trick is updating the table such that any record with a null field will not have a checkmark, when the user selects whatever option. Simply put, If any field in a record is null then chkValid is false else chkValid is true.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Quote Originally Posted by neuk View Post
    The problem I had with this is that it only checks the record that is selected.
    I would expect to run the process on the entire table once, from then on you should only have to check the record being added/edited on the form.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    neuk is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    May 2019
    Posts
    36
    Yes. I've been going back and forth on this, and I agree. Once the table has been updated, I should only have to update a single record at a time when the user changes/edits a given record. Thanks for talking some sense into me.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    No problem, post back if you get stuck.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 2
    Last Post: 08-03-2017, 04:58 AM
  2. Replies: 2
    Last Post: 06-14-2016, 03:01 PM
  3. Set focus when pop up form closes
    By projectpupil7 in forum Access
    Replies: 4
    Last Post: 11-15-2014, 02:02 PM
  4. Form closes after mssage box
    By liam01752 in forum Programming
    Replies: 1
    Last Post: 12-06-2010, 03:30 PM
  5. Replies: 1
    Last Post: 12-09-2005, 09:16 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