Automatically place decimal point (9850 become 98.50)

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

In bound fields, how can I have the decimal point automatically entered.
Instead of entering 98.50, users would enter 9850, and 98.50 would be stored
as the value in the table. Other examples below:

$100.00 would be entered as 10000
$2.50 would be entered as 250
$3.45 would be entered as 345

Will i need to write specific VB code for each field or is there a global
way to handle this?
 
You would divide by 100. You would either have to add code to the
AfterUpdate event of each control, or add code to the AfterUpdate event of
the form to process each control. The latter would involve less code, but
might be more confusing for the user, as the individual controls would not
be updated until the entire record was saved.

My recommendation is to not do this at all. The saving of a single
key-stroke per control doesn't, in my opinion, justify the adoption of a
non-standard approach.
 
In bound fields, how can I have the decimal point automatically
entered. Instead of entering 98.50, users would enter 9850, and 98.50
would be stored as the value in the table. Other examples below:

$100.00 would be entered as 10000
$2.50 would be entered as 250
$3.45 would be entered as 345

Will i need to write specific VB code for each field or is there a
global way to handle this?

Why store that calculated value in a table?

Couldn't you use a calculated field in the query to achieve that effect?
And show the result on the form, properly formatted, under/next to the
field where the value is entered, if it is absolutely necessary for users
to see that result.

Do not store a calculation in a table. Base all reports on that query,
though.
 
These are not calculated fields. These fields are being entered by users.

I am updating a legacy system that used this approach. The users' are
insisting that I not make them type the decimal point, but keep it as it was.
 
You'd have to write code for each control. I'd put code in the BeforeUpdate
or AfterUpdate like this,

If Instr(Me.txtSomeCtl,".")=0 Then
Me.txtSomeCtl = Me.txtSomeCtl/100
End If

If the user does enter a decimal, the value is accepted "as is". Otherwise,
it gets divided by 100.
 
Will i need to write specific VB code for each field or is there a global
way to handle this?

You can make a public function, place it in a standard module.

You can use:

Public Function AddDecimal()

Dim c As Control
Dim strValue As String

Set c = Screen.ActiveControl

If IsNull(c.Value) = True Then
Exit Function
End If

' convert number to string

strValue = CStr(c)

If InStr(strValue, ".") = 0 Then
' add the decimal
c.Value = c.Value / 100
End If

End Function

now, for each number type contorl on a form, simply put the above function
name in the controls after update event.

=AddDecimal()

In fact, yuou can hilghtl all 2, 3, or however many number contorls you have
on a screen, and hten type in the above function name. This way, you don't
have to edit all the contorls one by one. This approach also means you don't
have to create a event routine for EACH number control.

By the way, I also used a good number of legacy systems, and accounting
systems...and they often use the data entry format as you describe.
 
Back
Top