dynamic reports from form in MS Access - dynamic

I'm trying to generate a report off of a form with combo/list boxes that will filter the report parameters. I'm doing this for several forms/reports, but generally need the same thing for all.
For example, I have a form for my programs which has a combo box "cboYear" to select a year, based off of [progdate] and formatted as "yyyy" so the user can select what year they want their reports from. But, I also have a cascaded combo box "cboProgramTitle" that is based off of the [progdate] box, and only shows programs selected for the "cboYear". The code below will run the report and filter if something is selected for "cboProgramTitle", or returns all if left blank. How do I also get it to return all programs for the "cboyear", since this is already a 'formatted' version of [progdate], if a year is selected, but "cboProgramTitle" is left blank?
[CODE]Private Sub cmdAttendees_Click()
If IsNull(Me.cboProgramTitle) Then
DoCmd.OpenReport "rptProgramAttendees", acViewReport
Else
DoCmd.OpenReport "rptProgramAttendees", acViewReport, , "ProgramIDFK = " & cboProgramTitle & ""
End If
End Sub[/CODE]
I've run into this same problem with similar forms/reports, too. Need help filtering reports off multiple combo/list boxes, with one of them being the year list, or others being month or quarter lists.
I'm what I call a 'novice frankensteiner,' in that I can piecemeal code together, but can't really write it from scratch. :)
Thanks in advance!

Related

How to update multiple tables with one Update statement in Microsoft Access (using VBA coding and SQL)

Here is my setup: I have made a Parts Order Form the contains combo boxes labeled P1-P10 (part) with resource data taken from my table [Inventory C] showing OEM# (key) and Description. The other combo boxes, containing numbers, are named QO1-QO10 respectively (for quantities ordered). On selecting/typing an entry for the individual combo boxes the code that I have in a test button updates the record that matches the part # in the combo box (P1 for example) and the corresponding selected number from the number box (QO1 for example) and updates the [USED QTY] column of my table to add the selected number the corresponding part.
Public Sub TestButton_Click()
Dim SQL As String
SQL= "UPDATE [INVENTORY C]" & _
"SET [QTY USED]=[QTY USED]-(QO1.Value)" & _
"WHERE [OEM#] = P1.Value"
DoCmd.RunSQL SQL
End
End Sub
This code runs fine, but I have more than those two combo boxes. Currently I have to redo this same statement nine more times within the same code segment for them all to be updated with a click. During the test after clicking the Test Button, a message will alert you that you are changing a row on your table and to press OK to continue (good!) but it does it ten times (not so good... also there is an error if you press NO so held with an error handler would be appreciated). In the form I have it set up almost like an Excel worksheet, and my users will be using multiple boxes to input information; I will embed the code into a print function so that when they press print, it will also ask them to update all records entered ONE TIME (so that on click it asks: "You are about to Update (10) records. Any changes..."). How can I code this in VBA, using SQL to UPDATE MULTIPLE RECORDS in the SAME CODE SEGMENT Can I use a switch or case statement?? This help would be much appreciated.

How to select table values from combobox selection?

