Avatar of Barry Sweezey
Barry Sweezey
Flag 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
Microsoft AccessVBA

Avatar of undefined
Last Comment
Barry Sweezey

8/22/2022 - Mon
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
Gustav Brock

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 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
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
Gustav Brock

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Barry Sweezey

ASKER
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.