Macro to print a specific report
Attachment | Size |
---|---|
G&G_CE2.odb | 98.57 KB |
It’s a good question. There are quite a few ways to do what you want, for example use a button-attached MACRO as in THIS EXAMPLE. But to keep it simpler, maybe you might be able to just switch to a custom form layout (using a button) and print the selected record. The customer form would be formatted with a white background and suitable text layout to suit your printout format.
But, isn’t that what you are trying to do with your ‘Stampa’ (Print) button? What’s wrong with that method?
If you want to generate a report for several records (you have been working on), then you will need to FLAG them in your database TABLE, and then generate a REPORT with a QUERY that SELECTS all your flagged (marked) records as the data-source. I have successfully done what you seem to be wanting by adding a field/column called ‘batch’ which holds an integer flagging the batch mumber of the set of records you need to print out (report). That aforementioned QUERY selects all the records with the appropriate BATCH NUMBER as the source for the report. NOTE: The good thing about this kind of approach is that you have a permanent record of all the records you printed out for each batch (and can re-print them if necessary).
Press the button of the form always open, regardless of the record in which you work, the 1st record of the report. I wish they would open hand on the record corresponding to the form in which I work.
I notice your report has as its data-source your query Scheda Preventivi which, in your sample database, returns 4 records. So when you click on the Stampa button in your form Preventivo_Scheda, the MACRO runs the report Scheda Preventivi containing the 4 records just referred to. That’s what you would expect.
If you wanted your report to ONLY show the current record as displayed by the form (i.e. only ONE record), you would need to replace your Scheda Preventivi with one that only returns a SINGLE record/row (the ‘current’ record displayed by your form). Somehow your macro must ‘tell’ the query which is the currently-selected record. It does not do that at the moment (it just runs the report).
Possibly the macro you use to run the the report might be able to be modified to get the ID details of the current record and pass to a modified Scheda Preventivi query. Not being a macro expert, I can’t help you much with this technical aspect, but maybe one of our macro-savvy contributors can help here.
Another possibility would be to flag your current record, and re-write your Scheda Preventivi query to select ONLY this flagged record. Maybe your macro could be re-written to write the ID of the current record to a one-row FILTER table, and the report data-source query could then use this information to ONLY show the current record.
These are just ideas.
I’m still not sure Base is the answer for you. If all you are trying to accomplish is to print an estimate, then using a template in Writer may be a better choice. There are many available on the Internet.
Your example leads one to believe you have little or no experience with databases. Base is just an interface to send and/or receive data stored in a database (in your case embedded HSQLDB). Writer, Calc, Report Builder and many other applications can access data from a database, format the information and then print it provided a mechanism is in place to disperse exactly what is desired.
What you are attempting to do can be done, but it starts with a database design. You have an address table but where is the patient address stored? How will the name be stored – First/Last Name – what about duplicates? And the procedures, how are they saved and priced. Where are the relations between tables? These items are just the tip of the iceberg.
Again, you have not disclosed any information other than wanting to print what is on the form (use a template in Writer).
I am happy to help get you going in the right direction but at this point I’m not sure what your destination is.
Thank you for some much needed information.
Although not the best approach I have made a few minor changes to accommodate your wishes. There are no macros involved.
Your problem has to do with what information is presented for printing.
1 – On form Preventivo_Scheda there was no assurance the record displayed was saved. A navigation bar was added for this. The Stampa checkbox was set to Tristate = Yes this was changed to No and Input required was changed from Yes to No.
2 – Query Scheda Preventivi was changed only to add Stampa flag – select record if ON.
OK. With only those changes, if you want to print the record displayed on Preventivo_Scheda, turn on Stampa, save the record and then run report SchedaPreventivi. Correct record appears.
The way this is set up, only one record may be printed at a time – the first one with a Stampa ON. After printing, go back and turn Stampa OFF, then save the record. Now you can set another record for printing.
Some points which may be of interest –
All of the Rubica information displayed on the form is not necessary to accomplish this task. That information is gathered in the Query.
A better way to do this would be to allow multiple records with Stampa On, use Report Builder to print all records, then clear the Stampa On flag with a SQL statement.
Your code to open/close forms is repetitive and has some unnecessary lines. This can be changed as follows:
Replace this:
Sub ApriPrimaNota Dim InteractionHandler As Object InteractionHandler = createUnoService("com.sun.star.sdb.InteractionHandler") ThisDatabaseDocument.CurrentController.connect("","") ThisDatabaseDocument.FormDocuments.getByName ( "PrimaNota" ).open ' ( "Nome del formulario da aprire" ) ThisComponent.CurrentController.Frame.close(True) ' Chiude il formulario corrente End Sub
With This:
Sub ApriPrimaNota FormChange("PrimaNota") End Sub
Which Calls This routine:
SUB FormChange( sFormName )
Dim ObjTypeWhat Dim ObjName As String Dim sName as String Dim sTitle As String Dim iStart As Integer sTitle = ThisComponent.Title iStart = Instr(sTitle,":") + 2 sName = Mid(sTitle, iStart) ObjName = sFormName ThisDatabaseDocument.FormDocuments.getbyname( sName ).close ObjTypeWhat = com.sun.star.sdb.application.DatabaseObject.FORM If ThisDatabaseDocument.FormDocuments.hasbyname(ObjName) Then 'Check the form exists ThisDataBaseDocument.CurrentController.Connect() 'If the form exists connect to the database ThisDatabaseDocument.CurrentController.loadComponent(ObjTypeWhat, ObjName, FALSE) 'Open the form Else MsgBox "Error! Wrong form name used. "+chr(10)+"Form Name = " & ObjName End if End Sub
The routine FormChange will open the form named in the parameter sent to it and close the currently opened form.
I hope this sets you in the direction you wish.
Also, in future posts, please do not send samples with ‘live’ or ‘confidential’ data. While the more information provided is extremely helpful, always use TEST data.
I have sent back a file with the tables deleted. The only items you need to look at are the Preventivo_Scheda form and the Scheda Preventivi query.
Attachment | Size |
---|---|
G&G_CE_Sample.odb | 823 KB |
I thank you for the information I try macros that you sent me and have cleared the tables .