Macro to print a specific report

9 replies [Last post]
giampoul
Offline
Last seen: 9 weeks 3 days ago
Title: ★★
Joined: 26 Feb 2016
Posts: 15
I’m not a macro expert and I can not fix a problem that could be simple but that to me is very difficult hope that this forum will find a solution . The problem is getting the form where there is a button to start printing a report corresponding to the form of records to which I worked.I am attaching a odt files for greater understanding and a solution Thank You
AttachmentSize
G&G_CE2.odb98.57 KB
Ratskinger
Offline
Last seen: 4 days 18 hours ago
Title: ★★★★
Joined: 23 Feb 2016
Posts: 91
The result you wish is not
The result you wish is not clear. If you want all records in Query printed using Writer must set up mail merge. Better to use Report Builder. If you wish only record on form printed, more complicated – search for Invoicing. As is, it is performing as designed.
giampoul
Offline
Last seen: 9 weeks 3 days ago
Title: ★★
Joined: 26 Feb 2016
Posts: 15
I would like that while
I would like that while working on the tab form Estimates for example in the record No. 3, and clicking the Print button to open up to me the report by displaying the record n ° 3.
frofa
frofa's picture
Offline
Last seen: 13 hours 23 min ago
Title: ★★★★★
Joined: 24 Feb 2012
Posts: 804
maybe switch to a 'printout form' & try using a batch number
Hi giampoul:

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).

giampoul
Offline
Last seen: 9 weeks 3 days ago
Title: ★★
Joined: 26 Feb 2016
Posts: 15
Hi Frofa, Press the button
Hi Frofa,

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.

frofa
frofa's picture
Offline
Last seen: 13 hours 23 min ago
Title: ★★★★★
Joined: 24 Feb 2012
Posts: 804
your report data-source is multi-record
Hi giampoul:

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.

Ratskinger
Offline
Last seen: 4 days 18 hours ago
Title: ★★★★
Joined: 23 Feb 2016
Posts: 91
Hello giampoul I’m still
Hello giampoul

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.

giampoul
Offline
Last seen: 9 weeks 3 days ago
Title: ★★
Joined: 26 Feb 2016
Posts: 15
Almost or partially solved
I adopted by the time the second farfa solution to my problem lies, however, would be better or more suitable a macro … I hope that one of the Macro-experienced employees can help me. Thank you
Ratskinger
Offline
Last seen: 4 days 18 hours ago
Title: ★★★★
Joined: 23 Feb 2016
Posts: 91
Hello giampoul Thank you
Hello giampoul

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.

AttachmentSize
G&G_CE_Sample.odb 823 KB
giampoul
Offline
Last seen: 9 weeks 3 days ago
Title: ★★
Joined: 26 Feb 2016
Posts: 15
hi Ratskinger Ti ringrazio,
hi Ratskinger

I thank you for the information I try macros that you sent me and have cleared the tables .

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.