Page 1 of 2 12 LastLast
Results 1 to 15 of 26
  1. #1
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    281

    Trying to loop through selected items in a listbox to update an access table


    Hello...

    The code below seems fairly simple but I can't get past an 'Object Required' error on the If Line... I'm fairly certain it has to do with Tbl_Associates

    Because when I move the code to the 'Then' line it produces the same error...


    Code:
    Private Sub Command0_Click()
      
      Dim i As Integer  
      
        With Me.List1
          For i = 0 To .ListCount - 1
            
            If .Selected(i) Then
               
              If Tbl_Associates.[Agent Name] = .Column(0) And Tbl_Associates.Updated = .Column(1) Then
              Tbl_Associates.Approved = "Yes"
              End If
            End If
          
          Next I
          
          End With
          
        Exit Sub

  2. #2
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    Is your form bound to that table?

  3. #3
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    281
    Hi Andy...

    No, the form and table are stand alone objects...

  4. #4
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    The whole process will be much easier (and make more sense ) if form is bound to the table or a query which holds the info. Is this possible?

  5. #5
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    281
    I'm sure I can as this is just a test db. But I'm not sure how I would do either at this point...If you can give me some guidance I'm sure I can do it...

    Thanks Andy...

  6. #6
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051

    Trying to loop through selected items in a listbox to update an access table

    Ok try an update query
    Dim MySQL
    MySQL = “update tbl_associates set approved = “yes” where [agent name] = “ & column(0) & “ and [updated] = ‘“ & .column(1) & “‘“

    Debug.print MySQL

    Then check the immediate window to see whether MySQL is pulling in what you need

    Post here if you want.

    If it is right

    Currentdb.execute MySQL

    Not tested. Save copy of database.

    You may need to adjust if agentname is a text field

    I’ve asssumed updated is a text field.

  7. #7
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    281
    Wow... Nearly home...

    With a little tweaking this (below) worked perfectly insofar as it returned exactly what it should return....
    Code:
    Dim MySQL
            MySQL = "update tbl_associates set approved = ""yes"" where [agent name] = " & .Column(0,  i) & " and [updated] = '" & .Column(1,  i) & "'"
    However, it did not populate the Approved field with "Yes"

    Is it possible this may be due to the field labeled 'Updated' in the Where clause is a date field? (The Approved field which is being Set or Updated is a text field)

    Do I need to specify a date Format on that Where criteria?

  8. #8
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051

    Trying to loop through selected items in a listbox to update an access table

    Spot on

    It will depend on what values are stored in [updated] and column (1,i)

    But certainly remove the extra single quotes ‘ which is only for a text field.

    Can you post any of the debug lines. That would definitely help.

  9. #9
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    281
    Andy, I'm still working on this. (still not able to get the 'Approved' field to populate with "Yes") As soon as I hit pay dirt I will post the code and show this thread as resolved.

  10. #10
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    Ok. Try
    “where ....... and datediff(‘day’,[updated],”#” & .column(1,i) & “#=0”


    (Is the difference between the two dates in days zero)

  11. #11
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    Is the approved a yes/no field? If so, you can’t use text delimiters around yes. Just use -1 instead.

  12. #12
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    281
    Yeah, I was hoping that would be the case, unfortunately it is not, it is set to Text.

  13. #13
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    PMFJI,

    What is the list box row source? Do you have a PK field for Tbl_Associates (hopefully an Autonumber type)?

    Is there a chance that there could be more than 1 record in Tbl_Associates where "[Agent Name] = .Column(0) And Tbl_Associates.Updated = .Column(1)"?


    Really should edit the dB objects and remove any spaces in names....

  14. #14
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,973
    @andy49 - why DateDiff()?

    Maybe:

    And [updated] = #" & .Column(1, i) & "#"
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  15. #15
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051

    Trying to loop through selected items in a listbox to update an access table

    The debug.print line produces the mysql line in the intermediate window. As I’ve already said, posting that here would make it easier for us to help

    June: I wondered if one (or both) of the fields might store time as well so wouldn’t be equal as such.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 16
    Last Post: 03-31-2020, 10:19 AM
  2. Replies: 4
    Last Post: 07-26-2016, 02:53 AM
  3. Replies: 2
    Last Post: 03-27-2015, 08:48 AM
  4. Loop trough listbox to update table
    By mari_hitz in forum Programming
    Replies: 4
    Last Post: 04-21-2014, 07:16 AM
  5. Selected items in listbox
    By tomodachi in forum Access
    Replies: 1
    Last Post: 09-09-2010, 01:14 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