Access violations and datatype mismatch

96 Views Asked by At

I'm encountering a puzzling issue with my code where I'm getting two different errors on the same line. Initially, I received a

First chance exception at $761F8462. Exception class EOleException with message 'Data type mismatch in criteria expression'. Process Project1.exe (5588)

error. To address this, I modified the SQL statement to hardcode the phone numbers from which I want to retrieve associated data from the database.

However, after making this change, I started getting an "Access violation" error in the unit. The error seems to be related to the first data called in the form (e.g., GetName). Strangely, when I commented out this line on both forms, the error simply moved to the next line.

Just to provide some context: all the data in the database is stored as String (specifically, ShortText), and the profile picture is stored as an image path. It's worth noting that this code structure works perfectly fine on other forms, so I'm struggling to understand why it's causing issues in this particular case.

Any insights or help in troubleshooting this issue?

unit AccountDetails;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, Buttons, StdCtrls, ImgList, jpeg, pngimage, accdetails, DM;

type
  TFrmAccountDetails = class(TForm)
    Image1: TImage;
    Img_Profile: TImage;
    Lbl_BioHeading: TLabel;
    Lbl_Phone: TLabel;
    LblBio: TLabel;
    LblName: TLabel;
    Shape1: TShape;
    Panel1: TPanel;
    Image2: TImage;
    lbl_EventsHosted: TLabel;
    procedure Image2Click(Sender: TObject);
   //procedure FormShow(Sender: TObject);
    procedure getinfo;
    procedure assignvalues;
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FrmAccountDetails: TFrmAccountDetails;
  User: Tuser;

implementation

uses EventDetails;
{$R *.dfm}

procedure TFrmAccountDetails.assignvalues;
begin
lblName.Caption:=user.GetName;  (*** Author Kyle most likely means this line. ***)
lbl_phone.Caption:='Phone Number: '+user.GetPhone;
LblBio.Caption:='';
lblbio.Caption:=user.getBio;
img_profile.Picture.LoadFromFile((user.getProfilepic));
lbl_eventshosted.Caption:='Events Hosted: '+user.getnum_hosted;
end;

procedure TFrmAccountDetails.FormShow(Sender: TObject);
begin
getinfo;
assignvalues;
end;

procedure TFrmAccountDetails.getinfo;
var
  FName, LName, ProfilePic, BIO, Host_Phone, Hosted_events: string;
begin
  User := nil;
  with DM.DataModule1 do
  begin
    adouser.Close;
    adouser.SQL.Clear;
    adouser.SQL.Text :=
      'SELECT First_Name, Last_Name, Phone_Number, ProfilePic, BIO, Events_Hosted ' +
      'FROM tblUsers WHERE Phone_Number = ' + QuotedStr(EventDetails.FrmDetails.Host_Phonenum);

    // Execute the query and retrieve all the rows into the dataset
    adouser.Open;
    begin
    adouser.First;
    if not adouser.IsEmpty then
    begin
      FName := adouser.FieldByName('First_Name').AsString;
      LName := adouser.FieldByName('Last_Name').AsString;
      ProfilePic := adouser.FieldByName('ProfilePic').AsString;
      BIO := adouser.FieldByName('BIO').AsString;
      Hosted_events := adouser.FieldByName('Events_Hosted').AsString;
      Host_Phone := adouser.FieldByName('Phone_Number').AsString;
     User := Tuser.Create(FName, LName, ProfilePic, BIO, Host_Phone, Hosted_events);
    end;
    end;
  end;
end;

procedure TFrmAccountDetails.Image2Click(Sender: TObject);
begin
  Close;
end;

end.

unit AccDetails;

interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, ExtCtrls, jpeg;


  type
  TUser = class
  private
    FProfilePic: string;
    FBio: string;
    FFirstName: string;
    FLastName: string;
    FPhone: string;
    FNum_Hosted: string;

  public
    constructor Create(sFirstName, sLastName, sPhone, sProfilePic, sBio, sNum_Hosted: string);
    function GetProfilePic: string;
    function GetBio: string;
    function GetName: string; // Updated function name
    function GetPhone: string;
    function GetNum_Hosted: string;
  end;

implementation

{ TUser }

constructor TUser.Create(sFirstName, sLastName, sPhone, sProfilePic, sBio, sNum_Hosted: string);
begin
  FProfilePic := sProfilePic;
  FBio := sBio;
  FFirstName := sFirstName;
  FLastName := sLastName; // Fixed typo in variable name
  FPhone := sPhone;
  FNum_Hosted := sNum_Hosted;
end;

function TUser.GetProfilePic: string;
begin
  Result := FProfilePic;
end;

function TUser.GetBio: string;
begin
  Result := FBio;
end;

function TUser.GetName: string;
begin
  Result := FFirstName;
end;

function TUser.GetPhone: string;
begin
  Result := FPhone;
end;

function TUser.GetNum_Hosted: string;
begin
  Result := FNum_Hosted;
end;


end.
0

There are 0 best solutions below