Step 4: Add Code
The Main() subroutine is called when the application is started, so we'll look at that first. As you can see from the following listing, this is standard Visual Basic code. AppForge supplies its own set of database access routines, all of which begin with PDB...().
Sub Main() Dim txtCustomer As String Load frmCustomer If (OpenCustomerDatabase = True) Then 'Database was successfully opened. ' 'Populate the list box and show the form. PDBMoveFirst dbCustomer While Not (PDBEOF(dbCustomer) = True) PDBGetField dbCustomer, tCustomerDatabaseFields.Name_Field, txtCustomer frmCustomer.lbCustomers.AddItem txtCustomer PDBMoveNext dbCustomer Wend frmCustomer.lbCustomers.ListIndex = 0 frmCustomer.Show Else 'Unable To Open Database frmCustomer.Hide frmError.Show End If End Sub
The OpenCustomerDatabase() call was prebuilt by the conversion utility and is included in modCustomerDB.bas. The PDBMoveFirst() function jumps to the first record in the database; the PDBMoveNext() steps through the database one record at a time. We use the PDBGetField method to retrieve a specific field from a retrieved record. This field, tCustomerDatabaseFields.Name_Field, represents the customer's name and is added to the lbCustomers list box on the frmCustomer form (see Figure 3).
The frmCustomer form at runtime.
Selecting OK from frmCustomer causes the customer selected in the lbCustomers list box to be loaded into the frmCustomerDetail form text boxes (see Figure 4).
The frmCustomerDetail form at runtime.
The code that handles this button-click event is as follows:
Private Sub btnSelect_Click() Dim Selection As String Dim custRec As tCustomerRecord Selection = lbCustomers.List(lbCustomers.ListIndex) PDBMoveFirst dbCustomer While Not (PDBEOF(dbCustomer) = True) ReadCustomerRecord custRec If (custRec.Name = Selection) Then frmCustomerDetail.txtCustomerName.Text = custRec.Name frmCustomerDetail.txtCustomerCompany.Text = custRec.Company frmCustomerDetail.txtCustomerEMail.Text = custRec.EMail frmCustomerDetail.txtCustomerAddress.Text = custRec.Address frmCustomerDetail.txtCustomerCity.Text = custRec.City frmCustomerDetail.txtCustomerState.Text = custRec.State frmCustomerDetail.txtCustomerSalesRep.Text = custRec.SalesRep Me.Hide frmCustomerDetail.Show Exit Sub Else PDBMoveNext dbCustomer End If Wend 'If we make it to this point, the record wasn't found! Me.Hide frmError.Show End Sub
Selecting the Back button in frmCustomerDetail executes the following standard VB code:
Private Sub btnDetailBack_Click() Me.Hide frmCustomer.Show End Sub
The AppForge Booster is the runtime "virtual machine" that's installed on the PalmOS, allowing our VB app to run unmodified. The Booster is freely distributable and can be downloaded from http://www.appforge.com.