How to dynamically populate a report? - database

Hopefully this is the last question I have about Access since I will be done working with it very soon.
I currently have a report that is generated from a form that is filled out when a "Run" button is pressed. A database primary key is searched for and the form is populated. When the user clicks a "Print" button, a printable report is generated, being populated by data from that form. All of that was easy.
Now, in the form, there is a single list box. That list box displays results for a transaction type depending on which transaction type is clicked. For example, clicking "Purchase" will yield all customers who have purchased something in the list box, and clicking "Sold" will yield all customers who have been sold something in the list box. There are many more transactions than those two, but that does not matter for the purpose of this question.
What I need to do is make the contents of that list box (which is populated by a query in VBA) for every transaction type appear on the printable report.
So for example, the report might look something like this:
Purchases:
John
Cindy
Alex
Sold:
Matt
Steven
John
Rob
Does anyone know a simple way to do this? I can vaguely think of how I might do this, but it is complex and I don't know the exact functions I would need to use. Basically I was thinking I would run the query that populates the list box, but then parse through each result in a while loop, and display it on the report.
Thank you in advance for any help!

A Report is a fairly static object in comparison to a Form. To dynamically change the data that is displayed in a report you would typically use the Where argument for OpenReport, usually on a button press and reading some value from a control on the currently active form:
DoCmd.OpenReport "rptName", acViewPreview, , "SomeField=" & frmName!ctlName
If the value on the form is a string then it needs to be surrounded with apostrophes:
DoCmd.OpenReport "rptName", acViewPreview, , "SomeField='" & frmName!ctlName & "'"
A report also has a Filter property that you might use, or you can programmatically set its RecordSource. The first option is the most common, and the easiest.
If the report has a listbox then you would typically modify its RowSource, using a query with a criteria that refers to a control on your form. But I would question why this is necessary, as the listbox would have no relation to the rest of the data in the report.
If you are talking about dynamically changing what appears on the report - moving and hiding controls, etc. - then this is a different question. You would need to first open the report in Design View. This is unusual and rarely necessary. Typically, you would create a separate report, perhaps copying the original report, and just use the button to decide which report to display. (There are Report Events that can be used to hide, or move, controls on the report - OnFormat, for example - that wouldn't require the report to be opened in Design View, but I don't think this is what you are looking for.)

Assuming that the report will be created using the recordset that was used to create the form, you could join the recordset that populates the listbox to the recordset that populates the rest of the form. If you, say, LEFT JOIN the recordset for the list box to the recordset for the form on the transaction type, the resulting recordset should have every thing you need to create the report required.
If the report needs to chabnge its format based on the transaction type, try using the OnOpen and OnClose Events for the report and the OnFormat Event for the Detail sections. I have some experience with reports like that. If you need more help with the events, add comments with some specific detail and I can probably make useful suggestions.

Related

Running VB when a row is clicked on a datasheet subform in Microsoft Access 2013

So I'm creating a basic database in Microsoft Access 2013. I have a ParentForm, called formLanding, and multiple subforms within that ParentForm. Basically, what I want to happen is when a user clicks on a row within one of the subform datasheets (which display tables like Products and Users), I want to be able to display more information about whatever they clicked on.
Let's say I have subform datasheets called Products, Users, and Locations. If a user were to click on a row within Products, a box/form to the side would show all Users and Locations of the Product. If a user were to click on a row within Locations, the same box would show all Products in that Location.
The reason I want to do this via VB is because I think it'll be easier. I know how to write SQL and VB, but I don't understand Access very well. Rather than trying to link these multiple objects together using their interface I think it'd just be easier to work with VB. I'd also like to be able to run multiple queries sequentially and with parameters, something I haven't figured out how to do in Access yet.
How do I do this? How do I call VB when a user clicks on a row, have my VB queries run and return relevant information based on what they clicked on, and then send that information to another object and have it display it? Is Access built to handle this sort of thing?
Thanks.
EDIT: I wanted to add that the data details I want to display when a row is clicked could include data from multiple tables and return multiple rows. For instance, a Product could have 5 users and 2 locations. I would like to display that information in the same form/report.
You would do this by linking the sub forms to the parent form using the Child / Master properties in the SubForm container.
Assuming you can define a relationship between a Control(Field) in the Parent form and the Data in the sub forms, Access will perform this task automatically.
If you want to Link multiple forms you may need to use the OnCurrent event of the sub forms to change a unbound textbox value on the parent form to force a cascade update of the other subforms.
I can't answer all of your topics, but this string usually works for running more code when certain cells or rows are selected/edited.
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("O2:O2")) Is Nothing Then
Exit Sub
Else
'Do the things
Or reference a specific sheet and range of cells with
Worksheets("Sheet1").Range("R17:R55")

