Link to home
Start Free TrialLog in
Avatar of Barry Sweezey
Barry SweezeyFlag for United States of America

asked on

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

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
Avatar of Arana (G.P.)
Arana (G.P.)

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
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.
Avatar of Barry Sweezey

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.