Format a textcontrol!

  • Thread starter Thread starter Niklas Östergren
  • Start date Start date
N

Niklas Östergren

Hi!

I have created a function in which I format phone numbers according to
standard format in Sweden. The function returns the phone number (string) in
formatted version. This function works without any problem

I use this fomat functions i several forms but I end up with the
phonenumbers stored in the table formatted with - signs and spaces. That´s
not what I want. I´d like to store the data (phonenumbers) as the way it was
entered (ex. 087465211 not 08-746 52 11.

I run the function from the controls AfterUpdate_Event.

Anyone knowing what am I doing wrong and how to deal with this?

TIA!
// Niklas
 
Hi again!

Forgot to tell you.
In the AfterUpdate_Event of the control in which I´d like to format the data
i use the function like this:

Me.txtPhoneNo = FormatPhoneNo (Me.txtPhoneNo)

TIA!
// Niklas
 
I think you are doing this the hard way, just set the Format property of the
control to:

@@-@@@ @@ @@

This will affect how the data is displayed, but not how it is stored, so you
will also need to use it on reports. You could also set it as an input mask,
but you might find that the way these work irritates the users!
 
Hi John!

Well, the thing is that sometimes a phone number have totally 9 digits and
sometimes it has 10. And within these two main groups we have a varity of
different formatting depending on the prefix number. Her´s some examples:

030-45 71 22
0304-571 22
030-457 12 23
08-45 71 22
08-457 12 23

So you se what I´m trying to accomplish is formatting the phone numbers
correct no matter which phone number it is. And so far my function does
work. My problem is, which I think you get, is that the numbers also stores
formatted.

I´ll be avy for some hours but I´ll be back later today. Thanks for taking
time helping me out!

// Niklas
 
Your telephone number formats are as inconsistent as ours are then!

In that case you only have two options:
Format the number when it is entered and store the formatted number.
Store just the digits (the KeyPress event can be used to stop the user entering
anything but digits) and format it when you output it.

I am off for the weekend soon, but will be back on Monday if you want to carry
on the discussion.
 
Niklas said:
Forgot to tell you.
In the AfterUpdate_Event of the control in which I´d like to format the data
i use the function like this:

Me.txtPhoneNo = FormatPhoneNo (Me.txtPhoneNo)


"Niklas Östergren" skrev


The problem is that you're (re)setting the value of the text
box to the formatted string so that is what is being saved.
Since you can not get by with using the Format property, I
suggest that you use two text boxes, one for the user to
enter the unformatted value and the other to display the
formatted.

You can make this combination look just like a single text
box with its format property doing the formatting (as soon
as a user starts to edit the value, the unformatted string
is displayed). Position the display text box exactly on top
of the bound text box, txtPhoneNo. This way, the user will
see the formatted value.

Set the display text box (the one in front) GotFocus event
to immediately SetFocus to the data entry text box:

Me.txtPhoneNo.SetFocus

This will automatically bring the data entry text box to the
front so the user can edit/enter the phone number.

Set the txtPhoneNo text box's AfterUpdate event to store
your function's formatted string to the display text box:

Me.txtPhoneNoDisplay = FormatPhoneNo(Me.txtPhoneNo)

As soon as the user exits the data entry text box, the
display text box will automatically jump back to the front
and display the formatted result.
 
OK!

That´s one solution I havn´t thought of, obviously.

Isn´t it possible to change the controls format property at runtime?

Reguards!
// Niklas
 
Depends on the kind of form you're going to do this in.

You can not get a useful result by changing the properties
of a control in Continuous view. What I posted will work
in in either Continuous or Single Form view.

In Single Form view, your function could return the
appropriate Format string so you could stuff it into the
format property without affecting the saved value.
 
OK!

Thanks a lot Marshall I have now thought fo this during the night and I have
desided to save the data formatted and in case I need to I´ll trim the
string from not needed charcters before using it for anything else than just
storing it and showing it for the users.

The reason to this is that I have several form´s showing the same phone
numbers, which the user can chouse, depending on in which part of the
program they are currently working in. And two forms is of single form style
but the third form is a continuous form and I don´t want to have different
approaches to how to handle this in my db. So I´ll go for saving the data
formatted since this mean that I only have to deal with it once and that´s
when the user is entering the phone numbers or editing it.

Thanks for helping me out!

// Niklas


Marshall Barton said:
Depends on the kind of form you're going to do this in.

You can not get a useful result by changing the properties
of a control in Continuous view. What I posted will work
in in either Continuous or Single Form view.

In Single Form view, your function could return the
appropriate Format string so you could stuff it into the
format property without affecting the saved value.
--
Marsh
MVP [MS Access]

That´s one solution I havn´t thought of, obviously.
Isn´t it possible to change the controls format property at runtime?

the
data way
it
 
Back
Top