Copying data from one table to another

Creating and using forms
Post Reply
vstarc16
Posts: 36
Joined: Sun Feb 10, 2008 7:00 pm
Location: Zagreb, Croatia

Copying data from one table to another

Post by vstarc16 »

I need a macro that will copy data from the selected record (from the Table1 displayed in a form in a table control) and write them in a Table2, than reload the form that displays Table2. I already have in use macro that gets the data from the selected record in a table control and reload the Form2 with the filter applied (see below).

Code: Select all

GLOBAL index_surname as string, index_name as string, myrb as integer

sub Otvori_brza_pretraga
'Get the form (in this case TestForm1 = records of the database).
   odoc=thiscomponent
   oform=odoc.drawpage.forms.getbyindex(0)
   
'Get the contents of the first row (=index field in my case) of the selected record.
   index_surname=oform.getstring(14)
   index_name=oform.getstring(4)


'Display the second form (TestForm2).
   OpenForm(getFormsTC, getConnectionTC,"Pregled_dolazaka")

'Filter the TestForm2.
   odoc=thiscomponent
   oform=odoc.drawpage.forms.getbyindex(0)
   oform.ApplyFilter = True
   
  oForm.filter =("prezime LIKE " + "'%"+index_surname+"%'"+" AND name LIKE " + "'%"+index_name+"%'")
  
  
'Reload the TestForm2 with the filter applied.
   oform.reload
End Sub
I know that part of the code that applies filter have to be modified, but I have no idea how the code should read, I would really appreciate help with this.

Vanja
OOo 3.1.X on Ms Vista
dstockman
Posts: 38
Joined: Fri Feb 22, 2008 3:20 pm

Re: Copying data from one table to another

Post by dstockman »

Vanja:

Sorry, I do not have an answer to your question. I have the same question. Please let me know if you find the answer. I have the same code snippet you have for filtering forms so only the "active" record data appears in new forms you open.

I will describe my specific case.

My initial form allows a user to query for a patient's name or medical record number MRN). This initial query populates a table control in the initial form. The user can then choose which of the query results is the patient they want. The MRN is the field that links all the tables (Primary key). Therefore, when the user selects the patient they want to work with, I need to get the MRN from the table control on the initial form and save that MRN in a global variable so that all subsequently opened forms apply only to the selected pts.

In a way, the real question is how do I find the specific MRN value (or any value in a form/table) based on a user-chosen record in the table control.

Thanks for any help anyone can offer.

Doug
User avatar
voobase
Volunteer
Posts: 97
Joined: Tue Jan 15, 2008 3:07 pm
Location: Australia

Re: Copying data from one table to another

Post by voobase »

Hi there folks,

Here are a couple of links which might get you started.

The first thread contains links to forum pages that detail various macros for filtering to records. It also has the procedure to go straight to a certain record (which might be better for Doug).
http://www.oooforum.org/forum/viewtopic.phtml?t=76753
The second thread has a bit of a discussion about timing issues which can occur when you are trying to do these sorts of things. If you open a form using a macro you need to wait for it to finish loading before you try to copy stuff accross to it or else you might get errors. It also has a sub to straight away create a new record after the form has been loaded...
http://www.oooforum.org/forum/viewtopic.phtml?t=77391

As for copying things into a second table prior to the second form being opened, you could do this in your code using SQL before you launch the other form. Here is a macro I have written which demonstrates how to use the SELECT, INSERT and UPDATE syntax with your basic code. The macro's purpose is to update an "error log" table with the error name (and count) which is passed into the macro from other sub's. I have the error log table displayed in another form in a tablegrid and it has a button for doing the reload (for simplicity) (just need to set the "action" in the buttons properties to reload). If you needed the macro to do the reload of the other form, then you just need to get reference to the other form. I'll include that bit of code towards the end...

Code: Select all

Rem... Will record the last instance of an error in a table with the error count
Sub ErrorCall (Error_Name as String, Error_Count as integer)
'msgbox "ErrorCall"
		dim oForm as object
		dim Result
		dim strSQL As String
		dim oCreateStatement as object
		dim oCreateStatement2 as object
		dim aNum as integer
		
		oForm = thisComponent.Drawpage.Forms(0)
	Rem... This sub requires there to be at least one error in the table for things to work. This first bit just checks for that and adds in one if necessary
		strSQL="SELECT ""Error_Name"", ""ID"", ""Error_Date_Stamp"", ""Error_Time_Stamp"", ""Error_Count"" FROM ""Error_Table"" ORDER BY ""ID"" DESC "
		
  		oCreateStatement = oForm.ActiveConnection.CreateStatement
 		Result = oCreateStatement.executeQuery(strSQL)
 		
		If Result.next() then   Rem.. This actually does a result.next() so if there is no record yet the else bit inserts one.