How to quickly populate a combobox with database data (VCL)

I have read all sorts of documents across the web about what should be a farly common and painless implementation. Since I have found no consistent and slick reply (even the Embarcadero website describes some properties wrong!) I am going to post a "short" howto.
Typical, frequent use case: the developer just wants to painlessy show a couple of database-extracted information in a combo box (i.e. a language selection), get the user's choice out of it and that's it.
Requirements:
Delphi, more or less any version. VCL is covered.
A database table. Let's assume a simple table with an id and value fields.
A DataSet (including queries and ClientDataSets).
A DataSource linked to the DataSet.
A TDBLookupComboBox linked to the DataSource that shall show a list of values and "return" the currently selected id.
Firstly, we decide whether the sort order is the one we want or not and if all the items in that table must be shown. Most often, a simple ORDER BY clause or a DataSet index will be enough. In case it's not, we may add a sort_order field and fill it with integers representing our custom sort order. In case we want to show just some items, we add a visible or enabled field and use it in our SQL. Example:
SELECT id, value
FROM my_database_table
WHERE visible = 1
ORDER BY sort_order
I have defined visible as INTEGER and checking it against 1 and not TRUE because many databases (including the most used SQLite) have little support for booleans.
Then an optional but surprisingly often nice idea: temporarily add a TDBGrid on the form, link it to the same TDataSource of the TLookupComboBox and check that you actually see the wanted data populate it. In fact it's easy to typo something in a query (assuming you are using a SQL DataSet) and get no data and then you are left wondering why the TDBLookupComboBox won't fill in.
Once seen the data correctly show up, remove the grid.
Another sensible idea is to use ClientDataSets for these kinds of implementations: due to how they work, they will "cache" the few rows contained in your look ups at program startup and then no further database access (and slowdown and traffic) will be required.
Now open the TDBLookupComboBox properties and fill in only the following ones:
ListSource (and not DataSource): set it to the TDataSource connected to the DataSet you want to show values of.
ListField: set it to the field name that you want the user to see. In our demo's case it'd be the value field.
KeyField: set it to the field name whose value you want the program to return you. In our demo it'd be the id field.
Don't forget to check the TabOrder property, there are still people who love navigating through the controls by pressing the TAB key and nothing is more annoying than seeing the selection hopping around randomly as your ComboBox was placed last on the form despite graphically showing second!
If all you need is to show a form and read the TDBLookupComboBox selected value when the user presses a button, you are pretty much sorted.
All you'll have to do in the button's OnClick event handler will be to read the combo box value with this code:
SelectedId := MyCombo.KeyValue;
Where SelectedId is any variable where to store the returned value and MyCombo of course is the name of your TDBLookupComboBox. Notice how KeyValue will not contain the text the user sees onscreen but the value of the id field we specified in KeyField. So, if our user selected database row was:
id= 5
value= 'MyText'
MyCombo.KeyValue shall contain 5.
But what if you need to dynamically update stuff on the form, depending un the combo box user selection? There's no OnChange event available for our TDBLookupComboBox! Therefore, if you need to dynamically update stuff around basing on the combo box choices, you apparently cannot. You may try the various "OnExit" etc. events but all of them have serious drawbacks or side effects.
One possible solution is to create a new component inheriting from TDBLookupComboBox whose only task is to make public the hidden OnChange event. But that's overkill, isn't it?
There's another way: go to the DataSet your TDBLookupComboBox is tied to (through a DataSource). Open its events and double click on its OnAfterScroll event.
In there you may simulate an OnChange event pretty well.
For the sake of demonstration, add one integer variable and a TEdit box to the form and call them: SelectedId and EditValue.
procedure TMyForm.MyDataSetAfterScroll(DataSet: TDataSet);
var
SelectedId : integer;
begin
SelectedId := MyDataSet.FieldByName('id').AsInteger;
EditValue.Text := MyDataSet.FieldByName('value').AsString;
end;
That's it: you may replace those two demo lines with your own procedure calls and whatever else you might need to dynamically update your form basing on the user's choices in your combo box.
A little warning: using the DataSet OnAfterScroll has one drawback as well: the event is called more often than you'd think. In example, it may be called when the dataset is opened but also be called more than once during records navigation. Therefore your code must deal with being called more frequently than needed.
At this point you might rub your hands and think your job is done!
Not at all! Just create a short demo application implementing all the above and you'll notice it lacks of an important finishing touch: when it starts, the combo box has an annoying "blank" default choice. That is, even if your database holds say 4 choices, the form with show an empty combo box selected value at first.
How to make one of your choices automatically appear "pre-selected" in the combo box as you and your users expect to?
Simple!
Just assign a value to the KeyValue property we already described above.
That is, on the OnFormCreate or other suitable event, just hard-code a choice like in example:
MyCombo.KeyValue := DefaultId;
For example, by using the sample database row posted above, you'd write:
MyCombo.KeyValue := 5;
and the combo box will display: "MyText" as pre-selected option when the user opens its form.
Of course you may prefer more elegant and involved ways to set a default key than hard-coding its default value. In example you might want to show the first alphabetically ordered textual description contained in your database table or whatever other criterium. But the basic mechanic is the one shown above: obtain the key / id value in any manner you want and then assign it to the KeyValue property.
Thank your for reading this HowTo till the end!

