Solved

Formatting DBEdit-Components ???

Posted on 2000-04-25
10
2,086 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
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…

707 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

14 Experts available now in Live!

Get 1:1 Help Now