[go: up one dir, main page]

0% found this document useful (0 votes)
20 views5 pages

Code

delphi code

Uploaded by

ebrahimameera28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views5 pages

Code

delphi code

Uploaded by

ebrahimameera28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1️⃣ Login Form (frmLogin)

procedure TfrmLogin.btnLoginClick(Sender: TObject);

var

sFirstname, sSurname, sPassword: string;

LoggedInUserID: Integer;

begin

sFirstname := edtFirstname.Text;

sSurname := edtSurname.Text;

sPassword := edtPassword.Text;

LoggedInUserID := -1; // Default: not found

// Search for user in TblUser

DataModule1.TblUser.First;

while not DataModule1.TblUser.Eof do

begin

if (DataModule1.TblUser['Firstname'] = sFirstname) and

(DataModule1.TblUser['Surname'] = sSurname) and

(DataModule1.TblUser['Password'] = sPassword) then

begin

LoggedInUserID := DataModule1.TblUser['UserID'];

Break;

end;

DataModule1.TblUser.Next;

end;

if LoggedInUserID = -1 then

begin

ShowMessage('Login failed: User not found.');

Exit;

end;
ShowMessage('Login successful!');

// Pass the UserID to frmMyInfo and show it

frmMyInfo.UserID := LoggedInUserID;

frmMyInfo.Show;

end;

2️⃣ frmMyInfo Form

type

TfrmMyInfo = class(TForm)

DBGrid1: TDBGrid;

private

FUserID: Integer;

procedure SetUserID(const Value: Integer);

public

property UserID: Integer read FUserID write SetUserID;

procedure LoadUserDeliveries;

end;

implementation

procedure TfrmMyInfo.SetUserID(const Value: Integer);

begin

FUserID := Value;

LoadUserDeliveries; // Automatically load deliveries when UserID is set

end;

procedure TfrmMyInfo.LoadUserDeliveries;

begin

if FUserID = 0 then Exit; // safety check


DataModule1.DeliveryQuery.Close;

DataModule1.DeliveryQuery.SQL.Clear;

DataModule1.DeliveryQuery.SQL.Add(

'SELECT * FROM TblDelivery WHERE UserID = :UserID'

);

DataModule1.DeliveryQuery.ParamByName('UserID').AsInteger := FUserID;

DataModule1.DeliveryQuery.Open;

DBGrid1.DataSource := DataModule1.DeliveryDataSource;

end;

Perfect! Let’s make it super simple, Grade 12-level Delphi code to delete the currently selected
delivery in the DBGrid. No fancy stuff, just the basics:

Basic Delete Code

procedure TfrmMyInfo.btnDeleteClick(Sender: TObject);

begin

// Check if there is a record to delete

if DataModule1.DeliveryQuery.RecordCount = 0 then

begin

ShowMessage('No record to delete.');

Exit;

end;

// Ask user to confirm

if MessageDlg('Are you sure you want to delete this delivery?',

mtConfirmation, [mbYes, mbNo], 0) = mrNo then

Exit;
// Delete the current record

DataModule1.DeliveryQuery.Delete;

// Optional: show a message

ShowMessage('Delivery deleted successfully.');

end;

✅ Explanation (simple):

1. RecordCount = 0 → checks if the query has records.

2. MessageDlg → asks the user if they really want to delete.

3. Delete → removes the currently selected record (highlighted in the DBGrid).

Perfect! Let’s rewrite your frmMyInfo code so that the SQL is dynamic, simple, and Grade 12-
friendly. We’ll make it so you can filter by UserID and even optionally by a status (like “Pending” or
“Delivered”) if you want later.

Dynamic SQL Example in frmMyInfo

procedure TfrmMyInfo.LoadUserDeliveries(OptionalStatus: string = '');

begin

DataModule1.DeliveryQuery.Close;

DataModule1.DeliveryQuery.SQL.Clear;

// Base SQL - always filter by logged-in user

DataModule1.DeliveryQuery.SQL.Add('SELECT * FROM TblDelivery WHERE UserID = :UserID');

DataModule1.DeliveryQuery.ParamByName('UserID').AsInteger := FUserID;

// Optional filter by status

if OptionalStatus <> '' then

begin

DataModule1.DeliveryQuery.SQL.Add(' AND Status = :Status');

DataModule1.DeliveryQuery.ParamByName('Status').AsString := OptionalStatus;
end;

// Open the query to display in the DBGrid

DataModule1.DeliveryQuery.Open;

// Optional: message if no deliveries found

if DataModule1.DeliveryQuery.RecordCount = 0 then

ShowMessage('No deliveries found for this user.');

end;

You might also like