reate a database with one row

3 posts / 0 new
Last post
Jiyajijp
Offline
Joined: 12/08/2016 - 06:34
reate a database with one row

Hi Brian,
Is it possible to create a database with one row with blank/zero data with some column names.
Can we create a blank database using scripts.
Best Regards,
Jiyaji

Brian Element's picture
Brian Element
Offline
Joined: 07/11/2012 - 19:57

Hi Jiyaji,

Yes it is possible and I have done it many times for different scripts as sometimes you have to start from scratch.  If you look under the IDEAScript language browser under client look for NewDatabase.  Here is the example they use, they first create a new table using Client.NewTableDef function.  They then add on the fields using the AppendField, this is much like create a virtual field on a normal database.  The next step is to commit (create) the new database giving it a name.  Once the new database is created you can then start adding information to it, in this example they add one record.  I have a tendancy to do this in two steps, to first have a function to create the new database and then a second function to add the information.  I find it easier to follow as the adding information usually has other calculations in it.  You then append each new record using the AppendRecord function.  One thing is you need to close your database and reopen it to see any changes so I usually end the adding records with a db.close and then open it again.  If you don't do this it will appear blank until you manually close it and open it.  Here is the example script:


Sub Main

	' Create a table.
	Dim NewTable As Table
	Set NewTable = Client.NewTableDef
	
	' Define a field for the table.
	Dim AddedField As Field
	Set AddedField = NewTable.NewField
	AddedField.Name = "Field1"
	AddedField.Type = WI_CHAR_FIELD
	AddedField.Length = 20
	
	' Add the field to the table.
	NewTable.AppendField AddedField
	
	' Perform the same steps for a second field.
	Set AddedField = NewTable.NewField
	AddedField.Name = "Field2"
	AddedField.Type = WI_NUM_FIELD
	AddedField.Decimals = 2
	NewTable.AppendField AddedField
	
	' Change the table settings to allow writing.
	NewTable.Protect = False
	
	' Create the database.
	Dim db As Database
	Set db = Client.NewDatabase("SampleData.IMD", "", NewTable)
	
	' Obtain the recordset.
	Dim rs As RecordSet
	Set rs = db.RecordSet
	
	' Obtain a new record.
	Dim rec As Record
	Set rec = rs.NewRecord
	
	' Use the field name method to add data.
	rec.SetCharValue "Field1", "Character Value"
	rec.SetCharValue "Field2", 22.2
	rs.AppendRecord rec
	
	' Protect the table before you commit it.
	NewTable.Protect = True
	
	' Commit the database.
	db.CommitDatabase
	' Open the database.
	Client.OpenDatabase "SampleData.IMD"
	' Clear the memory.
	
	Set db = Nothing
	Set AddedField = Nothing
	Set NewTable = Nothing
	
End Sub 
Jiyajijp
Offline
Joined: 12/08/2016 - 06:34

Wow, this is the one Am looking for.. 
ThanQ Brain..