Results 1 to 6 of 6
  1. #1
    CGM3 is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Atlanta, GA
    Posts
    49

    Referencing A Form Field In A Report

    Okay, I've written a report which accesses the records in the table involved for a given Fiscal Year and Point-of-Contact person. The criteria are entered into combo boxes on a form, and VBA code runs it with the command line:

    DoCmd.OpenReport strReportName, acViewPreview, , strSQL

    where strReportName holds the name of the report, and strSQL is the 'WhereCondition' specifying the criteria, created from the combo boxes:

    strSQL = "[Fiscal Year]='" & Me!cboFY.Column(0) & "' And [POC]='" & Me!cboPOC.Column(0) & "'"

    The header of the report is a text box with a formula that references both [Fiscal Year] and [POC] to show the fiscal year and Point-of-Contact involved.

    The thing is, if no records are found for the given criteria, the header reads #Error. I can insert an IIf(IsError([Fiscal Year]),"(no data)",[Fiscal Year]) check (and the same for [POC]), but I thought if I referenced the combo boxes on the form instead I could avoid the whole problem.

    Unfortunately, when I make the header source data:

    ="Report for " & frmMainForm!cboPOC.Column(0) & " During FY " & frmMainForm!cboFY.Column(0)



    MS Access changes it to:

    ="Report for " & [frmMainForm]![cboPOC].[Column](0) & " During FY " & [frmMainForm]![cboFY].[Column](0)

    which, well, doesn't work. Is there a proper format to "fetch" the value of a field on a form into a report?

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Why not just use the NoData event of the report to put up a message and close the report?

  3. #3
    CGM3 is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Atlanta, GA
    Posts
    49
    Because the people for whom I'm creating the report want to be able to print the hardcopy showing that "John Smith" has no records for the given Fiscal Year.

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Good point. In that case just go through the Forms collection to reference the control:
    FORMS.frmMainForm!cboPOC.Column(0)

  5. #5
    CGM3 is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Atlanta, GA
    Posts
    49
    That works. For that matter, I got to looking into the On No Data event and found you can reference fields on the form there as well, such as:

    Code:
     
    Private Sub Report_NoData(Cancel As Integer)
     
         MsgBox "There are no records for " & Form_frmMainForm!cboPOC.Column(0) & " during FY " & Form_frmMainForm!cboFY.Column(0)
    , vbExclamation, "No Records Found"
         Cancel = True
     
    End Sub
    Both of these go into my Access Bits 'n' Stuff folder. Thanks.

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Excellent! Learning is a ton of fun and you're welcome.

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

Similar Threads

  1. Replies: 3
    Last Post: 06-23-2010, 02:02 PM
  2. Referencing a Subroutine
    By Lockrin in forum Programming
    Replies: 1
    Last Post: 02-26-2010, 10:09 AM
  3. Replies: 0
    Last Post: 12-08-2009, 01:19 PM
  4. Referencing table data in field validation rule
    By toad848 in forum Database Design
    Replies: 3
    Last Post: 03-19-2009, 07:03 AM
  5. Referencing problem
    By thegamezn in forum Forms
    Replies: 1
    Last Post: 12-09-2005, 10:13 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