I'm using an Access 2003 database and have 2 comboboxes I am trying to work with. The first box I have perfected already, which is a dropdown of different tables (categories of parts). Once that table is selected, I want to be able to look at the part numbers within that category through a dropdown box selection. From here I want to be able to pull up the correct report for that category with that part number in it so I can print a report for every part number. I'm sure I'll have to write some sort of VBA, Query or Macro AfterUpdate() code, but I just don't know how to fill that second combobox with the selected table's part numbers.
Click here for an image of my Menu layout
Here's my Query for the first box to show the tables I want:
SELECT Msysobjects.Name
FROM Msysobjects
WHERE (((Msysobjects.Name) not Like "MSYS*"
And (Msysobjects.Name) not like "_*"
And (Msysobjects.Name) not like "~*"
) AND ((Msysobjects.Type)=1))
ORDER BY Msysobjects.Name;
And I think this is what I'll need to print after the second box has it's selection:
Private Sub partnumberselect_AfterUpdate()
DoCmd.OpenTable Forms![_Datasheet Printing].Form.TagLabelSelection.Column(1), acViewNormal
End Sub
Thank you in advance and let me know if you have any questions.
You are attempting what are called "cascading comboboxes" which means the second box is dependent on the selection of the first.
This is accomplished through the control source of the second combo box.
The first thing you should do is write a query that returns all possible options of the second combobox, without caring so much about filtering it based on the first combo selection. Once you have it returning the correct data, you will add a WHERE clause to the second box's control source that's something like:
WHERE Msysobjects.Name Like Forms![_Datasheet Printing]!TagLabelSelection.Value
This is referencing your first combobox on your form. So after a selection is made in the first combobox, the underlying control source of the second will have the proper criteria to return the appropriate options.
However, you will need to add some VBA to the AfterUpdate() event on the first combobox. Once the selection is made, you need the second box to refresh the control source to populate the correct selections. The code is simply:
Forms![_Datasheet Printing]![MySecondComboboxName].Requery
Please see the example below.
Private Sub cboCountry_AfterUpdate()
On Error Resume Next
cboCity.RowSource = "Select tblAll.City " & _
"FROM tblAll " & _
"WHERE tblAll.Country = '" & cboCountry.Value & "' " & _
"ORDER BY tblAll.City;"
End Sub
You can read all about this concept, and others, in the link below.
http://www.fontstuff.com/access/acctut10.htm

DoCmd.OpenReport Asks for values Access07

I have a form that opens a report based on a combo box selection.
Which looks like.
The invoice shipment button opens the report via
DoCmd.OpenReport "ItemList4", acViewReport, , "ShipRef = " & Me.SRCB
SRCB is the combo box next to the shipment label.
When clicking the invoice shipment button I always get asked what the parameter value for S100018 is, so obviously it knows what the value is but isn't applying it to the filter when opening the report like so
How do I prevent this from happening?
I always create a query with a where clause that refers back to the textbox in the form. Then I build the report on the query, which selects 1 record, and I get my report.
I'l show a quick demo, here is a table named Persons:
Then create a form with a textbox, I named the form PersonForm:
Now creat a query that selects everything from the Persons table. In the where clause, open the builder, browse to the created form and select the textbox.
Then we create a very simple report with the report wizard, based on the query.
Now we go back to our created form and add a button. In the button, select for the option to open a report.
Now if you open the form in Form view, enter the name Ivo in my instance. And then click the button.
Of course you have to adjust the example to your context.
Create a query with the data that you need to generate the report. Then add a where clause in the query to the list box where you display your shipment id. Then let the report get the data from the query.
The problem seems to be the report. It looks like it's accesing a variable called S100018. Is this intended behaviour?
The problem was that it was not sending the filter argument as a string
it should be as such
DoCmd.OpenReport "ItemList4", acViewReport, , "ShipRef = '" & Me.SRCB & "'"

Creating a form where user inputs start and end dates of a report

