We help IT Professionals succeed at work.

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

Barry Sweezey
on
54 Views
Last Modified: 2018-10-15
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

CERTIFIED EXPERT

Commented:
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
CERTIFIED EXPERT
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
CERTIFIED EXPERT
Most Valuable Expert 2015
Distinguished Expert 2018
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
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.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.