Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > DB-Aware

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 07-31-2015, 10:00 AM
Marsheng Marsheng is offline
Senior Member
 
Join Date: Nov 2008
Posts: 314
Default Syntax for database object.

I can't seem to find how to do this. Tried many options

tEdit(fMemEdit.FindComponent('eMasterMEMNAME')).te xt:=tdbEdit(DM.FindComponent('tblMasterMEMNAME')). Field.AsString;


I can find the edit box on the form but not the data in the Data Module table

Thanks Wallace

Last edited by Marsheng; 08-02-2015 at 01:29 AM.
Reply With Quote
  #2  
Old 08-02-2015, 01:33 AM
Marsheng Marsheng is offline
Senior Member
 
Join Date: Nov 2008
Posts: 314
Default

This works

eMasterMEMNAME.Text:=dm.tblMasterMEMNAME.AsString;

so my variables are ok but I get access violation on any code I use for this

Code:
tEdit(fMemEdit.FindComponent('eMasterMEMNAME')).text:=tStringField(DM.FindComponent('tblMasterMEMNAME')).AsString;
The second part is wrong.
Reply With Quote
  #3  
Old 08-05-2015, 10:18 PM
Marsheng Marsheng is offline
Senior Member
 
Join Date: Nov 2008
Posts: 314
Default

Something is not right.

I get access violation error when I run this on the line eMasterMEMNAME.Text

If I comment out tblName.TableName:='tblMaster' I don't have an error.

Code:
var
   tblName: TMyTable;
begin
    tblName.TableName:='tblMaster';
    eMasterMEMNAME.Text:='Test';
Really confused now.
Reply With Quote
  #4  
Old 08-06-2015, 11:04 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,333
Default

And what is TMyTable???
An object you need to create perhaps?
Reply With Quote
  #5  
Old 08-06-2015, 11:22 AM
Marsheng Marsheng is offline
Senior Member
 
Join Date: Nov 2008
Posts: 314
Default

I'm using Devart's components and the tables are called TMyTable.

When compiling, delphi is happy with the code. Only crashes when I run it.

I may try another computer and see if I get the same error.
Reply With Quote
  #6  
Old 08-06-2015, 02:06 PM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,333
Default

Surely you don't have the basics down...

Code:
var
   tblName: TMyTable;
begin
    tblName.TableName:='tblMaster';
Replace TMyTable for TButton, to clarify that it has nothing to do with Devart in this case
Code:
procedure TForm1.Button1Click(Sender: TObject);
var 
  item: TButton;
begin
  item.Caption := 'crash';
end;
Run and you'll see it crashes (Access Violation)
You'll need to create the component (and free it as soon as you don't use it anymore

If you use the debugger (yes, Delphi has a good one, learn to use it), you'll see that item (or in your case tblName) is nil, and nil has nothing, so calling nil.Caption (or nil.Whatever) will raise an AV.
Reply With Quote
  #7  
Old 08-06-2015, 10:18 PM
Marsheng Marsheng is offline
Senior Member
 
Join Date: Nov 2008
Posts: 314
Default

Quote:
Surely you don't have the basics down...
You are correct, I'm still thinking as a DOS programmer. I've tried to find OOP courses but living here in Christchurch New Zealand my options are nil. A Java course is $3000 or a BSc at the University are my options.

I've tried some online tutorials but they seem to raise more questions than answers!!

The long term goal is do up-skill my programming.

PS you code does not crash - it changes the name of the form to 'Crash'

My short term issue is, find all the comp on a form with a prefix, say eMaster and retrieve the data from a data module table using part of the editbox name.

eMasterMEMNAME.Text gets the contents of the field MEMNAME from the table tblMaster in the data module.

I can find the components on the form, strip the names and build the required strings but I cannot find the syntax to retrieve the data from the data module.

The components do exist in the data module but I cant find how to access them without using dbEdit boxes.

What am I missing?

Thanks very much for you time.
Reply With Quote
  #8  
Old 08-07-2015, 08:03 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,333
Default

In your original code you do to many things in 1 line...

Split it up, so you can validate. If for some reason a component cannot be found an AV will be thrown. Furthermore, you do a hard cast, without 100% knowing that the found component is of that type. This can also be validated if you split things up.

Example of validating:
Code:
// NOTE: This is just a demonstration, it uses showmessages to show the state of the component
procedure ShowComponentState(AParentComponent: TComponent; AName: String; AClass: TClass);
var
  component: TComponent;
begin
  component := AParentComponent.FindComponent(AName);
  if not Assigned(component) then
    ShowMessage(AName + ' not found')
  else
  begin
    if not component.InheritsFrom(AClass) then
      ShowMessage(AName + ' is not of class ' + AClass.ClassName)
    else
      ShowMessage('Succesfully found ' + AName);
  end;
end;

procedure TForm2.Button1Click(Sender: TObject);
begin
  // Some examples of validating
  ShowComponentState(Self, 'edit1', TEdit); // found
  ShowComponentState(Self, 'edit2', TEdit); // not found
  ShowComponentState(Self, 'adotable1', TEdit); // invalid type
  ShowComponentState(Self, 'adotable1', TADOTable); // found
end;
And to go back to your original question, I doubt "tblMasterMEMNAME" is of type tStringField. Most likely of type TMyTable (??)
Example of testing using the method above:
Code:
  ShowComponentState(DM, 'tblMasterMEMNAME', TStringField);
  ShowComponentState(DM, 'tblMasterMEMNAME', TMyTable );
Reply With Quote
  #9  
Old 08-07-2015, 08:45 AM
Marsheng Marsheng is offline
Senior Member
 
Join Date: Nov 2008
Posts: 314
Default

Thanks I try that over the weekend.

I've made an appointment with a lecturer next week to see about upskilling.

After your last post I youtubed OOP and watched 4-5 but all were not much help.

I still think in procedural way rather the OO. I'm beginning to think OO is just a fancy word for a procedural function with parameters !!

I do a bit of c with assembler and this is always procedural.

Thanks Wallace.
Reply With Quote
  #10  
Old 08-07-2015, 11:05 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,333
Default

No, OO is not a fancy word, especially with the introduction of generics it's very powerfull.
But as you stated correctly, it expects a different mindset.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 05:44 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.