I am working on MS Access 2007.
I am trying to create a form where the user will input 2 dates - a start date and an end date. I want these dates to become the boundaries of a report.
For example if the user inputs 01/03/14 and 10/03/14, I want Access to create a report of the data (eg Selling Price) between these two dates.
How can I do this?
There are a few ways to perform this.
The easiest, and most straight forward, in my personal opinion, involve creating a form where the user(s) will be entering Start/End dates, using a query that captures all of the information necessary for your report (including your date parameters), and the report with your query as the recordsource.
Form
Create a form with 2 text box controls: Name them whatever you like (StartDateTxt, EndDateTxt, for example), and a button control.
Query
Now, create a query, that retrieves all of the correct information you need for this report. I know you said selling price, for starters.
For the date fields you want to narrow down your search by, you need to put them in the WHERE clause or the Criteria field in QBE. It might look something like:
[Forms]![YourFormName]![StartDateTxt] and [Forms]![YourFormName]![EndDateTxt]
Note: Referencing fields can sometimes be tricky.
Report
Next, you need to create a report. If you're new to this, you can use the Report Wizard located at: Create - Reports section - Report Wizard.
You can select your query from the dropdown list on the first prompt, and add the fields you desire/need. Click next: Here you can sort your order of appearance on the report. Click next: Then you can justify your layout - if you aren't familiar with the differences, you can play around with them. Click next: Select a 'theme' to be applied to your report (font/colors, etc). Click next: Then you can either preview the report or Modify the report's design. (Really, you can do either, by clicking either button and clicking print preview or design view.)
If you click on Preview the report - Access should ask you for any parameters your underlying query requires.
Back to the form.
Right click on your button control. -> Properties -> Event -> OnClick -> click the [...] icon to the very right of the row. Select code builder.
VBA
In-between Private Sub and End Sub, you're going to write
DoCmd.OpenReport "YourReportName"
You can also check to make sure your report isn't already open.
Dim ReportName As String
ReportName = "YourReportName"
If CurrentProject.AllReports(ReportName).IsLoaded Then
DoCmd.Close acReport, ReportName
End If
DoCmd.OpenReport ReportName, acViewPreview
I hope this helps you/answers your question.
Edit: If anyone wants to edit/add on to this either by means of clarification or another alternative approach, please feel free. Just let me know.

Populate Text box on access form based on the value of combo box on the same form [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
MSAccess - populate text box with value from query
I have a single form on which there exists several combo boxes and text box. one combo box value (for the Wells) will be filled independently, then I needs the text box to have its value based on the value of the wells combo box value.
I have created a query that solved the propblem partially, it requires parameter which the wells combo box value. If I ran with query a part from the form, it wroks good and asks for the parameter and it is OK.
I do think to make use of VBA, adding a code which process a SELECT statement (of the query mentioned above), then to tell it to take its parameter from the wells combo value which will ready on the form.
Can someone help on this. Can this works as I descriped.
Thanks in advance.
Mohamed
Further to my question above, I have tried the following solution:
Private Sub Well_ID_Change()
Last_Ref.ControlSource = " SELECT TOP1 New_Ref FROM" & _
" BSW_Transactions WHERE BSW_Transactions.New_Ref Is Not Null AND BSW_Transactions.Well_ID = " & Me.Well_ID.Value & _
" ORDER BY BSW_Transactions.Sample_Date DESC"
End Sub
the Last_Ref is the text box I want to fill in with result of the embedded SELECT statement in the code. The Well_ID is the combo box which value will be the parameter of the SELECT statement. The Well_ID is number field and it displays the well_name and stores the associated ID value in the table. Upon running the form after saving changes, the Last_Ref text box showed (#Name?). I guessed that the text box (is a number field) found a text in the combo box Well_ID, so I added ".Value" to the above syntax at the criteria Me.Well_ID. However the problem still exists.
May I mistaken in the syntax, would someone help on this. Can this works fine?
Thanks in advance.
Mohamed
You are doing this the wrong way around. Bind the form to a table, the wizards will do it for you, then add the combobox. Choose "Find a record on my form" from the wizard. The code or macro to do this will be generated. Once this is done, selecting a record in the combo will populate the form with the data for that record.
Try this in the AfterUpdate event of your combo box:
Private Sub MyTextBox_AfterUpdate()
MyTextBox.ControlSource = DLookup("WellsField", "WellsTable", "CriteriaField = '" & [MyComboBox] & "'")
MyTextBox.Requery
End Sub
This will tell your TextBox to lookup the value in your Wells table and the Requery ensures the value is up to date every time your combo box is updated. Note the ' in the criteria part of DLookup. These ' are only required for String values.