Zen AM
Zen AM

Reputation: 31

Access violation while trying to call a procedure from another Unit

I have two different units in Delphi. The first unit has a procedure named ApplyUpdates and all it does is run a query.

I need to access this procedure from another unit, and when I do so I get an error message that says "Access violation"".

The procedure in the first unit is:

procedure TForm1.ApplyUpdates ( var AppType: string);
begin
  qryApplyUpdates.ParamByName('type').DataType := ftString;
  qryApplyUpdates.ParamByName('type').ParamType := ptInput;
  qryApplyUpdates.ParamByName('type').AsString := AppType;
  qryApplyUpdates.ExecSQL;
end;

From the second unit I call this procedure as:

var
  UserForm: TForm1;
begin
    UserForm.ApplyUpdates (AppType );
end;

When I debug it, it stops right at the first line of the procedure.

My question is: What am I doing wrong that I cannot access this procedure from the first unit?

Upvotes: 0

Views: 262

Answers (1)

Ken White
Ken White

Reputation: 125669

If the form is autocreated, don't use the local variable at all:

// The default declared variable for an autocreated form is the classname without the prefix
Form1.ApplyUpdates(AppType);

If the form is not autocreated, you have to create the form before you can use it.

var
  UserForm: TForm1;
begin
  UserForm := TForm1.Create(nil);
  try
    UserForm.ApplyUpdates(AppType);
  finally
    UserForm.Free;
  end;
end;

Upvotes: 2

Related Questions