Results 1 to 9 of 9
  1. #1
    Rick5150 is offline Novice
    Windows Vista Access 2007
    Join Date
    Oct 2011
    Posts
    16

    Open Current Report With Form Button

    I have no idea how to write code, so I would appreciate it if someone could help me with this problem- and it is a simple one for you guys, I am sure.

    I am building a database. I have attached a form which works beautifully. I have designed a report that shows every thing I need. All I want is a button on the form that shows the current information as a report.



    This was easy to do in previous versions of Access. Now, I cannot figure it out.

  2. #2
    varadin is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Posts
    10
    I use the following OnClick code attached to a button on the form....

    Assuming you use a unique identifier - mine was "ID" the code below should work for you. It saves the record if it needs to, makes sure it's not a new record (blank) and then selects the current record by ID and assigns data to variable strWhere and then opens a your report "ReportName" as a preview, and passes data to it.


    Code:
    Private Sub cmdPrint_Click()
        Dim strWhere As String
    
        If Me.Dirty Then    'Save any edits.
            Me.Dirty = False
        End If
    
        If Me.NewRecord Then 'Check if blank record
            MsgBox "Select a record to print"
        Else
            strWhere = "[ID] = " & Me.[ID] 'Selects current form data
            DoCmd.OpenReport "ReportName", acViewPreview, , strWhere
        End If
    End Sub

  3. #3
    Rick5150 is offline Novice
    Windows Vista Access 2007
    Join Date
    Oct 2011
    Posts
    16
    I cannot understand what that is even doing. I am baffled about why Access has removed the "current record" option or whatever it was called that I used with the button wizard in earlier versions...

  4. #4
    varadin is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Posts
    10
    I hear ya, it took me a while to figure it out as well.

  5. #5
    Rick5150 is offline Novice
    Windows Vista Access 2007
    Join Date
    Oct 2011
    Posts
    16
    I had to play around with it a little, and I am not certain what the "Dirty" reference was, but I commented it out and it worked fine - so I would up deleting parts of the code and was left with the following edit(s):

    Private Sub buttonWorkOrder_Click()
    Dim strWhere As String

    If Me.NewRecord Then 'Check if blank record
    MsgBox "Select a record to preview"
    Else
    strWhere = "[ID] = " & Me.[ID] 'Selects current form data
    DoCmd.OpenReport "Work Order Report", acViewPreview, , strWhere
    End If
    End Sub

    Thank you for your help on this. I cannot tell you how much I appreciate it. I still have no idea what the code is saying, but it works.

  6. #6
    varadin is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Posts
    10
    My comment showed you what the dirty did, if the record had been edited but not saved - it will do so when you click the button. I thought this would be necessary to have the most recent data in the report. You are most welcome - like I said, it took me forever to figure/find this one out.

  7. #7
    Rick5150 is offline Novice
    Windows Vista Access 2007
    Join Date
    Oct 2011
    Posts
    16
    Oh, what I meant was, is "Dirty" part of the language, or is that something only relevant to your database?

  8. #8
    varadin is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Posts
    10
    Dirty is part of the language, not my database. I even changed "ReportName" for you so you wouldn't be confused by "CoC1" =)

  9. #9
    Rick5150 is offline Novice
    Windows Vista Access 2007
    Join Date
    Oct 2011
    Posts
    16
    Got it. Thank you for the clear explanation. I will add it back as it seems like a great feature to have. Thank you again!

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

Similar Threads

  1. Open (sub)form linking 2 criteria on current form
    By websterh in forum Programming
    Replies: 2
    Last Post: 02-07-2011, 11:56 PM
  2. Current Date button on Report
    By Steven.Allman in forum Programming
    Replies: 8
    Last Post: 10-31-2010, 03:28 PM
  3. Button to open a Report
    By 95DSM in forum Reports
    Replies: 1
    Last Post: 07-09-2010, 11:11 AM
  4. Replies: 3
    Last Post: 01-14-2010, 08:32 AM
  5. Open form to current record
    By rbpd5015 in forum Access
    Replies: 1
    Last Post: 08-28-2009, 01:53 AM

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