Link to home
Start Free TrialLog in
Avatar of Anthony Hiscock
Anthony HiscockFlag for South Africa

asked on

Please help me with my INSERT INTO syntax. i keep getting an error and i don't know why.

I am trying to write a simple program to insert boolean and string values into a database using SQL, but i keep getting an error in my INSERT INTO syntax.
I am coding on Delphi 2010.
Here is my code:
procedure TForm2.btnCreateNewProfileClick(Sender: TObject);
VAR
  iNum :  Integer;
  sNum : String;
begin
  if (Length(edtNewName.Text) > 25) or (Length(edtNewName.Text) < 1) then
    ShowMessage('Username must be between 1 and 25 characters long!')      // Name characrter validation
    else
      if (Length(edtNewPassword.Text) > 25) or (Length(edtNewPassword.Text) < 5) then
        ShowMessage('Password must be between 5 and 25 characters long!')  // password validation
        else
          if NOT Ord(edtNewPassword.Text) = Ord(edtPassConfirm.Text) then
            ShowMessage('Passwords do not match!');


  tblUsers.Open;                    //save input to database
  tblUsers.Last;
  inum := tblUsers['profile'];
  inum := inum + 1;
  sNum := IntToStr(iNum);
  qryDatabase.SQL.Clear;
  qryDatabase.SQL.Add('INSERT INTO Users (UserName, Password, Profile, Admin) ');
  qryDatabase.SQL.Add('VALUES ('+ QuotedStr(edtNewName.Text)+','+QuotedStr(edtNewPassword.Text)+',' +QuotedStr(sNum)+ ',' +cbxAdmin.Checked+ ')');     // apparently this is wrong
  qryDatabase.ExecSQL;
  qryDatabase.Open;

end;

Open in new window

Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

How is your table definition, please?
Avatar of Kanti Prasad
Kanti Prasad

Hi

remove cap N so change  sNum := IntToStr(iNum);  to  sNum := IntToStr(inum); and see that your data types in the user table are strings. I am assuming cbxAdmin.Checked is also a string or else change the admin data type accordingly
What error are you getting?

If that is the code then you will always insert the user even if there are problems with the name or password.

mlmcc
I assume Profile is a numeric field, so you need to removed the QuotedStr() invocation from the
QuotedStr(sNum)
expression
Because we don't know table definition ... we can assume only. I think that Admin field should be:
QuotedStr(cbxAdmin.Checked)
ASKER CERTIFIED SOLUTION
Avatar of Geert G
Geert G
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think that nobody is waiting except for asker response ... if he saw these comments (at all).
Avatar of Anthony Hiscock

ASKER

I see people asking for my table definition, I'm not sure what exactly that is though? I only started studying IT recently so i guess I still have much to learn.
studying IT ?
that's like studying medicine ...

IT and Medicine have 1 thing in common : they have a lot different kinds of knowledge bases
programming, databases, hardware, software, networking ... just to name a few top items
off course each item, holds a load of specialties within, like Delphi in programming


we can only help you on your long path ... :)
I was using Microsoft Access 2010. To specify my studies would have been irrelevant to my problem, i know it is a broad field, in year 1 we do many different things; photoshop, flash, c#, networking, databases etc. specialising later. My goal for now is software engineering and going into either security or gaming... Still weighing the odds. Nevertheless thank you Geert Gruwez your first two comments helped :)
Thanks, everyone. I will be using this website a lot from now on. Hopefully i can start contributing as I learn more.