![]() |
#1
|
|||
|
|||
![]()
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. |
#2
|
|||
|
|||
![]()
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; |
#3
|
|||
|
|||
![]()
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'; |
#4
|
|||
|
|||
![]()
And what is TMyTable???
An object you need to create perhaps? |
#5
|
|||
|
|||
![]()
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. |
#6
|
|||
|
|||
![]()
Surely you don't have the basics down...
Code:
var tblName: TMyTable; begin tblName.TableName:='tblMaster'; Code:
procedure TForm1.Button1Click(Sender: TObject); var item: TButton; begin item.Caption := 'crash'; end; 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. |
#7
|
|||
|
|||
![]() Quote:
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. |
#8
|
|||
|
|||
![]()
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; Example of testing using the method above: Code:
ShowComponentState(DM, 'tblMasterMEMNAME', TStringField); ShowComponentState(DM, 'tblMasterMEMNAME', TMyTable ); |
#9
|
|||
|
|||
![]()
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. |
#10
|
|||
|
|||
![]()
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. |
![]() |
Thread Tools | |
Display Modes | |
|
|