Event Code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ladybug via AccessMonster.com

    #1

    Event Code

    I have a form called "EmpRpt". It has three fields. First field is
    "chrUserID, " this is a combo box with a row source from another table. The
    Second Field is "BeginDate. " The Third field is "EndDate."

    I then have a command button. I want to put a code in this buttons on click
    event. The code should open up a report called "EmpRpt."

    The report will have the three fields just like the form. I want those three
    fields to appear with the data the user entered in the form. I would also
    like this report to open in print preview.

    Can someone please help me with a code? Thank you!

    --
    Message posted via AccessMonster.c om


  • Allen Browne

    #2
    Re: Event Code

    See:
    Print the record in the form
    at:
    How to add a button to a form in a Microsoft Access database, so as to print just the active record in the form.


    Note that your table must have a primary key so you can be certain the
    report has the same record.

    --
    Allen Browne - Microsoft MVP. Perth, Western Australia
    Tips for Access users - http://allenbrowne.com/tips.html
    Reply to group, rather than allenbrowne at mvps dot org.

    "ladybug via AccessMonster.c om" <u21071@uwewrot e in message
    news:767016588c ee4@uwe...
    >I have a form called "EmpRpt". It has three fields. First field is
    "chrUserID, " this is a combo box with a row source from another table.
    The
    Second Field is "BeginDate. " The Third field is "EndDate."
    >
    I then have a command button. I want to put a code in this buttons on
    click
    event. The code should open up a report called "EmpRpt."
    >
    The report will have the three fields just like the form. I want those
    three
    fields to appear with the data the user entered in the form. I would also
    like this report to open in print preview.
    >
    Can someone please help me with a code? Thank you!

    Comment

    • Salad

      #3
      Re: Event Code

      ladybug via AccessMonster.c om wrote:
      I have a form called "EmpRpt". It has three fields. First field is
      "chrUserID, " this is a combo box with a row source from another table. The
      Second Field is "BeginDate. " The Third field is "EndDate."
      >
      I then have a command button. I want to put a code in this buttons on click
      event. The code should open up a report called "EmpRpt."
      >
      The report will have the three fields just like the form. I want those three
      fields to appear with the data the user entered in the form. I would also
      like this report to open in print preview.
      >
      Can someone please help me with a code? Thank you!
      >
      You could create a query called EmpRpt. Add the 2 tables (since the
      combo is linked) you use and drag down the fields you want from the 2
      tables. Save the query.

      Go to Reports, click New, and use the ReportWizard. Select the query
      EmpRpt. Follow the steps to create the report. Save it as EmpRpt.

      Now in your command button you open the property sheet to the event tab
      and select the OnCLick event. You could enter
      Docmd.OpenRepor t "EmpRpt",acView Preview

      You can filter it as well. Lets say you have a field called EmpID and
      it is numeric. You could enter
      Docmd.OpenRepor t "EmpRpt",acView Preview,,"EmpID = " & Me.chrUserID
      and this will display the report for the current empid.

      Now it's possible you might open a report where all of the data has not
      been saved. If this were a new record, the report might even say no
      data exists! So sometimes you might want to save the record first
      before displaying a report. Your code might look like this
      If Me.Dirty then Me.Dirty = False
      Docmd.OpenRepor t...
      or
      If Me.Dirty then DoCmd.RunComman d acCmdSaveRecord
      Docmd.OpenRepor t...


      Comment

      • ladybug via AccessMonster.com

        #4
        Re: Event Code

        I did what you gave me and I rcvd an error that I have entered an expression
        that has an invalid reference to the property dirty. I took out that section
        of the code and it opens the report, but no data is captured. Here is the
        code I have:

        Private Sub Command6_Click( )
        Dim strWhere As String

        If Me.NewRecord Then 'Check there is a record to print
        MsgBox "Select a record to print"
        Else
        strWhere = "[chrUserId] = " & Me.[chrUserID]
        DoCmd.OpenRepor t "EmpRpt", acViewPreview, , strWhere
        End If
        End Sub


        Allen Browne wrote:
        >See:
        Print the record in the form
        >at:
        How to add a button to a form in a Microsoft Access database, so as to print just the active record in the form.

        >
        >Note that your table must have a primary key so you can be certain the
        >report has the same record.
        >
        >>I have a form called "EmpRpt". It has three fields. First field is
        >"chrUserID, " this is a combo box with a row source from another table.
        >[quoted text clipped - 11 lines]
        >>
        >Can someone please help me with a code? Thank you!
        --
        Message posted via AccessMonster.c om


        Comment

        • ladybug via AccessMonster.com

          #5
          Re: Event Code

          What two tables? There is no table for the dates they are just text fields.


          Salad wrote:
          >I have a form called "EmpRpt". It has three fields. First field is
          >"chrUserID, " this is a combo box with a row source from another table. The
          >[quoted text clipped - 8 lines]
          >>
          >Can someone please help me with a code? Thank you!
          >
          >You could create a query called EmpRpt. Add the 2 tables (since the
          >combo is linked) you use and drag down the fields you want from the 2
          >tables. Save the query.
          >
          >Go to Reports, click New, and use the ReportWizard. Select the query
          >EmpRpt. Follow the steps to create the report. Save it as EmpRpt.
          >
          >Now in your command button you open the property sheet to the event tab
          >and select the OnCLick event. You could enter
          > Docmd.OpenRepor t "EmpRpt",acView Preview
          >
          >You can filter it as well. Lets say you have a field called EmpID and
          >it is numeric. You could enter
          Docmd.OpenRepor t "EmpRpt",acView Preview,,"EmpID = " & Me.chrUserID
          >and this will display the report for the current empid.
          >
          >Now it's possible you might open a report where all of the data has not
          >been saved. If this were a new record, the report might even say no
          >data exists! So sometimes you might want to save the record first
          >before displaying a report. Your code might look like this
          > If Me.Dirty then Me.Dirty = False
          > Docmd.OpenRepor t...
          >or
          > If Me.Dirty then DoCmd.RunComman d acCmdSaveRecord
          > Docmd.OpenRepor t...
          --
          Message posted via AccessMonster.c om


          Comment

          • Allen Browne

            #6
            Re: Event Code

            If the form does not have a Dirty property, then it is an unbound form.

            If it is an unbound form, then there is no current record to print.

            --
            Allen Browne - Microsoft MVP. Perth, Western Australia
            Tips for Access users - http://allenbrowne.com/tips.html
            Reply to group, rather than allenbrowne at mvps dot org.

            "ladybug via AccessMonster.c om" <u21071@uwewrot e in message
            news:7670a6cdcc 48f@uwe...
            >I did what you gave me and I rcvd an error that I have entered an
            >expression
            that has an invalid reference to the property dirty. I took out that
            section
            of the code and it opens the report, but no data is captured. Here is the
            code I have:
            >
            Private Sub Command6_Click( )
            Dim strWhere As String
            >
            If Me.NewRecord Then 'Check there is a record to print
            MsgBox "Select a record to print"
            Else
            strWhere = "[chrUserId] = " & Me.[chrUserID]
            DoCmd.OpenRepor t "EmpRpt", acViewPreview, , strWhere
            End If
            End Sub
            >
            >
            Allen Browne wrote:
            >>See:
            > Print the record in the form
            >>at:
            > http://allenbrowne.com/casu-15.html
            >>
            >>Note that your table must have a primary key so you can be certain the
            >>report has the same record.
            >>
            >>>I have a form called "EmpRpt". It has three fields. First field is
            >>"chrUserID, " this is a combo box with a row source from another table.
            >>[quoted text clipped - 11 lines]
            >>>
            >>Can someone please help me with a code? Thank you!

            Comment

            • Salad

              #7
              Re: Event Code

              ladybug via AccessMonster.c om wrote:
              What two tables? There is no table for the dates they are just text fields.
              >
              You stated "First field is "chrUserID, " this is a combo box with a row
              source from another table..."

              So I figured 2 tables were in play. Sometimes combos can have multiple
              columns and I figured the data in the combo might be used as well as the
              other fields.
              >
              Salad wrote:
              >
              >>>I have a form called "EmpRpt". It has three fields. First field is
              >>>"chrUserID ," this is a combo box with a row source from another table. The
              >>
              >>[quoted text clipped - 8 lines]
              >>
              >>>Can someone please help me with a code? Thank you!
              >>
              >>You could create a query called EmpRpt. Add the 2 tables (since the
              >>combo is linked) you use and drag down the fields you want from the 2
              >>tables. Save the query.
              >>
              >>Go to Reports, click New, and use the ReportWizard. Select the query
              >>EmpRpt. Follow the steps to create the report. Save it as EmpRpt.
              >>
              >>Now in your command button you open the property sheet to the event tab
              >>and select the OnCLick event. You could enter
              >> Docmd.OpenRepor t "EmpRpt",acView Preview
              >>
              >>You can filter it as well. Lets say you have a field called EmpID and
              >>it is numeric. You could enter
              > Docmd.OpenRepor t "EmpRpt",acView Preview,,"EmpID = " & Me.chrUserID
              >>and this will display the report for the current empid.
              >>
              >>Now it's possible you might open a report where all of the data has not
              >>been saved. If this were a new record, the report might even say no
              >>data exists! So sometimes you might want to save the record first
              >>before displaying a report. Your code might look like this
              >> If Me.Dirty then Me.Dirty = False
              >> Docmd.OpenRepor t...
              >>or
              >> If Me.Dirty then DoCmd.RunComman d acCmdSaveRecord
              >> Docmd.OpenRepor t...
              >
              >

              Comment

              • ladybug via AccessMonster.com

                #8
                Re: Event Code

                So why do I get the error for the Dirty property?

                Allen Browne wrote:
                >If the form does not have a Dirty property, then it is an unbound form.
                >
                >If it is an unbound form, then there is no current record to print.
                >
                >>I did what you gave me and I rcvd an error that I have entered an
                >>expression
                >[quoted text clipped - 27 lines]
                >>>>
                >>>Can someone please help me with a code? Thank you!
                --
                Message posted via AccessMonster.c om


                Comment

                • ladybug via AccessMonster.com

                  #9
                  Re: Event Code

                  The dates do not come from a table. This is just a form which sets the
                  criteria that will be used when opening the report.

                  Salad wrote:
                  >What two tables? There is no table for the dates they are just text fields.
                  >
                  >You stated "First field is "chrUserID, " this is a combo box with a row
                  >source from another table..."
                  >
                  >So I figured 2 tables were in play. Sometimes combos can have multiple
                  >columns and I figured the data in the combo might be used as well as the
                  >other fields.
                  >
                  >>>>I have a form called "EmpRpt". It has three fields. First field is
                  >>>>"chrUserID, " this is a combo box with a row source from another table. The
                  >[quoted text clipped - 28 lines]
                  >>> If Me.Dirty then DoCmd.RunComman d acCmdSaveRecord
                  >>> Docmd.OpenRepor t...
                  --
                  Message posted via AccessMonster.c om


                  Comment

                  • Allen Browne

                    #10
                    Re: Event Code

                    If the error is as you said - invalid reference to the property dirty - it
                    is because your form is unbound.

                    (If the error said you can't set the property, it's because the record
                    cannot be saved.)

                    --
                    Allen Browne - Microsoft MVP. Perth, Western Australia
                    Tips for Access users - http://allenbrowne.com/tips.html
                    Reply to group, rather than allenbrowne at mvps dot org.

                    "ladybug via AccessMonster.c om" <u21071@uwewrot e in message
                    news:7671418c98 553@uwe...
                    So why do I get the error for the Dirty property?
                    >
                    Allen Browne wrote:
                    >>If the form does not have a Dirty property, then it is an unbound form.
                    >>
                    >>If it is an unbound form, then there is no current record to print.
                    >>
                    >>>I did what you gave me and I rcvd an error that I have entered an
                    >>>expression
                    >>[quoted text clipped - 27 lines]
                    >>>>>
                    >>>>Can someone please help me with a code? Thank you!

                    Comment

                    • Salad

                      #11
                      Re: Event Code

                      ladybug via AccessMonster.c om wrote:
                      The dates do not come from a table. This is just a form which sets the
                      criteria that will be used when opening the report.
                      What help do you need?

                      Do you need help in creating a report?

                      Do you need help calling the report?

                      What do you want to print?

                      Allen's doing his best to help, I'm confused with your requirements.
                      You might want to tell us what you have, what you need, whether you are
                      using a table as the form's recordsource or if there is no recordsource.
                      >
                      Salad wrote:
                      >
                      >>>What two tables? There is no table for the dates they are just text fields.
                      >>
                      >>You stated "First field is "chrUserID, " this is a combo box with a row
                      >>source from another table..."
                      >>
                      >>So I figured 2 tables were in play. Sometimes combos can have multiple
                      >>columns and I figured the data in the combo might be used as well as the
                      >>other fields.
                      >>
                      >>
                      >>>>>I have a form called "EmpRpt". It has three fields. First field is
                      >>>>>"chrUserID ," this is a combo box with a row source from another table. The
                      >>
                      >>[quoted text clipped - 28 lines]
                      >>
                      >>>> If Me.Dirty then DoCmd.RunComman d acCmdSaveRecord
                      >>>> Docmd.OpenRepor t...
                      >
                      >

                      Comment

                      • Bob Quintal

                        #12
                        Re: Event Code

                        "ladybug via AccessMonster.c om" <u21071@uwewrot e in
                        news:76714e6932 a13@uwe:
                        The dates do not come from a table. This is just a form which
                        sets the criteria that will be used when opening the report.
                        Your statement that the form has three fields is what leads
                        people astray. Forms do NOT have fields. Tables have fields.
                        The process of binding a form to a table or query means that the
                        fields appear in the textboxes. Had you said there are three
                        textboxes on the form, you would have been clear.

                        As to your problem of the report not opening, your data talks of
                        chrUserID. is this really a text field, or an ID Number field?
                        If it's a text field, you need to modify the way you pass the
                        info to the form
                        strWhere = "[chrUserId] = " & Me.[chrUserID] will work for a
                        numeric field.
                        strWhere = "[chrUserId] = '" & Me.[chrUserID] & "'" is
                        required for a text type ID.


                        >
                        Salad wrote:
                        >>What two tables? There is no table for the dates they are
                        >>just text fields.
                        >>
                        >>You stated "First field is "chrUserID, " this is a combo box
                        >>with a row source from another table..."
                        >>
                        >>So I figured 2 tables were in play. Sometimes combos can have
                        >>multiple columns and I figured the data in the combo might be
                        >>used as well as the other fields.
                        >>
                        >>>>>I have a form called "EmpRpt". It has three fields. First
                        >>>>>field is "chrUserID, " this is a combo box with a row source
                        >>>>>from another table. The
                        >>[quoted text clipped - 28 lines]
                        >>> If Me.Dirty then DoCmd.RunComman d acCmdSaveRecord
                        >>> Docmd.OpenRepor t...
                        >


                        --
                        Bob Quintal

                        PA is y I've altered my email address.

                        --
                        Posted via a free Usenet account from http://www.teranews.com

                        Comment

                        Working...