Lookup - Best Way and The Difference

Angelo
Member Posts: 180
I tried to created 4 methods to lookup table by using C/AL in the Control. All are have the same result (lookup the table, get the value, and put it to field or variable). What is the best way? are they any difference among this 4 methods?
"Test Lookup" is new field in Customer table.
In the form, pull "Test Lookup" and overwrite standard lookup :
"Test Lookup" - Onlookup (VAR Text : Text[1024];) : Boolean
//Method 1
CLEAR(CustForm);
CustTable.SETRANGE("No.",'10000','50000');
CustForm.SETTABLEVIEW(CustTable);
CustForm.SETRECORD(CustTable);
CustForm.LOOKUPMODE(TRUE);
IF CustForm.RUNMODAL = ACTION::LookupOK THEN BEGIN
CustForm.GETRECORD(CustTable);
"Test Lookup" := CustTable."No.";
END;
//Method 2
CLEAR(CustForm);
CustTable.SETRANGE("No.",'10000','50000');
CustForm.SETTABLEVIEW(CustTable);
//CustForm.SETRECORD(CustTable); //REMOVE SETRECORD
CustForm.LOOKUPMODE(TRUE);
IF CustForm.RUNMODAL = ACTION::LookupOK THEN BEGIN
CustForm.GETRECORD(CustTable);
"Test Lookup" := CustTable."No.";
END;
//Method 3
CustTable.SETRANGE("No.",'10000','50000');
IF FORM.RUNMODAL(0,CustTable) = ACTION::LookupOK THEN
"Test Lookup" := CustTable."No.";
//Method 4
CustTable.SETRANGE("No.",'10000','50000');
IF FORM.RUNMODAL(0,CustTable) = ACTION::LookupOK THEN BEGIN
Text := CustTable."No.";
EXIT(TRUE);
END;
"Test Lookup" is new field in Customer table.
In the form, pull "Test Lookup" and overwrite standard lookup :
"Test Lookup" - Onlookup (VAR Text : Text[1024];) : Boolean
//Method 1
CLEAR(CustForm);
CustTable.SETRANGE("No.",'10000','50000');
CustForm.SETTABLEVIEW(CustTable);
CustForm.SETRECORD(CustTable);
CustForm.LOOKUPMODE(TRUE);
IF CustForm.RUNMODAL = ACTION::LookupOK THEN BEGIN
CustForm.GETRECORD(CustTable);
"Test Lookup" := CustTable."No.";
END;
//Method 2
CLEAR(CustForm);
CustTable.SETRANGE("No.",'10000','50000');
CustForm.SETTABLEVIEW(CustTable);
//CustForm.SETRECORD(CustTable); //REMOVE SETRECORD
CustForm.LOOKUPMODE(TRUE);
IF CustForm.RUNMODAL = ACTION::LookupOK THEN BEGIN
CustForm.GETRECORD(CustTable);
"Test Lookup" := CustTable."No.";
END;
//Method 3
CustTable.SETRANGE("No.",'10000','50000');
IF FORM.RUNMODAL(0,CustTable) = ACTION::LookupOK THEN
"Test Lookup" := CustTable."No.";
//Method 4
CustTable.SETRANGE("No.",'10000','50000');
IF FORM.RUNMODAL(0,CustTable) = ACTION::LookupOK THEN BEGIN
Text := CustTable."No.";
EXIT(TRUE);
END;
1
Comments
-
I use method 2 because on a custom lookup I usually want to decide which form is used.
And SETRECORD is not necessary.
Tino Ruijs
Microsoft Dynamics NAV specialist0 -
I recommend to use the following
//Method 5 CustTable."No." := Text; CustTable.SETRANGE("No.",'10000','50000'); IF FORM.RUNMODAL(0,CustTable) = ACTION::LookupOK THEN BEGIN Text := CustTable."No."; EXIT(TRUE); END;
If you need a lookup form other than the default one you can either just replace the 0 with FORM::"My Lookup Form" or even declare a Form type variable and set it up as needed. Use SETTABLEVIEW as well as SETRECORD in that case.
Setting the primary key of the lookup table (using an assignment as above or using SETRECORD) sets focus to a record close to the one specified already (using FIND('=<>')). This enables you to start typing the code, then lookup, and chances are, you can just press enter to complete the code. Using SETRECORD without setting up the record first only overrides the SourceTablePlacement / SourceTableRecord properties.
Returning the value through Text rather then assigning directly to "Test Lookup" enables you to see the value you selected before confirming the field by leaving it and also to cancel the field entry after lookup before confirming the field.0 -
Hi Vaprog,
"Setting the primary key of the lookup table (using an assignment as above or using SETRECORD) sets focus to a record close to the one specified already (using FIND('=<>')). This enables you to start typing the code, then lookup, and chances are, you can just press enter to complete the code. Using SETRECORD without setting up the record first only overrides the SourceTablePlacement / SourceTableRecord properties."
--> When I test your above statement, I can't prove it. I tried to type customer code 30 (my intention to get customer code 30000), then I lookup or press enter, it will now return "30000".
"Returning the value through Text rather then assigning directly to "Test Lookup" enables you to see the value you selected before confirming the field by leaving it and also to cancel the field entry after lookup before confirming the field."
--> I'm a bit confused on this. when I test to put message("Test Lookup") onValidate, it will show "Test Lookup" value even though I assign to "Text" when lookup using your method.0 -
Hi AngeloAngelo wrote:--> When I test your above statement, I can't prove it. I tried to type customer code 30 (my intention to get customer code 30000), then I lookup or press enter, it will now return "30000".Angelo wrote:--> I'm a bit confused on this. when I test to put message("Test Lookup") onValidate, it will show "Test Lookup" value even though I assign to "Text" when lookup using your method.0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions