Results 1 to 6 of 6
  1. #1
    dref is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jan 2010
    Posts
    55

    Loop using a command button

    I have a form with a combo for selecting years.


    The form has a command button which if cliched should update table A with the year selected in the combo box.
    Updates are for fields where there is no year in table A.
    Any help?
    Thanks

  2. #2
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    No loop necessary. Create an update query that uses the combo and has the appropriate criteria and run it from the button.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Or, if you're not comfortable with writing Queries, you can loop through the RecordSet and fill in Records that have no year.

    Here's a step-by-step, modified for your particular case, for doing this.

    • Create a Command Button.
    • Name it cmdUpdateYearField
    • Place the code below in the button's OnClick event

    Code:
    Private Sub cmdUpdateYearField_Click()
    
    If Nz(Me.cboYears, "") <> "" Then
    
    Dim rs As DAO.Recordset
    Dim UF_Rec As String
    
    Set db = CurrentDb
    
    Set rs = db.OpenRecordset("YourTableName")
       
       UF_Rec = Me!UniqueField
    
      Do While Not rs.EOF
      rs.Edit
      If Nz(rs!YearField, "") = "" Then
       rs!YearField = Me.cboYears
      End If
      rs.Update
      rs.MoveNext
      Loop
      rs.Close
      Set rs = Nothing
      
      Me.Requery
    
      Me.Recordset.FindFirst "[UniqueField] = '" & UF_Rec & "'"
     
    Else
      MsgBox "You Must First Select a Year!"
      cboYears.SetFocus
      cboYears.Dropdown
      
    End If
    
    End Sub


    You'll need to replace

    • cboYears with the actual name of your 'years' Combobox
    • YourTableName with the actual name of your underlying Table
    • YearField with the actual name of this Field in your Table
    • UniqueField with just that; a Field that is unique to a given Record, such as a Primary Key Field


    If your UniqueField is defined as a Text Datatype, the above code will work.

    If, on the other hand, your UniqueField is defined as a Number Datatype or an AutoNumber Datatype, the above code will not work!

    You'll need to replace

    Dim UF_Rec As String

    with

    Dim UF_Rec As Integer

    and replace

    Me.Recordset.FindFirst "[UniqueField] = '" & UF_Rec & "'"

    with

    Me.Recordset.FindFirst "[UniqueField] = " & UF_Rec

    Linq ;0)>

  4. #4
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    The bigger the table the more horribly inefficient that method would be.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    That's probably true, but most people coming here for help, especially those using names like TableA, are not working on a level where they're dealing tens of thousands or millions of records! And it does work!

    Linq ;0)>

  6. #6
    mediaad is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2012
    Posts
    1
    Thanks!!!!!!!!!!!!!!!!!!!!!!!!!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 06-15-2012, 10:47 AM
  2. Button Command
    By JayX in forum Access
    Replies: 7
    Last Post: 12-27-2011, 12:58 PM
  3. Command Button
    By tia in forum Access
    Replies: 1
    Last Post: 11-20-2011, 11:47 AM
  4. Replies: 1
    Last Post: 07-27-2010, 02:27 PM
  5. Command Button Help!
    By arthura in forum Programming
    Replies: 3
    Last Post: 06-30-2009, 12:55 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