Solved

Formatting DBEdit-Components ???

Posted on 2000-04-25
10
2,345 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
Technology Partners: 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 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses
Course of the Month10 days, 6 hours left to enroll

624 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