Results 1 to 10 of 10
  1. #1
    cofeman is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Aug 2015
    Posts
    5

    Question Open Report2 if field is empty

    Hello there,
    As newby I'm designing database for renting cars. For now all work fine, but I'm stuck on one idea.
    I have two types of contracts, one is for Companies as [Report1] and other is for Regular people [Report2].

    On the form [Rent] I have button for creating PDF file automaticly with name and contract number and stored in specified folder "c:\Contracts".
    Code for that buton is:
    Code:
    Private Sub PDFbtn_Click()
    Dim myPath As String
    Dim strReportName As String
    DoCmd.OpenReport "Report1", acViewPreview, acWindowNormal
     
    myPath = "C:\Contracts\"
    strReportName = [Name] & "_" & [LastName] & "_" & [ContractNr] & ".pdf"
    DoCmd.OutputTo acOutputReport, "Najem", acFormatPDF, myPath + strReportName, True
     
    DoCmd.Close acReport, "Report1"
    End Sub
    Now what I want is, that if filed [Client] is empty report [Report2] will be opened after press PDF button.
    On the form, [Client] is dropdown filed for select Company which is renting a car.

    Can maybe here someone plese help me to figured out how can I create such a kind of expression.
    Thanks for any help.

  2. #2
    RayMilhon is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,071
    Try this:
    Code:
    Private Sub PDFbtn_Click()
    Dim myPath As String
    Dim strReportName As String
    
    
    If isnull([Client]) Then
         DoCmd.OpenReport "Report2", acViewPreview, acWindowNormal
    Else
         DoCmd.OpenReport "Report1", acViewPreview, acWindowNormal
    End if
    
     
    myPath = "C:\Contracts\"
    strReportName = [Name] & "_" & [LastName] & "_" & [ContractNr] & ".pdf"
    DoCmd.OutputTo acOutputReport, "Najem", acFormatPDF, myPath + strReportName, True
     
    'Don't forget to close the report   I also believe there is a way to close all open reports without using each report name but I can't think of it at the moment.   I'm also not that 'confident that is true in Access.
    
    
    If isnull([Client]) Then
             DoCmd.Close acReport, "Report2"
         Else
            DoCmd.Close acReport, "Report1"
    End if
    
    End Sub

  3. #3
    RayMilhon is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,071
    The comment before the close statement looked different in the editor than it does now. That's why the comment ' is in the wrong place.

  4. #4
    cofeman is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Aug 2015
    Posts
    5
    Thanks a lot RayMilhon.
    Button work as it should be, really thanks.

    But problem now is, that created PDF have all records in it. For now I have create two record for test purpose, 1 is as Company contract, 2 is as Regular contrat.
    PDF filename was generated and stored correctly in folder, only that that there are now 2 pages in PDF - both records from table [Rent].
    Any clue for that?

  5. #5
    RayMilhon is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,071
    Can you post the query for the 2 reports?

  6. #6
    cofeman is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Aug 2015
    Posts
    5
    I have only this, but I'm sorry it's in my native language...

    This query shows correct data, but as I wrote both of ID 1 and 2 are included in created PDF.

    Click image for larger version. 

Name:	screen1.jpg 
Views:	4 
Size:	81.7 KB 
ID:	21814

  7. #7
    RayMilhon is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,071
    Unfortunately I can't see the entire query but I don't see how the query can show the correct records but the report shows all records. The report obtains it's records from the query. Double check the criteria in the query that populates the report. Do this in the Report Design View go to the Report Properties page and check the record source for the report. To the right of that you'll see 2 command buttons one being the "..." click on that and double check that the report is looking at the correct query Other than that I don't have a clue.

  8. #8
    cofeman is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Aug 2015
    Posts
    5
    Thanks again,
    I have try to add some code to docmd, found this on forum:
    this is my first PDF button, without separate Form1 and Form2:
    Code:
    Private Sub PDFbtn_Click()
    Dim myPath As String
    Dim strReportName As String
    DoCmd.OpenReport "Report1", acViewPreview, acWindowNormal, "ID=" & ID
     
    myPath = "C:\Contracts\"
    strReportName = [Name] & "_" & [LastName] & "_" & [ContractNr] & ".pdf"
    DoCmd.OutputTo acOutputReport, "Najem", acFormatPDF, myPath + strReportName, True
     
    DoCmd.Close acReport, "Report1"
    End Sub
    as you can see I add in DoCmd.OpenReport.... at the end this code
    Code:
    "ID=" & ID
    and it actually work excellent.

    Sadly it doen't work on your suggestion with IF.
    Can you maybe know why?

    Or mybe if I think correct is there any code, to put in fields on Report to hide itselft if NULL? Then I prefer to make it that way

    Thanks a lot for your time and all help.

  9. #9
    cofeman is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Aug 2015
    Posts
    5
    Hey RayMilhon,

    I figured out and it works like a charm

    In your code I add extra line, regadless I have now two Form and now my WORKING code looks like this:
    Code:
    Private Sub PDFbtn_Click()
    Dim myPath As String
    Dim strReportName As String
    
    
    If IsNull([Client]) Then
         DoCmd.OpenReport "Form2", acViewPreview, acWindowNormal, "ID=" & ID
    Else
         DoCmd.OpenReport "Form1", acViewPreview, acWindowNormal, "ID=" & ID
    End If
     
    myPath = "C:\Contracts\"
    strReportName = [Name] & "_" & [LastName] & "_" & [ContractNr] & ".pdf"
    DoCmd.OutputTo acOutputReport, "Form1", acFormatPDF, myPath + strReportName, True
    DoCmd.OutputTo acOutputReport, "Form2", acFormatPDF, myPath + strReportName, True
    
    If IsNull([Client]) Then
             DoCmd.Close acReport, "Form2"
         Else
            DoCmd.Close acReport, "Form1"
    End If
    End Sub
    Thank you very much for all suggestion and help.
    Now I can go to try make some Queries for rental list per Client and per Customer

    Thanks again,
    Best regards,
    Lali

  10. #10
    RayMilhon is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,071
    Congratulations on getting it to work.

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

Similar Threads

  1. Replies: 12
    Last Post: 10-22-2014, 02:11 PM
  2. Replies: 2
    Last Post: 06-25-2014, 10:29 PM
  3. Replies: 2
    Last Post: 04-23-2012, 10:13 PM
  4. Replies: 4
    Last Post: 03-01-2012, 08:15 PM
  5. empty field
    By amber mara in forum Access
    Replies: 2
    Last Post: 05-05-2010, 01:46 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