Drilldown Reports

Help
I am attempting to create a drill down report using report server and Visual Studio 2005.
I have been able to create a tree structure, but the original report looks like it opened a new report. (Crystal Report)
This was done by the report hiding all other data apart from the drilled data and the data title.
I can’t seem to be able to do this at the moment or seem to find any help online to do what is needed.
Does anyone know how I can do this or know of any material online that could help?
Thanks
i can think of two different ways to interpret your question. 1. you want a table in a report that, when the user clicks on a specific row, drills down into the report. 2. you want a report that, when a user clicks on certain rows, it opens up another report that's a drill down of the data. for option one, read my blog here and tell me if that's what you're looking for.
Not sure which way you're asking, so I'll list both.
For all your data on a single report which is grouped you can right click on the row indicator on the table and select properties, then visibility. Inside visibility you should see a checkbox with "Display can be toggled by this item" then the dropdown will have a list of textboxes that are on the report. Just select the one you want to control the drilldown.
The alternative way is through report actions, which you create a separate report with or without parameters, and select the textbox and choose properties, goto action, select "Goto Report" then select the report to drill down to, and add your parameters.
Hope this helps.

How do I save data created in a form in Access 2003 when I reference other tables?

I'm not sure how to fix this issue, but when I utilize combo boxes on my form which selects data from multiple tables I'm not able to save any of the data selected.
I tried to use the Bound form utility but it only asked me to "Enter Parameter Value". I took out the "Bound" portion of the code and the combo box works fine.
I have a nice little save button, but it doesn't actually save anywhere.
I tried using a query, but that didn't fix it either unless, of course, I didn't do the query correctly.
Here is the "Row Source" information...
SELECT SubArea.SubAreaID, SubArea.SubAreaName, SubArea.ProductName FROM SubArea WHERE (((SubArea.ProductName)=Forms![Combo Form]!Product));
Do I need to put something in VBA or perhaps a Macro to have all the data selected to actually save into a table? HELP!
For the Bound Form
Open the form in design mode, right click on the combo box and go to properties. Check out if it is actually bound to something in the Control Source field.