Rem... There is already a first error in table so do nothing
		Else
		strSQL="INSERT INTO ""Error_Table"" ( ""Error_Name"" ) VALUES ( '" & "First Error" & "' )"

		oCreateStatement3 = oForm.ActiveConnection.CreateStatement
		oCreateStatement3.executeUpdate(strSQL)
		End if
		
		If  Error_Name <> Result.GetString(1) then 
		
		strSQL="INSERT INTO ""Error_Table"" ( ""Error_Name"", ""Error_Count"" ) VALUES ( '" & Error_Name & "', '" & Error_Count & "' )"

		oCreateStatement2 = oForm.ActiveConnection.CreateStatement
		oCreateStatement2.executeUpdate(strSQL)
		
		Else
			Rem.. Get the current date from the hsql database engine
				oStatement = oForm.ActiveConnection.CreateStatement
                oStatement.EscapeProcessing = False
                oResultSet = oStatement.executequery( "CALL CURRENT_DATE" )
                'oResultSet = oStatement.executequery( "CALL CURRENT_TIMESTAMP" ) Rem.. If you were after the current timestamp then this way
                oResultSet.next


			If Result.GetString(1) = Error_Name AND CdateToIso(Result.GetString(3)) = CdateToIso(oResultSet.getString( 1 )) then
					Error_Count = Error_Count + Result.GetInt(5)
				strSQL = "UPDATE ""Error_Table"" SET ""Error_Name"" = '" & Error_Name & "', ""Error_Count"" = '" & Error_Count & "'  WHERE ""ID"" = '" & Result.GetInt(2) & "' "	
		
			
				oStatement2 = oForm.ActiveConnection.CreateStatement
				oStatement2.executeUpdate(strSQL)
				
			Else 
		'	msgbox "Error is different or Date is different"
			End if
			
		End if

Rem... To reload a particular form you would do something like this...
Rem... The second form is found by searching from thiscomponent for the form name
   oForm2 = thisComponent.Parent.FormDocuments 
   if oForm2.HasByName("Form2Name") then     
      oForm2 = oForm2.getByName("Form2Name")
      if not IsNull(oForm2.Component) then    
        oForm2 = oForm2.Component.DrawPage.Forms.getByName("MainForm")     

oForm2.Reload()
     End if
  End if       

End sub
If the syntax of the SQL statement looks a bit confusing then think of it this way. What is happening is you are creating a "string" out of various shorter strings which are joined with the & symbol. Also remember that to pass the " symbol in a string you need to double it to "". So something like this SQL string

Code: Select all

"UPDATE ""Error_Table"" SET ""Error_Name"" = '" & Error_Name & "', ""Error_Count"" = '" & Error_Count & "'  WHERE ""ID"" = '" & Result.GetInt(2) & "' "
is actually made out of these strings which are strung together by the & symbol...

Code: Select all

"UPDATE ""Error_Table"" SET ""Error_Name"" = '"
Error_Name
"', ""Error_Count"" = '"
Error_Count
"'  WHERE ""ID"" = '"
Result.GetInt(2)
"' "
Where Error_Name and Error_Count are string variables

Couple of quick tips for Vanja.... When you are getting your form, you might find this code better to use...

Code: Select all

oForm = thisComponent.Parent.FormDocuments 
   if oForm.HasByName("FormName") then     
      oForm = oForm.getByName("FormName")
      if not IsNull(oForm.Component) then    
        oForm = oForm.Component.DrawPage.Forms.getByName("MainForm")     

Rem... Either put your code here or after the second end if

     End if
  End if
Also, rather than finding things by their index number. Your code will read better if you find things by their name... for example rather than using...

Code: Select all

index_surname=oform.getstring(14)
You might use...

Code: Select all

index_surname=oform.getstring(oForm.FindColumn("SurnameColumn"))
And the syntax for updating that field this way would be

Code: Select all

