Solved

Formatting DBEdit-Components ???

Posted on 2000-04-25
10
2,129 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

919 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now