Solved

Formatting DBEdit-Components ???

Posted on 2000-04-25
10
2,176 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
  • 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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 50 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Mydac connection data base issue 3 157
Best Firemonkey component pack 1 103
Communication Between RC4 Delphi <-> PHP 3 113
Comparing dates in Delphi. Greater than/ Less than 3 151
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

776 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