The Base service provides a number of methods and properties to facilitate the management and handling of Office Base documents.
This service is closely related to the Document service, which provides generic methods for handling Office documents, including Base documents. Hence, the Base service extends the Document service and provides additional methods that are specific for Base documents, enabling users to:
Refer to the Document service to learn more about methods and properties that can be used to manage Office documents.
Before using the Base service the ScriptForge library needs to be loaded or imported:
• Basic macros require to load ScriptForge library using the following statement:
GlobalScope.BasicLibraries.loadLibrary("ScriptForge")
• Python scripts require an import from scriptforge module:
from scriptforge import CreateScriptService
The Base service can be invoked in a variety of ways. The code snippet below uses the method CreateBaseDocument from the UI service to create a new Base file.
Note that in all examples the object oDoc is an instance of the Base service.
Dim ui As Object, oDoc As Object
Set ui = CreateScriptService("UI")
Set oDoc = ui.CreateBaseDocument("C:\Documents\MyFile.odb")
The Base service can also be instantiated while opening an existing Base file, as shown below:
Set oDoc = ui.OpenBaseDocument("C:\Documents\MyFile.odb")
If a Base document is already open, it is possible to instantiate the Base service directly:
Dim oDoc As Object
Set oDoc = CreateScriptService("SFDocuments.Document", "MyFile.odb")
The examples above can be translated to Python as follows:
from scriptforge import CreateScriptService
ui = CreateScriptService("UI")
doc = ui.CreateBaseDocument(r"C:\Documents\MyFile.odb")
doc = ui.OpenBaseDocument(r"C:\Documents\MyFile.odb")
doc = CreateScriptService("SFDocuments.Document", "MyFile.odb")
The use of the "SFDocuments." substring in the previous example is optional.
List of Methods in the Base Service | ||
---|---|---|
CloseFormDocument FormDocuments Forms | GetDatabase IsLoaded OpenFormDocument | PrintOut SetPrinter |
Closes the given form document. Returns True if closure is successful.
svc.CloseFormDocument(formdocument: str): bool
formdocument: The name of the FormDocument to be closed, as a case-sensitive string.
If form documents are organised in folders, it is necessary to include the folder name to specify the form document to be opened, as illustrated in the following examples:
oDoc.CloseFormDocument("Folder1/myFormDocument")
doc.CloseFormDocument('Folder1/myFormDocument')
Returns an array with the full names (path/name) of all form documents in the Base document as a zero-based Array of strings.
svc.FormDocuments(): str[0..*]
The code snippet below prints the names of all form documents in the current Base document.
Dim oDoc as Object, myForms as Object, formName as String
Set oDoc = CreateScriptService("Document", ThisDataBaseDocument)
Set myForms = oDoc.FormDocuments()
For Each formName In myForms
MsgBox formName
Next formName
bas = CreateScriptService("Basic")
doc = CreateScriptService("Document", bas.ThisDataBaseDocument)
myForms = doc.FormDocuments()
for formName in myForms:
bas.MsgBox(formName)
To learn more about form documents, refer to the Form service help page.
Depending on the parameters provided this method will return:
svc.Forms(formdocument: str): str[0..*]
svc.Forms(formdocument: str, form: str = ''): svc
svc.Forms(formdocument: str, form: int): svc
formdocument: The name of a valid form document as a case-sensitive string.
form: The name or index number of the form stored in the form document. If this argument is absent, the method will return a list with the names of all forms available in the form document.
Although it is possible to use index numbers to refer to forms, this is only recommended when there is just one form in the form document. If there are two or more forms, it is preferable to use the form name instead.
The first line of the example below returns a list of all forms in the form document "myFormDocument". The second line returns an instance of the Form service representing the form "myForm".
Dim formsList as Object : formsList = oDoc.Forms("myFormDocument")
Dim oForm as Object : oForm = oDoc.Forms("myFormDocument", "myForm")
formsList = doc.Forms("myFormDocument")
form = doc.Forms("myFormDocument", "myForm")
Returns an instance of the Database service that allows the execution of SQL commands on the database defined and/or stored in the current Base document
svc.GetDatabase(user: str = '', password: str = ''): svc
user, password: Optional login parameters as strings. The default value for both parameters is an empty string "".
Dim myDoc As Object, myDatabase As Object, ui As Object
Set ui = CreateScriptService("UI")
Set myDoc = ui.OpenBaseDocument("C:\Documents\myDb.odb")
' User and password are supplied below, if needed
Set myDatabase = myDoc.GetDatabase()
' ... Run queries, SQL statements, ...
myDatabase.CloseDatabase()
myDoc.CloseDocument()
ui = CreateScriptService("UI")
myDoc = ui.OpenBaseDocument(r"C:\Documents\myDb.odb")
myDatabase = myDoc.GetDatabase()
# ... Run queries, SQL statements, ...
myDatabase.CloseDatabase()
myDoc.CloseDocument()
Returns True if the specified FormDocument is currently open.
svc.IsLoaded(formdocument: str): bool
formdocument: The name of a FormDocument to be checked, as a case-sensitive string.
If Not oDoc.IsLoaded("myFormDocument") Then
oDoc.OpenFormDocument("myFormDocument")
End If
if not doc.IsLoaded("myFormDocument"):
doc.OpenFormDocument("myFormDocument")
Opens the specified FormDocument either in normal or in design mode.
If the form document is already open, it is activated without changing its mode. The method returns True if the form document could be opened.
svc.OpenFormDocument(formdocument: str, designmode: bool = False): bool
formDocument: The name of the FormDocument to be opened, as a case-sensitive string.
designmode: If this argument is True the FormDocument will be opened in design mode.
Most form documents are stored in the root of the Base document and they can be opened simply using their names, as in the example below:
oDoc.OpenFormDocument("myFormDocument")
If form documents are organised in folders, it becomes necessary to include the folder name to specify the form document to be opened, as illustrated in the following example:
oDoc.OpenFormDocument("myFolder/myFormDocument")
doc.OpenFormDocument("myFormDocument")
doc.OpenFormDocument("myFolder/myFormDocument")
This method sends the content of the given form document to a default printer or a printer defined by the SetPrinter() method.
Returns True if the document was successfully printed.
svc.PrintOut(opt formdocument: str, pages: str = "", copies: num = 1): bool
formdocument: A valid document form name as a case-sensitive string. The form document must be open. It is activated by the method.
pages: The pages to print as a string, like in the user interface. Example: "1-4;10;15-18". Default is all pages.
copies: The number of copies. Default is 1.
If oDoc.PrintOut("myForm", "1-4;10;15-18", Copies := 2) Then
' ...
End If
if doc.PrintOut('myForm', copies=3, pages='45-88'):
# ...
Define the printer options for a form document. The form document must be open.
Returns True when successful.
svc.SetPrinter(opt formdocument: str, opt printer: str, opt orientation: str, paperformat: str): bool
formdocument: A valid document form name as a case-sensitive string.
printer: The name of the printer queue where to print to. When absent, the default printer is set.
orientation: Either PORTRAIT or LANDSCAPE. When absent, left unchanged with respect to the printer settings.
paperformat: Specifies the paper format as a string value that can be either A3, A4, A5, LETTER, LEGAL or TABLOID. Left unchanged when absent.
oDoc.SetPrinter("myForm", Orientation := "PORTRAIT")
doc.SetPrinter('myForm', paperformat='TABLOID')
All ScriptForge Basic routines or identifiers that are prefixed with an underscore character "-" are reserved for internal use. They are not meant be used in Basic macros or Python scripts.