Forms, Formats, and Contents

was studying the excellent Francesco Balena book, “ProgrammingVisual Basic 6.0,” edited by Microsoft Press, the cost of which iswidely compensated by both the book and the enclosed CD contents&#151:when Iran into the explanation of DataFormat property for data bound controls,and of StdDataFormat object. It seemed a very interesting subject for anarticle, so here it is. At the same time, I thank Francesco for thisopportunity.

One of DAO (and RDO) defects is that in order to view data in adefinite format we must write specific code when reading or writing data (toconvert a viewing formatted value into field formatted one and viceversa), andthis defect influences the data editing phase and the input one too.

ADO exposes the DataFormatproperty for data bound controls, that allow us to view data in the wanted format,while it remains in its native format in the table field. Custom formats arepossible too. DataFormat property sets or returns a format object, not aformat string, as you might think if you are familiar with Format$function. This object has not only some properties, in which the obvious Formatone, but also very useful events (which I’ll speak about afterwards).

Yet, for more demandingformatting needs, such as the currency conversion ones, for example, DataFormatproperty may turn out to be insufficient. Let’s suppose we have a productstable with prices in Currency format, and set in the currency of yourcountry. We can set DataFormat property as Currency and so weget, since our system is so set, a custom format for our currency. Everythingworks very well: we ‘see’ the prices in Currency format on the form andin numeric format in the table. Let’s make things a bit more complex now, bydisplaying values as Euro currency on the form while continuing to keep thevalue in the native currency in the database table. This task requires somecode for conversions between the currencies and a different format for eachone.

That’s why a library providedwith VB (“Microsoft Data Formatting Object Library”) isuseful: it exposes StdDataFormat object. Once we have instanced such anobject, we can set the properties we are interested in (the Type property inparticular), assign it to a data bound control DataFormat property andtake advantage of Format and UnFormat events, that represent itsreal power.

The Format event raiseswhen data is ‘read’ from the data source and is about to be displayed in thebound control (more in general, used by the data consumer), whereas the UnFormatevent raises when a data bound control value (if changed) is written back tothe data source. This means that we have two places which we can write code,for complex formatting operations, instead of writing in several places, as wemust do with a DAO (or RDO) control.

Another very interesting aspect is that it’s not necessary we set theproperties of a StdDataFormat object: we can just take advantage of itsevents to manage by code the formatting operations, as in this case, when acurrency conversion is involved.

If we don’t use a StdDataFormatobject (that is we rely on DataFormat property only) these propertiesand events are managed implicitly. Nevertheless my idea is of dealing withEuro-other currency conversion, without creating a new field in the table, andwith as little code quantity as possible. As a matter of fact I need to translatethe displayed data in the selected currency (Euro or my currency), keeping theoriginal format (in my currency) in the data source table.

The Study Project, Step by Step
Create a new project, select it in Project Window and name it as EuroConvert.
Select Form1 and name it frmProdotti; set its Caption as “Products”.
Add the “Microsoft ADO Data Control 6.0 (SP3)(OLEDB)” to theproject components.
Add the “Microsoft Data Formatting Object Library” to theproject references.
Add to frmProdotti an ADODC control (Adodc1), and set its Alignproperty as 2 – vbAlignBottom.
Click on Adodc1 with the right mouse button and select Properties:In the General tab set the connection source as ‘Use connectionstring‘, click on Generate button, select “Microsoft Jet3.51 OLE DB Provider”, then click on Next button and then onthe little button on the right of the textbox to select file Nwind.mdb,that is in the same VB folder; verify the connection clicking on the properbutton and then on OK button.
TAKE CARE: during this study the data will be changed. Therefore, if you wantto save the original file, get a copy of it to restore the fact before thestudy.
In the RecordSource tab of Properties dialog for Adodc1,set the Command Type as 2 – adCmdTabel and select Productstable.

Add to frmProdotti a textbox and a label for two fields of the table (ProductName,UnitPrice) setting adequately first the DataSource property forthe textboxes (Text1 and Text2 ) as Adodc1 and then the DataFieldone to the relative field, then ‘copy’ the name of the field and ‘paste’ it inthe Caption property of the relative label (Label1 and Label2).
Near the Text2 textbox, the one for the price, put an OptionButton (Option1),’copy’ it and ‘paste’ it and answer ‘Yes’ to get a two OptionButton array (Option1(0)and Option1(1)), set their Caption property as “ourcurrency” and “Euro”, respectively; the former Valueproperty is set as True (I’m calling ‘our currency’ what you must intendfor French, or German, or other country currency).

Normal Situation
Let’sset Text2 DataFormat property by clicking on the ellipsis buttonand selecting Currency. Then click OK: now we have our textboxvisualizing our currency in our known format. Eventually, the format is acustom one (for VB), if it’s not a standard format. Now we can run ourapplication and try to change the values in the price textbox. We realize thatthe currency symbol ($, L, D,

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may