Search code examples
ms-access

Is there a way to use buttons in MS Access to fill in text boxes in a report?


Here's the crux of the question, I have 26 compliance queries to run, in a previous question it was suggested that I should filter a single query, or two, on a single report. I like this idea, and have rewritten the query to pull all available data from all the fields, this query works fine. The report will work fine as well, as it does with a model query that I had coded up beforehand. What I would like to do is this:

The end user is being given an interface in access that is locked down, I want them to click a button, and that button will run the query and send to the text box just the field that is called for.

I have tried doing this through VB using the where clause and aliasing the column being called, this did not work at all. I have the report currently pulling the correct data, but not displaying the dates along side it. But it is filtering correctly aside from that.

So what needs to happen is this : Button click : Query runs, and is filtered for "Compliance Issue 1" and puts the dates in "Compliance Issue 1" in the text box on the report.

Right now... I get a list of names, the correct list of names, but an empty column.

I have tried using OpenArgs, but all it did was fill in the date column with "Compliance Issue 1" not the actual data in that column.

Is what I am trying to do even possible in access, and if so does someone have a reference or suggested starting point.

My background : 6 Months of python coding, 3 months of SQL , and some limited access from 20 years ago.


Solution

  • As noted, using the filter of the openreport is without question the way to go (one would not write a whole bunch of different queries - you can send/have any filter for that report - you can EVEN use a sub query in the filter that you send to the report.

    As for displaying values in the report that are not from "rows" of data?

    There are two approaches that work quite well.

    First up, is you have that launcher form. This of course allows the user to select critrea - maybe even some nice combob boxes. These selections take care and you build up the filter in code that you pass to the report.

    As for text boxes to be filled out from that form and inclluded in the report?

    If they are static values from the report (say filter options, or even just a notes box that you could type in some text? To display such values in textboxes on the report?

    You can directly set the text box data source (in the designer) to the report propter form like this:

    =(forms!MyPromptForm!notes)
    

    So, any value you shove into text boxes on the report prompt form can thus be displayed in any text box on the report with the above type of expression. And it does not even take code to achieve this goal. So, you could say with above enter some notes into that text box, and thus on the report, whatever you typed into that text box will now show up in the report. You just drop in a text box onto the report, and set the data source of the text box to the above expression that references the form with the values we want from that form.

    The next approach, and I often use this in the case that some value/expression/calculation has to occur for each row. In this case, you can use the reports on-detail format event. This allows you to run code for EACH row of data.

    You are free to run ANY code in that event - and that includes after running such code to set a text box in the reports detail section.

    So, say the query only had the Hotel ID (PK). This is a lame example, but you could then write this code in the on-format event of the reports detail section.

    dim strSQL     as string
    dim rst        as DAO.RecordSet
    
    strSQL = "SELECT HotelName from tblHotels where ID = " & me.HotelID
    set rst = CurrentDb.OpenRecordSet(strSQL)
    me.HotelName = rst!HotelName
    rst.Close
    

    So in above, we assume that a row text box is called HotelID, and then in code we build a whole sql query from scratch, pulled the row data from a table, and then SHOVE/SET the value of the un-bound text box called hotelName.

    As noted, the above is a simple example, but we are free to run any code we want, pull any data we want, and set the value of ANY text box for the given detail section ONE row of values.

    So, above shows two approaches. The first approach is code free - and you can put forms! expression directly into the report, and the values from that report prompt form will thus show up directly in the report. However, if you need VBA code to run for each row, pull values, walk the dog, and THEN set a text box on that one details row of data, then you are as above shows free to write procedural code in the report that fires + runs for each row of data - and that means you can quite much do anything you want in terms of running code. I mean, even that on detail format event COULD pull values from your report prompt form, but as the 1st example shows, you can shove in forms! expression directly into a text box - and those forms! expressions can be values from a existing form that is open before the report is launched.