Subject Re: using a query as variable : question does anyone some opinions
From Mervyn Bick <invalid@invalid.invalid>
Date Fri, 31 May 2024 17:14:25 +0200
Newsgroups dbase.getting-started

On 2024/05/30 21:00, Dirk C wrote:
> Hello
> the following is working, but like to have som opinion about
>
> using thh query build on form or create a variable of a query
>
> both are working, but i am wondering if to catch the variable is more
> stable

One of the Golden Rules of programing is "If it works, it's not wrong.".
:-) There may, however, be a simpler, better, faster, more elegant (take
your pick :-) ) way of doing it.

The main reason for saving a query (or any object for that matter) to a
memory variable is to save typing later in your source code.  It makes
absolutely no difference to dBASE whether you use

      q_Cross = this.parent.vraagbak_artCon
      q_cross.active = false
      q_cross.sql = form.vraagbak_zoek
      q_Cross.active = true
or
      form.vraagbak_artCon.active = false
      form.vraagbak_artCon.sql = form.vraagbak_zoek
      form.vraagbak_artCon.active = true

All programmers develop a style so this is purely a personal opinion but
I don't like changing a query's sql property in a program.  While it is
possible with a standard query, keep in mind that this causes problems
with an ADOquery.  Instead of using applyLocate() I prefer to use a
parameter driven query.

Back in the early days of dBASE, FUNCTIONs returned a value and
PROCEDUREs didn't.  Over time the distinction has been removed and
PROCEDURE as defining command has fallen into disuse.  As function
knop_zoek_onclick() doesn't return a value it is actually a procedure.
As a procedure it actually needs some code to deal with success or
failure to find a matching record otherwise your program is just waiting
for further input from the user.  Either set the cursor ready to receive
a new search value or use the selected record to do something.

In function knop_zoek_onclick() there is no need for
q_Cross.rowset.beginlocate() which is used to allow a datalinked
entryfield to be used to input a search value.

Instead of nesting if...else...endif constructs as you have done, an
if...elseif...else...endif construct (which works exactly like DO CASE)
is a better option in this case as this terminates immediately a test
returns true.

If you use similar code multiple times in a function or procedure it can
usually be factored out.  This will cut down on the size of the code and
will usually make it easier to follow.

A reworked version of function knop_zoek_onClick() follows.  It hasn't
been tested so there may be typos.  I'm not suggesting that you change
your existing working code but this is an approach to keep in mind for
"next time".

function knop_zoek_onclick()
    local c_Ref1, c_Waarde1, q_Cross, c_Ref2, c_Ref3,bFound
    c_Waarde1 = this.parent.veld_Zoek.value
    q_Cross = this.parent.vraagbak_artCon
    q_Cross.rowset.locateOptions = 0
    c_Ref1 = "conartikel1 ='"+c_Waarde1+"'"
    c_Ref2  = "conartikel2 ='"+c_Waarde1+"'"
    c_Ref3  = "conartikel3 ='"+c_Waarde1+"'"
//   q_cross.active = false
//   q_cross.sql = form.vraagbak_zoek
//   q_Cross.active = true
    if empty(c_waarde1)
       bFound = false
       msgBox("data ingeven", "***** bci dataverwerking *****", 48)
         //No search value.  Remaining tests will be skipped
    elseif  q_Cross.rowset.Applylocate(c_Ref1) = true
         bFound = true
         //c_Ref1 found so rowpointer on record. Remaining tests will be
skipped
        //if c_Ref1 not found, try c_Ref2
    elseif  q_Cross.rowset.Applylocate(c_Ref2) = true
       bFound = true
    elseif  q_Cross.rowset.Applylocate(c_Ref3) = true
       bFound = true
    else
       bFound = false
       msgbox("geen data gevonden vir "+cWaarde1)
       form.veld_Zoek.value = ''  //Remove previous value
       form.veld_Zoek.setFocus()   // Ready to accept a new search value
    endif
    if bFound = true  //A matching row was found.
       form.processRow()  //Code to continue the program instead of
                          //waiting for the user to press another button.
    endif
    return


Mervyn.