Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If..Else statements failing to recognize value!

Status
Not open for further replies.

jcfrasco

IS-IT--Management
Apr 27, 2001
89
US
I used an ADOQuery object to connect to SQL server and I successfully retrieved my recordset. I put a grid on my form connected to the ADOQuery object to see what values it returns. My problem is that I would like to use If..Else statements to set certain controls according to the values. I'm using statements like:
If adoqLogon.FieldByName('strModule').AsString='WorkOrder' Then 'Do Something'. When I run the program it will not recognize the values such as WorkOrder in the field. But, if I set the If statement to look for a value not equal to WorkOrder, it runs the required code. I'm assuming that it's a string but I think that it's cast as something else.

I would appreciate any advice on this matter.

jcfrasco
 
Is it case-sensitive? ('WorkOrder' vs. 'WORKORDER')
Is the database column name really 'strModule' (or is strModule a string variable that contains the actual column name?)
Does the database column really contain the value 'WorkOrder' or is that the column name?

It's really hard to tell from the small fragment of code you supplied.
 
Sorry for being so vague on the details. The value in the table is exactly 'WorkOrder', the name of the field in the database is strWorkOrder. I used SQL in the ADOQuery object to return a dataset like this: SELECT strLoginID, strModule FROM Security_Main WHERE strLoginID= txtLoginID.text. After I get the recordset I want to look at the value returned in strModule for a particular LoginID and set various controls on the form according to what this LoginID is allowd to do. After the recordset opens, I'm using If statments to see if the value returned in the strModule field is equal to another string value. The idea, when everything works correctly, is to increment through the recordset comparing the values in strModule to a preset value. If the value is in the recordset then the LoginID is granted permission for that module. If not, then this LoginID does not have permission to enter. I was using Flash Filer database before and everything worked fine. Now I'm migrating this program to SQL server 2000 and the data types are a little different.

I hope this helps.

jcfrasco
 
I'll have to play with it for awhile and get back to you. Meanwhile, why not just include this with your WHERE clause:
Code:
 ' AND strModule=' + txtModule.Text
(or whatever contains the module name you are looking for)
 
Here is the SQL I used to create a test table:
Code:
create table Security_Main
  (strLoginID varchar(8),
   strModule varchar(10))
insert into Security_Main values('me','Module1')
insert into Security_Main values('me','WorkOrder')
insert into Security_Main values('me','Module3')
Here is some code that works. Compare with what you are doing do see where the problem is.
Code:
procedure TForm1.Button7Click(Sender: TObject);
begin
  adoqLogon.SQL.Clear;
  adoqLogon.SQL.add( 'SELECT strLoginID, strModule FROM Security_Main WHERE strLoginID='
                  + QuotedStr('me'));
  adoqLogon.Open;
  while not adoqLogon.Eof do
    begin
      if adoqLogon.FieldByName('strModule').AsString='WorkOrder' Then
         ShowMessage ( 'Do Something' )
      else
         ShowMessage( 'Not permitted to access ' + adoqLogon.FieldByName('strModule').AsString);
      adoqLogon.Next;
    end;
  adoqLogon.Close;
end;
(I would normally use "with..do" but I thought this might be clearer.)

 
Try doing this:

var
s : string;
begin
s := Trim(adoqLogonstrModule.AsString);
if s = 'WorkOrder' then
{code}
else
{code}
end;

Simply put, the data just might need to have any spaces trimmed from it. That would explain why the false IF statement is working for you, and the true IF isn't.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top