oform.UpdateString( oForm.FindColumn("SurnameColumn"), "TheStringSurnameValue" )
Also a good thing to remember is that when you are using the above lines of code you are only reading or changing the form's RowSet. This means you are not reading or changing the data in the actual database table directly. The RowSet is like a copy of the row of data from the database table. You can manipulate the data in the rowset and it is stored back in the database table after you do a oForm.Updaterow() (i.e a save). Refreshing the rowset in your form occurs when ever you change to a new row in your form, or use the code oForm.Refreshrow()

For Doug... You have the choice of at least three ways to ensure your subsequently opened forms only relate to that particulat MRN. Information on all should be found if you follow the links in the threads I have linked to up the top.
1/ By using the form filters
2/ By going straight to the appropriate record by using a sub similar to the "findFirst_Own_data_source" sub which is in one of the threads. Also have a look at the doco by C.Benitez which I had linked to there.
3/ Rather than using a global variable you may prefer to create a "Dummy table" which is a table that holds only one row of data. You store your MRN value in that and use an "UPDATE" SQL statement when ever you need to change it. In the forms you create you could base the MainForm on this "Dummy table" and create a "SubForm" to the mainform which displays your medical record. If you set up the link/join between the Mainform and Subform then only the records with that MRN would be displayed. It would be similar to the "Dropdown Record Switcher" detailed in this thread... http://www.oooforum.org/forum/viewtopic.phtml?t=72134

Cheers

Voo
OOo 2.3.X on MS Windows Vista
dstockman
Posts: 38
Joined: Fri Feb 22, 2008 3:20 pm

SOLVED Re: Copying data from one table to another

Post by dstockman »

Voo:

Thanks very much for all the information. I now have all my forms linked via the MRN field so all forms I open only relate to a specific patient. This has been very helpful. You know a great deal and are a very patient teacher. Thanks!

I have so much to learn and so little time. Is there one place to go to find much of this information already organized?

Doug
User avatar
voobase
Volunteer
Posts: 97
Joined: Tue Jan 15, 2008 3:07 pm
Location: Australia

Re: Copying data from one table to another

Post by voobase »

No worries Doug,
I have so much to learn and so little time. Is there one place to go to find much of this information already organized?
I wish you all the best for your project. Be careful because it does take time if you start putting lots of macro code behind your forms.
Is there one place to go to find much of this information already organized?
Unfortunately I have not found that one place yet. There are some good tutorials out there and you can get a lot of information by searching the two forums. Here are some links to all the information I use. I find that the choice ones are those by Andrew Pitonyak and C.Benitez, especially "forms and dialogs". The "Basic Programmers Guide" also is quite good. For more complete doco, try the SDK, however, it is rather complex.

If you want to learn to write macro's make sure you familiarize yourself with using the XRAY tool. It is invaluable. With XRAY you can check the properties and methods of the controls and forms. You will soon learn how everything relates. http://wiki.services.openoffice.org/wik ... ment_basic

Getting started with base. (wiki)
http://wiki.services.openoffice.org/wik ... ng_Started

Star Office User Manuals and tutes
http://docs.sun.com/app/docs/coll/so7en

NeoOffice User Guides
http://trinity.neooffice.org/modules.php?name=Forums

Basic Programmers Guide
http://wiki.services.openoffice.org/wiki/Documentation

StarOffice Programmer’s Tutorial
http://api.openoffice.org/basic/man/tut ... torial.pdf

Andrew Pitonyak macro examples
http://www.pitonyak.org/oo.php

Forms and dialogs pdf and Database Development pdf by C.Benitez
http://www.geocities.com/rbenitez22/

Learn Open Office.org Spreadsheet Macro Programing
http://www.packtpub.com/openoffice-ooob ... k#indetail
(Chapter 6 on databases is free... look for link on RHS of page)

HSQLDB User Guide
http://hsqldb.org/doc/guide/

Open Office Software Development Kit
http://download.openoffice.org/2.4.0/sdk.html

OpenOffice.Org Training Tips and Ideas
http://openoffice.blogs.com/openoffice/reports/

Cheers

Voo
OOo 2.3.X on MS Windows Vista
dstockman
Posts: 38
Joined: Fri Feb 22, 2008 3:20 pm

Re: Copying data from one table to another

Post by dstockman »

Voo:

Great links! Now I just need to quit my jbb and leave my family so I can learn all this.

Thanks!

Doug
Post Reply