Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Formatting DBEdit-Components ???

Posted on 2000-04-25
10
Medium Priority
?
2,430 Views
Last Modified: 2010-08-05
Hello,

I have an DB-Application.
One of the tables has a numeric field.
The field always has to contain a number of the format : YYYYMMxxx,
where 'xxx' is a unique number within the same year (YYYY) and month (MM).

This number has to be shown in both a DBGrid and DBEdit, but has to have the following
format : YYYY-MM-xxx.
I used the FieldEditor of my Table-component, and fill the DisplayFormat and EditFormat with '0000-00-000'.
Displaying the field looks OK, but when I want to change the DBEdit, than I have to fill in
the whole number (YYYYMMxxx), wihtout the hyphens.
What I want is that the user sees '____-__-___' and just has to fill in the numeric part.
(so, the DBEdit should act like a MaskEdit with Editmask set to '0000-00-000').


Can someone help me out with this?

P.S. : I'm using D5 with Access-DB (Office 97).
0
Comment
Question by:wimmeyvaert
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2747530
hi,

just place this line of code

dbedit.field.editmask := YourmaskDefintionstring;

meikl
0
 
LVL 6

Author Comment

by:wimmeyvaert
ID: 2747587
Hi meikl,

I already tried this, but it gives an error when posting the record (not a valid integer value') because my field is numeric (TIntegerField) and the DBEdit contains something like '2000-04-001'

0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2747709
hmm, i see,

ok a workarround, don't use the displayformat or editformat if your TIntegerField-Instance, just use this

procedure TForm1.FormCreate(Sender: TObject);
begin
  dbedit1.field.editmask := '0000-00-000;0;_';
end;

let me know, if it works

meikl
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Author Comment

by:wimmeyvaert
ID: 2747832
Hello meikl,

I think I don't have good news -> same error (but now also the displaying of the data isn't good, because I left the DisplayFormat-property blank).

I worked out a solution myself, but I'm not very happy with it. I suppose there must be a cleaner way to solve my problem !
This is what I do :

DisplayFormat and EditFormat : '0000-00-000'

My code :

procedure TForm1.DBNavigator1BeforeAction(Sender: TObject; Button: TNavigateBtn);
var
  strTemp : String;
begin
  if Button in [nbEdit, nbInsert] then
    DBEdit1.Field.EditMask := '0000-00-000'
  else if Button in [nbPost, nbCancel] then
  begin
    if Button = nbPost then
    begin
      With DBEdit1 do
      begin
        strTemp := StringReplace(EditText,'-','',[rfReplaceAll]);
        Field.EditMask := '';
        Field.Text := strTemp;
      end;
    end;
  end;
end;


Maybe you have a better suggestion?
Thanx,
The Mayor.

P.S. : Doesn't there exist DBMaskEdit-components (freeware + source) ?
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2747909
? terrible,
it works for me as recommended,
be sure to include in your mask the ";0;_" part
so that the whole maskdefinitionstring looks '0000-00-000;0;_'
well for display you must in this case i guess also the displaymask to the same. (I had not done this)

meikl
0
 
LVL 6

Author Comment

by:wimmeyvaert
ID: 2747951
Hi meikl,

I'm leaving the office right now, but I'll try your solution (again) tomorrow,
and I will let you know if it works ...


CU,

The Mayor


0
 
LVL 6

Author Comment

by:wimmeyvaert
ID: 2754699
Hello meikl,

I've tested your suggestion. It is correct that the data in de DbEdit is
displayed correctly, but when I focus the control, than the data looks all messed up.

Example :
DB-Field contains : 123456789
-> DBEdit contains : 1234-56-789 <OK>
-> When DBEdit is focussed, than it suddenly contains : 1234--5-6-789 <NOK>


Do you have an explanation for this strange behavior ?

Best regards,
The Mayor.



0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2755210
hi mayor,

well i see, its a conflict between the editmask and the displayformat. here a workaround

unit dbe_mask_u;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, DBCtrls, StdCtrls, Mask, Db, DBTables;

type
  TForm1 = class(TForm)
    Table1: TTable;
    DataSource1: TDataSource;
    DBEdit1: TDBEdit;
    DBEdit2: TDBEdit;
    DBNavigator1: TDBNavigator;
    procedure FormCreate(Sender: TObject);
    procedure DBEdit2Enter(Sender: TObject);
    procedure DBEdit2Exit(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  dbedit2.Field.EditMask := '0000-00-000;0;_';
  TIntegerField(dbedit2.Field).DisplayFormat := '0000-00-000;0;_';
end;

//empty the displayformat onenter the control
procedure TForm1.DBEdit2Enter(Sender: TObject);
begin
  TIntegerField(dbedit2.Field).DisplayFormat := '';
  dbedit2.SelectAll;
end;

//reset the displayformat onleave the control
procedure TForm1.DBEdit2Exit(Sender: TObject);
begin
  TIntegerField(dbedit2.Field).DisplayFormat := '0000-00-000;0;_';
end;

end.

hope this helps

meikl
0
 
LVL 6

Author Comment

by:wimmeyvaert
ID: 2755559
Hello meikl,

I had already made the same changes you propose, and it works OK.

Anyway, because you have been so helpfull, I'll give you the points.

Thanx again,

The Mayor.
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 150 total points
ID: 2756237
well mayor,

guessing i should answer,
i do that now

glad that you get it work
good luck again

meikl ;-)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question