UltraLite ActiveX User's Guide
Tutorial: An UltraLite Application for PocketPC
Lesson 3: Write the sample code
In this application, you connect to the database during the Form_Load event. You can also connect to a database using the general module.
This example uses a connection string to connect to the database. For an example using a ULConnectionParms object, see Lesson 3: Write the eMbedded Visual Basic sample code.
Write code to connect to the UltraLite database
Double-click the form to open the Code window.
Declare the required UltraLite objects.
Enter the following code in the General area of your form.
Dim DatabaseMgr As ULDatabaseManager Dim Connection As ULConnection Dim CustomerTable As ULTable Dim colID, colFirstName, colLastName As ULColumn
Add code to connect to the database in the Form_Load event.
In the code below, CreateObject
is used to create the initial database manager object. The database manager then tries to open a connection to the database specified by the connection string. If the database does not exist, it creates a new database using the given schema.
Sub Form_Load() Dim conn_parms As String Dim open_parms As String Dim schema_parms As String On Error Resume Next conn_parms = "uid=DBA;pwd=SQL" open_parms = conn_parms & ";ce_file=\Program Files\tutorial\tutCustomer.udb" schema_parms = open_parms & ";ce_schema=\Program Files\tutorial\tutCustomer.usm" Set DatabaseMgr = CreateObject("UltraLite.ULDatabaseManager") Set Connection = DatabaseMgr.OpenConnection(open_parms) If Err.Number = UlSQLCode.ulSQLE_NOERROR Then MsgBox "Connected to an existing database." ElseIf Err.Number = UlSQLCode.ulSQLE_DATABASE_NOT_FOUND Then Err.Clear Set Connection = DatabaseMgr.CreateDatabase(schema_parms) If Err.Number <> 0 Then MsgBox Err.Description Else MsgBox "Connected to a new database" End If End If End Sub
Run the application.
Choose Run
After an initial message box, the form loads.
Click OK in the top right corner to terminate the application.
Use the File Viewer to check that a database file named tutcustomer.udb has been created on the emulator.