Topic: If statement causes exe file to hang

I'm trying to fill a combobox manually as the field I need to fill it with  does not have a foreign key.


The code works without the IF statement but fails on <> ''. If I put a space between the two quote marks the program will start.

The reason for the IF statement below is because I don't want the combobox filled with Null values, I only want to see (in this case) a list of  purchase order numbers, so if one does not exist the combobox won't get filled with a space.

procedure frmMain_OnShow (Sender: TObject; Action: string);
var
    poRef: Tdataset;
begin
    
   try
        SQLQuery('SELECT orderRef FROM orders',poRef); {read in the table Orders column OrderRef}
        
        frmMain.cmbOrderRef.Clear; {clear the combobo contents}

        while not poRef.EOF do begin
         if (poRef.FieldByName('orderRef').AsString) <> ' '  then  {check for no PO number in field}
         begin
            frmMain.cmbOrderRef.Items.Add(poRef.FieldByName('OrderRef').AsString);
            poRef.next;
         end;
       end;
    finally
        poRef.close;
        poRef.Free
    end;
.......
.......
.......
end;

Any pointers as to what I'm doing wrong?

On a clear disk you can seek forever

2 (edited by derek 2020-05-25 13:51:49)

Re: If statement causes exe file to hang

Hi CDB,
Not an answer to your actual question(sorry!) but you can always fill a combobox with rows from any table irrespective of whether there is a relationship or not. 
Just manually type in the table name in the 'foreign key' property and the field name in the 'fieldname' property of the combobox object.  Functionally, it's a bit like a 'listbox' in Excel.
You can then use the 'filter' property of the combobox to get rid of any null values.
Obviously, doing it like this, there is no referential integrity (nor is there with a script) but it's very easy.
Have a look at the attachment.
Derek.

Post's attachments

Attachment icon cdbcb.zip 337.2 kb, 283 downloads since 2020-05-25 

3 (edited by CDB 2020-05-26 00:15:23)

Re: If statement causes exe file to hang

Well that I didn't know Derek, thanks.

I had a brainwave this morning and realised that I can solve the problem in the SQLQuery.

So I now remove the IF statement altogether and change the query to


SQLQuery('SELECT orderRef FROM orders WHERE orderRef > 0',poRef);

And using your tip of typing the table name into the foreign key property  I placed the "orderRef >0" into the filter property.


The only difference between this and using my code, is that with the code there is no blank (-1) itemIndex and using MVD there is.  picture attached.

Post's attachments

Attachment icon MVD_addingnotnulltocombo.png 53.82 kb, 128 downloads since 2020-05-26 

On a clear disk you can seek forever

4 (edited by derek 2020-05-26 10:09:49)

Re: If statement causes exe file to hang

Hi CDB,
If you don't want to see the first entry blank, untick the 'first empty item' property for the combobox (see screenshot). 
However, the 'first empty item' of a combobox is there to give you the option to select ALL items in any search;  untick that property or build your own combobox by sqlquery and you lose that option.
Derek.

Post's attachments

Attachment icon cdbscreenshot.jpg 141.32 kb, 133 downloads since 2020-05-26