VB for the Palm OS
By submitting your email address, you agree to receive emails regarding relevant topic offers from TechTarget and its partners. You can withdraw your consent at any time. Contact TechTarget at 275 Grove Street, Newton, MA.
Intimidated by the prospect of PalmOS C programming? Looking for a rapid application development tool for PDA development? This article explores the new AppForge development toolkit. (To download a zip containing the source files for this article, click here.)
Bryan Morgan was the founder of the Wireless Developer Network. He is currently an independent consultant and writer focusing on the wireless industry. Mr. Morgan is a regular contributor to InformIT (from which this tip is excerpted) on wireless application development topics.
AppForge development actually takes place within the Visual Basic 6 programming environment and uses the Visual Basic programming language. For an experienced Visual Basic developer, I'd put the learning curve to picking up AppForge anywhere from 15 to 30 minutes! In this article, I'll step through the process of creating a database-driven customer contacts application. I'll use a simple customer contacts database and build several screens using AppForge for viewing this data.
Step 1: Create an Access Database
The PalmOS includes a filesystem that functions as a database, albeit a non-relational one. "Tables" are accessed on an individual basis and any joins that must take place must be "hand-rolled" by the developer. (This is not the case if you're using a commercial database for the PalmOS such as Oracle Lite or IBM's DB/2 Everywhere, but that's a discussion for another article!) AppForge includes a utility that converts Microsoft Access .MDB databases into the .PDB format used by the PalmOS. This process is as simple as creating a database using Microsoft Access and then converting the database using the AppForge Database Converter for Palm.
Our customer database is quite simple, containing fields for a name, company, email address, address, city, state, ZIP, and sales representative. Before converting the file, you'll be prompted for a CreatorID and a Type. These are PalmOS constructs used to uniquely identify a specific database (necessary since all databases are stored in one flat-file system). To reserve a unique ID (important if you're doing commercial PalmOS development), visit http://www.palmos.com/dev/tech/palmos/creatorid/. I completed this process and reserved the CreatorID XZXZ, which you'll see if you analyze the source code included with this example.
Once the conversion process has been completed, the converter will ask whether you want to create a Visual Basic code module for working with the database. Choosing OK causes a complete helper module to be created, simplifying routine tasks such as opening the database and getting/retrieving records. This module also contains a new Visual Basic type that represents a single record within this table.
Step 2: Create a Visual Basic Project
At this stage, we're ready to create our AppForge project and begin coding. Start Visual Basic 6 and select AppForge Project from the New Project dialog box. Doing this sets several compilations and project defaults and loads the AppForce ingots, ActiveX controls that function in both the Visual Basic development environment and on the PalmOS. These ingots include common user-interface widgets such as the AFButton, AFCheckBox, AFGrid, AFLabel, AFListBox, and AFTextBox. All AppForge projects require a Sub Main() to be defined and set as the startup object, so your first step should be to create a new module?name it whatever you want; I chose modCustomerDatabase?and set that as the default start object. This setting can be accomplished by selected Project/Properties and setting the Startup Object to Sub Main().
Step 3: Create Necessary Forms
Our simple customer contacts application will require three forms:
- frmCustomer?Used to display a list of customer names to the user; contains a list box (lbCustomers) and a button (btnSelect).
- frmCustomerDetail?Used to display the details of a specific customer; contains a list of labels and associated text boxes (for customer name, company, email, address, city, state, and sales rep information) along with a button (btnDetailBack) for returning to frmCustomer.
- frmError?Used to display a basic error to the user if the database cannot be found or if a record doesn't exist.
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, xtCustomer frmCustomer.lbCustomers.AddItem txtCustomer PDBMoveNext dbCustomer Wend frmCustomer.lbCustomers.ListIndex = 0 frmCustom er.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.
Selecting OK from frmCustomer causes the customer selected in the lbCustomers list box to be loaded into the frmCustomerDetail form text boxes. 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.
To read this entire tip, and to get a look at figures and download some zipped code, click over to InformIT. You have to register there, but it's free.
David Mccarter's VB Tips and Techniques
Author : David McCarter
Publisher : APress
Published : Jun 2000
The author provides hard to find coding tips and techniques that span the gamut for Visual Basic professionals. Tips that could save hours of work and increase productivity.