Results 1 to 8 of 8
  1. #1
    swenger is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2016
    Posts
    151

    Close Form Code

    Hi,

    I have a form which has a few buttons to print. I want that when someone clicks a button it prints a report but then closes the form. my code seems to close the print window but not the form. See my original code below. I tried changing the close section to the following but got a type mismatch

    Code:
    Dim stDocName As String
    stDocName = "frmMenuLabelPrnt"
    DoCmd.close stDocName



    Code:
    Private Sub Print_Click()
    On Error GoTo Print_Click_Err
    
    
    If Frame16 = 1 Then
        DoCmd.OpenReport "PackingCard1", acViewPreview, "", "", acNormal
        DoCmd.RunCommand acCmdPrint
    Else
    If Frame16 = 2 Then
        DoCmd.OpenReport "PackingCard2", acViewPreview, "", "", acNormal
        DoCmd.RunCommand acCmdPrint
    Else
    End If
    End If
    
    
    DoCmd.close
    
    
    Print_Click_Exit:
        Exit Sub
    
    
    Print_Click_Err:
        MsgBox Error$
        Resume Print_Click_Exit
    
    
    End Sub

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You have to specify the object type first. Look at the arguments for Close.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    Docmd.close acform “formname” ??

  4. #4
    swenger is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2016
    Posts
    151
    That worked. However the Report still stays open which I am guessing is from

    Code:
     DoCmd.OpenReport "PackingCard1", acViewPreview, "", "", acNormal
    I tried the following

    Code:
    stDocName = "PackingCard1"
    DoCmd.close acReport, stDocName
    but the message is the this object is not open

    Can I print the report without running the line in the code to acpreview first?

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Just delete acViewPreview and the report will print to the default printer.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    swenger is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2016
    Posts
    151
    Thanks for your help

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No problem.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Code:
    Private Sub Print_Click()
    Be aware that "Print" is a reserved word in Access and shouldn't be used for object names.

    I modified your code a little.....
    Code:
    Private Sub btnPrint_Click()
        On Error GoTo Print_Click_Err
        Dim ReportName As String
    
        ReportName = ""
    
        Select Case Me.Frame16
            Case 1
                ReportName = "PackingCard1"
            Case 2
                ReportName = "PackingCard2"
        End Select
    
        If ReportName = "" Then
            MsgBox "No report selected"
        Else
            DoCmd.OpenReport ReportName, , , , acNormal
            DoCmd.RunCommand acCmdPrint
        End If
    
        DoCmd.Close acForm, Me.Name
    
    Print_Click_Exit:
        Exit Sub
    
    Print_Click_Err:
        MsgBox Error$
        Resume Print_Click_Exit
    
    End Sub

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

Similar Threads

  1. Code Close Form
    By mortonsafari in forum Forms
    Replies: 3
    Last Post: 07-18-2017, 11:25 PM
  2. Replies: 5
    Last Post: 10-13-2015, 02:53 PM
  3. Close form with code
    By CoachPoffy in forum Programming
    Replies: 6
    Last Post: 01-07-2015, 08:38 AM
  4. Replies: 1
    Last Post: 05-03-2012, 02:25 PM
  5. close form code not releasing recordsource..
    By dmeehanjr in forum Forms
    Replies: 1
    Last Post: 08-12-2010, 05:42 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