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 ULConnectionParms object to connect to a database. Alternatively, you can use a connection string in the application code.
See ULConnectionParms class.
Double-click the form to open the Code window.
Declare the required UltraLite objects:
Enter the following code in the declarations area of your form.
Public DatabaseMgr As New ULDatabaseManager Public Connection As ULConnection Public CustomerTable As ULTable
Specify the connection parameters:
Add a ULConnectionParms object to your form named ULConnectionParms1. The ULConnectionParms control is a database icon on the toolbox.
In the Properties window, specify the ULConnectionParms properties.
If you are deploying to a Windows CE device, specify the following properties:
Add code to connect to the database in the Form_Load event.
The database manager opens a connection to the database specified by the ULConnectionParms1 object:
Private Sub Form_Load() ' enable error handling On Error Resume Next Set Connection = DatabaseMgr.OpenConnection(ULConnectionParms1.ToString()) If Err.Number = ULSQLCode.ulSQLE_NOERROR Then MsgBox "Connected to an existing database" Else MsgBox Err.Description Exit Sub End If End Sub
Once the connection code is working, the line that issues a Msgbox to indicate a connection has been made can be removed.
If you prefer to use a connection string rather than the ULConnectionParms object, you can alter the code illustrated above to use this syntax instead:
Set Connection = DatabaseMgr.OpenConnection _ ("dbf=C:\tutorial\mvb\tutcustomer.udb;" & _ "ce_file=\tutorial\mvb\tutcustomer.udb")
Note the inclusion of the database file name specification for the potential target platforms (dbf= for the desktop environment and ce_file= for the Windows CE device environment.
Add code to end the application and close the connection when the End button is clicked.
Sub btnDone_Click() Connection.Close End End Sub
Run the application.
Choose Run > Execute.
After an initial message box, the form loads.
To terminate the application, click the End button.