Results 1 to 10 of 10
  1. #1
    JessicaNicole is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Feb 2012
    Location
    Washington
    Posts
    8

    Command Button to Open Report Based on Current Record

    I'm sure this is super simple and I'm just over-thinking.

    I have a tabbed form with subforms on each tab. They all update how they are supposed to. I want to put a command button on one of the subforms that will open according to the record that the subform is currently on. I tried just doing the normal wizard, didn't work. So I looked online for some code and this is what I found:

    Private Sub cmdOpenGrowerInforpt_Click()
    DoCmd.RunCommand acCmdSaveRecord
    DoCmd.OpenReport "rptGrowerInformation", acViewPreview, , "[GrowerID]='&"
    [GrowerID]
    End Sub



    I bolded that last part because it's whats causing the code not to run. I don't know what I should be putting there to make it work. Hopefully I'm making sense. Any help would be great. Thanks!

  2. #2
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    I think it's just a syntax error. Try this:

    DoCmd.OpenReport "rptGrowerInformation", acViewPreview, , "[GrowerID]='" me![GrowerID] & "'" if [GrowerID] is NOT numeric,

    and

    DoCmd.OpenReport "rptGrowerInformation", acViewPreview, , "[GrowerID] = " & Me![GrowerID] if [GrowerID] IS numeric.

    I use me![GrowerID] to ensure MS Access knows it's a form control, though it would probably run without it.

    [GrowerID] must be a field in the record source table/query for the report.

    John

  3. #3
    JessicaNicole is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Feb 2012
    Location
    Washington
    Posts
    8
    I put that code in and nothing happens when I click on the button. I don't have any compile errors with that code either.

  4. #4
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Are you sure that the docmd.openreport is being executed? You can check by putting a msgbox "Report opening" command just before it.

    If the docmd.openreport is running, could it be opening the report, but putting it behind other windows? I've seen that happen.

    If the msgbox does not display - is the code in the OnClick event for the right button?

    John

  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
    The example for the syntax where GrowerID is NOT numeric, i.e. is defined as Text, was missing an Ampersand:

    Code:
    DoCmd.OpenReport "rptGrowerInformation", acViewPreview, , "[GrowerID]= '" & Me![GrowerID] & "'"


    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  6. #6
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Thanks, Linq - that's what happens when one doesn't proof read.

    J.

  7. #7
    JessicaNicole is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Feb 2012
    Location
    Washington
    Posts
    8
    The message box isn't showing up either. I checked and it is in the right on click. I only have this button in my db so far. The GrowerID is a numeric number.

  8. #8
    ketbdnetbp is offline Competent Performer
    Windows 7 32bit Access 2003
    Join Date
    Mar 2011
    Location
    Midwest
    Posts
    254
    JessicaNicole - You could try...

    Private Sub cmdOpenGrowerInforpt_Click()
    DoCmd.RunCommand acCmdSaveRecord
    Dim stDocName As String
    stDocName = "rptGrowerInformation"
    Dim stLinkCriteria As String
    stLinkCriteria = "[GrowerID]=" & "Me![GrowerID]"
    DoCmd.OpenReport stDocName, acViewPreview, stLinkCriteria
    End Sub

    In addition, I don’t know if it really matters but, you may want to check the reports Filter On property, it may have to be YES.

    Hope this helps,

    Jim

  9. #9
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Do the properties for the command button have [Event Procedure] for the On Click event? If not, that is the problem - Access does not know there is a procedure associated with the Click event.

    Where is the procedure? It has to be in the code module for the form.

    You are not even seeing the MsgBox, which indicates that for some reason the procedure is not being executed at all.

    John

  10. #10
    joshynaresh is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Aug 2013
    Posts
    131
    I have one form "Journal Credit" have command bottom "Post" and i have one report "Journal Receipt". both table have ID field with same value.

    I want when I click on post bottom of form "Journal Credit" record should be saved and report "journal Receipt" should be open as print preview as dialog with same record.
    is it possible with VBA code.
    Plz help be with VBA Code.

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

Similar Threads

  1. Replies: 2
    Last Post: 05-11-2012, 11:52 AM
  2. Command button open url
    By patrickmcdiver in forum Programming
    Replies: 2
    Last Post: 03-06-2012, 11:08 AM
  3. Command button to open Report
    By swagger18 in forum Programming
    Replies: 0
    Last Post: 11-17-2011, 02:56 AM
  4. Open Current Report With Form Button
    By Rick5150 in forum Reports
    Replies: 8
    Last Post: 10-12-2011, 02:28 PM
  5. Replies: 3
    Last Post: 01-14-2010, 08:32 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