| View previous topic :: View next topic |
| Author |
Message |
Chrisb1985 General User

Joined: 26 Jul 2011 Posts: 23
|
Posted: Mon Oct 31, 2011 12:55 pm Post subject: Display last record |
|
|
| Is it possible to set a form's default to find the last record when it opens, rather than the first? |
|
| Back to top |
|
 |
dacm Super User


Joined: 07 Jan 2010 Posts: 734
|
Posted: Mon Oct 31, 2011 2:03 pm Post subject: |
|
|
You could base the Form on a Query or SQL command that sorts the records by the auto-increment ID field descending.
OR use a macro:
| Code: | Sub InitializeForm 'Form > Event > When Loading
Dim oForm As Object
Wait 100 'required on slower CPUs to allow time for Form to load
oForm = ThisComponent.DrawPage.Forms.GetByIndex(0) '.GetByName("<form name here>") may be easier to troubleshoot
' oForm.MoveToInsertRow
oForm.Last
' oForm.First 'this along with .Last above leaves the total number of records in nav bar
End Sub |
see also: http://www.oooforum.org/forum/viewtopic.phtml?p=377359#377359 _________________ Soli Deo gloria
Tutorial: avoiding data loss with Base + Migrating 'Embedded databases' |
|
| Back to top |
|
 |
RPG Super User

Joined: 24 Apr 2008 Posts: 2696 Location: Apeldoorn, Netherland
|
Posted: Mon Oct 31, 2011 3:08 pm Post subject: |
|
|
Hello dacm
You use the wait statement but I think the next line is maybe better.
| Code: | | if oForm.isloaded = false then oForm.load |
Romke |
|
| Back to top |
|
 |
dacm Super User


Joined: 07 Jan 2010 Posts: 734
|
Posted: Mon Oct 31, 2011 9:52 pm Post subject: |
|
|
Romke,
Looks good.
...or maybe the following since the macro is run from the Form's 'When Loading' event:
| Code: | ' Wait 100 'required on slower CPUs to allow time for Form to load
Do Until oForm.isloaded = true 'replaces Wait 100 (above)
Loop |
dacm |
|
| Back to top |
|
 |
RPG Super User

Joined: 24 Apr 2008 Posts: 2696 Location: Apeldoorn, Netherland
|
Posted: Tue Nov 01, 2011 12:38 am Post subject: |
|
|
Hello Dacm
I can not proof it but I have the idea with the "do until" the computer can spent a lot of time in the loop and can not work on loading the form. When I started with the API it have had also work with the wait statement. I have learned the oform.isLoaded and oform.load from a person who did have an other problem and he did use isloaded. In tests I have done it seems to me that the combination is working.
An other idea is that the API is well designed and when you work with the API in the way they expect then you need not so much code. This means for me: the API must do the work and not BASIC.
Romke |
|
| Back to top |
|
 |
dacm Super User


Joined: 07 Jan 2010 Posts: 734
|
Posted: Tue Nov 01, 2011 7:05 am Post subject: |
|
|
| RPG wrote: | | ...with the "do until" the computer can spent a lot of time in the loop and can not work on loading the form |
Yes, that's probably true. I should have placed the "Wait" inside the loop.
| Code: | Do Until oForm.isloaded = true 'allows faster CPUs to skip the loop
Wait 100 'allows time for the Form to load on slower CPUs
Loop |
Or reduced to:
| Code: | | IF oForm.isloaded = false THEN Wait 100 'allows faster CPUs to continue while waiting for slower ones |
But why would you send oform.load to a form that's already in the process of loading? Since this macro is triggered by the 'when loading' event, the Form is always in the process of loading when that command is dispatched, so it just seems redundant from the Basic-coding perspective. _________________ Soli Deo gloria
Tutorial: avoiding data loss with Base + Migrating 'Embedded databases' |
|
| Back to top |
|
 |
RPG Super User

Joined: 24 Apr 2008 Posts: 2696 Location: Apeldoorn, Netherland
|
Posted: Tue Nov 01, 2011 10:41 am Post subject: |
|
|
Hello Dacm
There is a little difference in the way we have the same problem. I did discover the problem when start loading a form document and want go to a specific record. the BASIC code was faster then loading the document sometime it was working good sometimes not. In this thread the code is execute when the dataform is already is loaded or start loading.
During writing this post I did some test.
It seems to me when you want go to the last record of a dataform then the next code is enough.
The macro must be bound to the dataform and not to the datadocument.
| Code: | Sub Macro1(oEvent)
dim oForm
oForm=oEvent.source
if oForm.isloaded then oForm.last
End Sub |
The oForm.isloaded is needed for not going to the last record when going to edit mode of the form. It works even when all records must be loaded and it takes two seconds.
This is the same problem as for all new OOo users who starts with macros. You cannot use a little part of code in an other place without understanding all details. It give me also problems.
Romke |
|
| Back to top |
|
 |
