Results 1 to 3 of 3
  1. #1
    scoe is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2013
    Posts
    91

    Combining the code from 3 buttons into one


    Hi, I would be grateful for some fresh eyes on this if possible.
    I have a report form that runs a series of reports, for each report I have 2 buttons, one to run all the sponsors 'Run All' and another 'Run Selection' that runs all sponsors selected from a combo box. These boxes work with no problems. However I have had to set a third box that resets the combo box after the selection button has been used and before the ‘select all’ button can be used, as the table needs to be repopulated.
    What I am trying to do is combine everything into one button but cannot seem to collate the codes correctly.

    I have been trying to link them with an if /else statement but cannot get it to work. Each button code is as follows:

    Refresh Button
    [Private Sub Command25_Click()
    Me.Sponsor_List = -2
    Me.Command25.SetFocus
    Dim x As Long
    '
    ' clear out old selected states list
    '
    DoCmd.SetWarnings (WarningsOff)

    DoCmd.RunSQL ("Delete * from [Apps Partners T]")
    For x = 0 To Me.Sponsor_List.ListCount - 2
    If Me.Sponsor_List.Selected(x) = False Then
    DoCmd.RunSQL ("Insert into [Apps Partners T] (sponsor) values ('" & _
    Me.Sponsor_List.ItemData(x) & "')")

    DoCmd.SetWarnings (WarningsOn)
    End If
    Next x
    End Sub]

    Run all – Basic embedded macro to open report

    Run selection

    [Private Sub Command14_Click()

    Dim x As Long
    '
    ' clear out old selected states list
    '
    DoCmd.SetWarnings (WarningsOff)

    DoCmd.RunSQL ("Delete * from [Apps Partners T]")
    For x = 0 To Me.Sponsor_List.ListCount - 2
    If Me.Sponsor_List.Selected(x) = True Then
    DoCmd.RunSQL ("Insert into [Apps Partners T] (sponsor) values ('" & _
    Me.Sponsor_List.ItemData(x) & "')")

    End If
    DoCmd.SetWarnings (WarningsOn)
    Next x
    '
    ' open query
    '

    Const REPORTCANCELLED = 2501

    On Error Resume Next
    DoCmd.RunMacro "AppsPostInSummary"
    DoCmd.OpenReport "CIN Report Apps", View:=acViewPreview
    Select Case Err.Number
    Case 0
    ' no error
    Case REPORTCANCELLED
    ' anticipated error, so ignore
    Case Else
    ' unknown error, so inform user
    MsgBox Err.Description, vbExclamation, "Error"
    End Select

    End Sub]

    Your thoughts would be appreciated.

  2. #2
    amrut is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2012
    Location
    India
    Posts
    616
    You can put the repeated code in a sub routine like
    Sub DoThis()
    'Put your repetitive code here
    End Sub
    Then call this from click event of your buttons

  3. #3
    scoe is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2013
    Posts
    91
    Thank you so much, didn't try that, will do now.

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

Similar Threads

  1. Command buttons and Code
    By Jillette in forum Forms
    Replies: 4
    Last Post: 04-25-2012, 05:06 PM
  2. Option Buttons & After Update code - Linked
    By Norbayah in forum Forms
    Replies: 2
    Last Post: 02-28-2012, 09:52 PM
  3. VBA Code for buttons, Access 2010
    By nchesebro in forum Programming
    Replies: 3
    Last Post: 06-29-2011, 09:21 AM
  4. Navigation Buttons Stop My Code
    By millerdav99 in forum Programming
    Replies: 6
    Last Post: 03-18-2011, 11:13 AM
  5. VBA Code in Access w/ option buttons
    By WebKiid in forum Access
    Replies: 1
    Last Post: 07-16-2009, 12:20 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