Access 2007, Textbox search box like the Facebook name search box on the top right

so basically I have an AddCompany Form, where theres a textbox [CompanyName], i want to type a new company name in there, but meanwhile check if theres an already existing one.
So for example,say i want to type Microsoft, when i type M, the textbox shows whole bunch of other names with M, and then keeps going until I type finish typing microsoft. Basically jsut how the facebook search box works.
How do i implement this on Microsoft Access 2007?? could it be in the on dirty event/on change/On key down event???
please enlighten me!!
Thank you very much!!!
A much simpler version of the same thing can be done with the Access wizards. If you turn on the form wizards in form design and click the Combo Box button and point to a location in the header of your form, you'll automatically get a choice to create a combo box that will look up a record that matches something listed in the dropdown list.
However, keep these things in mind:
it works only when you've bound your form to an entire table (which is not recommended for large recordsets)
the code it creates is horrendously BAD.
There are a number of ways to solve this problem. If you're happy binding your form to the entire table, bookmark navigation (as with the code created by the wizard) will be fine. However, I recommend you use this code instead of the wizard code (assumes the combo box has a bound column with the PK ID of the record you're trying to find):
Private Sub MyComboBox_AfterUpdate()
If IsNull(Me!MyComboBox) Then Exit Sub
With Me.RecordsetClone
.FindFirst "[MyID]=" & Me!MyCombBox
If Not .NoMatch Then
If Me.Dirty Then Me.Dirty = False
Me.Bookmark = .Bookmark
End If
End With
End Sub
The combo box would need to use a SQL rowsource, and it would be something like:
SELECT CompanyID, CompanyName FROM Company ORDER BY CompanyName
And you'd define the combo box to have 2 columns, the first one the bound column, and you'd set the width for the first column to 0. The wizard walk you through doing this and set it up for you. The only thing it does wrong is write really bad code to do so.
Now, if you're not searching for a unique value, it gets more complicated, and you might want to use a different approach. Suppose you have a form that displays people and you want to see the ones for a particular company. In that case, you might want to filter by CompanyName. In that case, instead of doing a Find operation as outlined above, you might want to apply a filter. In that case the AfterUpdate event of your combo box would be something like this:
Private Sub MyComboBox_AfterUpdate()
If IsNull(Me!MyComboBox) Then Exit Sub
Me.Filter = "[CompanyName]=" & Chr(34) & Me!MyComboBox & Chr(34)
Me.FilterOn = True
End Sub
Now, in that case, your combo box would have but one column, with the company name and no hidden ID field, so it's somewhat different.
I don't like programming this kind of filtering as it's provided by the Access UI (you can right click on the CompanyName field and in the shortcut menu that pops up, type in the company name you want to filter for, including wildcards, ? and *). Also, it can get confusing when you try to filter when there's already a filter in place. If you're viewing just CitiCorp and then you filter by JP Morgan Chase, you won't get anything.
Instead, I'll tend to change the Recordsource of the form instead of applying a filter. This means each search will give you a new resultset and is the most efficient approach. It does make things a little bit more complicated in terms of returning to your original starting point, but in a properly-designed app, you should probably be presenting only subsets of data to the users in the first place, so the problem this approach "causes" is one that you'd need to resolve to build a proper and efficient user interface that retrieves only the data that the user really needs.
In general, this feature is called auto-completion (I've also heard find-as-you-type). There is an article giving an example of how to do this in Access on this page.
The forms in Access have event handler functions. So couldn't you use autocompletion code added to these functions for this functionality? I found a book on Google Books which discusses this.
I can think of the following code which would need to be added to the event handler function:
1. SQL to find existing potential matches
2. Code generated drop-down box to display potential matches

Resources