Villeroy Super User


Joined: 04 Oct 2004 Posts: 10065 Location: Germany
|
Posted: Tue Nov 01, 2011 12:01 pm Post subject: Re: Display last record |
|
|
| Chrisb1985 wrote: | | Is it possible to set a form's default to find the last record when it opens, rather than the first? |
I use to revert the sort order so the latest record is the first. New records are appended to the end until you refresh the form. _________________ Rest in peace, oooforum.org
Get help on http://forum.openoffice.org |
|
| Back to top |
|
 |
RPG Super User

Joined: 24 Apr 2008 Posts: 2696 Location: Apeldoorn, Netherland
|
Posted: Tue Nov 01, 2011 12:44 pm Post subject: |
|
|
Hello
I did have some more testing
The small macro is for going to the last record. It must be called from an event of a dataform. This is real simple
| Code: | Sub Macro1(oEvent)
dim oForm
oForm=oEvent.source
if oForm.isloaded then oForm.last
End Sub |
The next two macro are doing the same. They are both called from a button in a form document calling an other form document for opening going to the last record. In the macro OpenForm1 I do use a variable for an object what is returned from the methode for opening a formdocument. Then I need the line : if oForm.isloaded=false then oForm.load. If I do not use this line then I get an error. I have not to use a wait statement.
| Code: | sub openForm1
'Variables for document what will be opened
dim sNewfileFormName ' This is the name how it is in the database
sNewfileFormName="loading" 'is a writer form it must be in this database
dim oNewDoc
if ThisDatabaseDocument.FormDocuments.hasbyname(sNewfileFormName) then
' the form exist and can maybe opend
oNewDoc=ThisDatabaseDocument.FormDocuments.getbyname(sNewfileFormName).open
else
msgbox "The does not exist and cannot be opend. We end the programm"
end
end if
'The form is now open
oForm = oNewDoc.DrawPage.Forms.GetByIndex(0)
if oForm.isloaded=false then oForm.load
oForm.Last
end sub |
In the sub openForm2 I do not use the object what is returned by opening a form. I do use stardesktop.currentcomponent what is the eqyuivalent of thiscomponent but thiscomponent is not working. Now I have to use: wait 100. After the wait then the currentcomponent of the desktop is the form what is opened. But it is not permitted to use the line: if oForm.isloaded=false then oForm.load
| Code: | sub openForm2
'Variables for document what will be opened
dim sNewfileFormName ' This is the name how it is in the database
sNewfileFormName="loading" 'is a writer form it must be in this database
dim oNewDoc
if ThisDatabaseDocument.FormDocuments.hasbyname(sNewfileFormName) then
' the form exist and can maybe opend
ThisDatabaseDocument.FormDocuments.getbyname(sNewfileFormName).open
else
msgbox "The does not exist and cannot be opend. We end the programm"
end
end if
'The form is now open
wait 100 '
oForm = stardesktop.currentcomponent.DrawPage.Forms.GetByIndex(0)
oForm.Last
end sub |
When Villeroy means try to find easy solutions without using macros he has real right. Even this thread makes clear that little difference makes code is not more working.
Romke |
|
| Back to top |
|
 |
dacm Super User


Joined: 07 Jan 2010 Posts: 734
|
Posted: Tue Nov 01, 2011 3:02 pm Post subject: |
|
|
| RPG wrote: | | When Villeroy means try to find easy solutions without using macros he has real right. Even this thread makes clear that little difference makes code is not more working. |
Yes, simple is often the best approach. I mentioned sorting in the first line of my first response above, but I just thought we were having a little fun with the macro-code optimization along the way.
| dacm wrote: | You could base the Form on a Query or SQL command that sorts the records by the auto-increment ID field descending.
OR use a macro: |
_________________ Soli Deo gloria
Tutorial: avoiding data loss with Base + Migrating 'Embedded databases' |
|
| Back to top |
|
 |
RPG Super User

Joined: 24 Apr 2008 Posts: 2696 Location: Apeldoorn, Netherland
|
Posted: Tue Nov 01, 2011 3:17 pm Post subject: |
|
|
Hello
| dacm wrote: | | I just thought we were having a little fun with the macro-code optimization along the way | It was for me also fun with macros and also to discover again what the little difference mean.
Romke |
|
| Back to top |
|
 |
Chrisb1985 General User

Joined: 26 Jul 2011 Posts: 23
|
Posted: Wed Nov 02, 2011 12:16 pm Post subject: |
|
|
| Thanks for the help. Problem Solved |
|
| Back to top |
|
 |
|