How do I target records on an Access form that meet certain criteria?

Barry Sweezey
Barry Sweezey used Ask the Experts™
on
I have a form on which I want to do something in records that meet certain criteria.  In my test code below, I loop through the recordset for the form, but my code sets the values for every record to the values appropriate to the first record.  The code below sets Text304 on every record to True if "rst!DateEntered > #9/27/2018 9:02:36 AM#" is true for the first record and every record to false otherwise.

Right now I'm just writing to a text field to diagnose the problem, but what I want to do is to make a field on certain records editable. How do I do that?



Private Sub Form_Load()

    Me.txtTitle.SetFocus
    Dim rst As DAO.Recordset
    Dim fld As Field
 
    Set rst = Me.Recordset
    Do While Not rst.EOF
        If rst!DateEntered > #9/27/2018 9:02:36 AM# Then
            Text304 = "True"
        Else
            Text304 = "False"
        End If
        rst.MoveNext
    Loop
    Me.Refresh
   
End Sub
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Try adding a rst.movefirst before the "Do While" loop, see how it goes
 
with DAO, you must remember to populate the Recordset as it only takes the records that have already been accessed
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
Sounds like  Text304 is an unbound textbox. It will always show the same value for every record.

For debugging, use something like this:

    Set rst = Me.RecordsetClone
    Do While Not rst.EOF
        Debug.Print rst!ID.Value ' your primary key.
        Debug.Print , rst!DateEntered.Value
        If rst!DateEntered.Value > #9/27/2018 9:02:36 AM# Then
            Debug.Print , "True"
        Else
            Debug.Print , "False"
        End If
        rst.MoveNext
    Loop

Open in new window

and study the output.
Barry SweezeySoftware Engineer

Author

Commented:
Thanks! That worked.  Now, I tried setting the locked property of my Update field (probably a bad name), but I can't change the data in that field in any record.


Private Sub Form_Load()

    Me.txtTitle.SetFocus
 'Sub Print_Field_Names()
    Dim rst As DAO.Recordset, intI As Integer
    Dim fld As Field
 
    Set rst = Me.Recordset
    Do While Not rst.EOF
        If rst!DateEntered > #9/27/2018 9:02:36 AM# Then
            Update.Locked = True
            Debug.Print rst!DateEntered & " " & "True"
        Else
            Update.Locked = False
            Debug.Print rst!DateEntered & " " & "False"
        End If
        rst.MoveNext
    Loop
    Me.Refresh
   
End Sub
Most Valuable Expert 2015
Distinguished Expert 2018
Commented:
Try this:

Private Sub Form_Load()

    Me.txtTitle.SetFocus

    Dim rst As DAO.Recordset, intI As Integer
    Dim fld As DAO.Field
 
    Set rst = Me.RecordsetClone
    Do While Not rst.EOF
        If rst!DateEntered.Value > #9/27/2018 9:02:36 AM# Then
            Me!Update.Locked = True
            Debug.Print rst!DateEntered & " " & "True"
        Else
            Me!Update.Locked = False
            Debug.Print rst!DateEntered & " " & "False"
        End If
        rst.MoveNext
    Loop
    rst.Close

End Sub 

Open in new window

But, again, all Update textboxes will be locked, not record by record.
For this to happen, use code in the OnCurrent event of the form.
Barry SweezeySoftware Engineer

Author

Commented:
Thanks again.  I was thinking I had to loop through the records, but now the program just decides if the record is editable when the user clicks on it.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial