1
SELECT *
FROM Candidate
WHERE Candidate.Username = CurrentUser

CurrentUser is a global variable (string) which gets the username from the login. When I try to use the SQLQuery with a TDBGrid I get the error, Parameter CurrentUser has no default value.

unit UntVar;

interface

Var Currentuser:String;


implementation

end.

Code for the Global variable.

CurrentUser := EdtUser.Text; Code for when currentuser gains a value.

2
  • This is probably happens, because you not defining CurrentUser and trying to give it to the query. Commented Feb 18, 2014 at 15:59
  • So I would have to give Currentuser a value before I could use it in the query? Commented Feb 18, 2014 at 16:02

2 Answers 2

2

You can use this Code:

  ADOQuery1.SQL.Clear();
  ADOQuery1.SQL.Add('SELECT * FROM Candidate');
  ADOQuery1.SQL.Add('WHERE Candidate.UserName = :CurrUser');
  ADOQuery1.Parameters.ParamByName('CurrUser').Value := CurrentUser;

  ADOQuery1.Open;
Sign up to request clarification or add additional context in comments.

Comments

0

You're not doing anything to assign the content of the global variable to the parameter. (They're not visible or connected in any way. The SQL.Text is simply a string, and the GlobalUser variable is simply a variable holding a string. You need to do something to provide the value to the SQL itself.)

You need something like this:

Query.SQL.Text := 'SELECT * FROM Candidate WHERE Candidate.UserName = :CurrUser';
Query.ParamByName('CurrUser').AsString := CurrentUser;  // Your global var
Query.Open;

1 Comment

As I'm using a TADOQuery I'm unsure where I would put the code or in what format/syntax

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.