Jump to content

ADO and Access question!

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
12 replies to this topic

#1
Trix09

Trix09

    Newbie

  • Members
  • Pip
  • 8 posts
Hello all! I have been searching for weeks now for a solution to this and still cannot find any.

I am just trying to learn the basics of connecting to a Access database and sending and retrieving data.

So far, i have my form setup with edit boxes and a button etc, and a simple ADOQuery thrown on the page which is connected to my Access file through the connection string.

So far i have been successful in grabing the data from the database and showing it in a EDIT box by doing this

procedure TForm3.Button1Click(Sender: TObject);

begin

q1.Active:=true;


while not q1.Eof do begin

Edit1.Text:=q1.FieldByName('iName').Text;

Edit2.Text:=q1.FieldByName('iSize').Text;

Edit3.Text:=q1.FieldByName('iColour').Text;

q1.Next;

end;

end;

This works and puts the information into the boxes. But now i am stuck on how do i go about putting data IN the database?

So if someone was to enter something into a EDIT box, and i wanted to take that information and put it into a database, how do i go about this?

Hope this all makes sense! It's driving me crazy now ^^

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You really need to learn SQL. The standard way to get data in and out of a database is to set the SQL string in your ADOQuery and then call either the Open method (for a select statement) or Execute method (for insert, update, etc).
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Trix09

Trix09

    Newbie

  • Members
  • Pip
  • 8 posts
Ok thank you. I have managed after looking around here, to get it to post information into the database, and retrieve it. I'm having one last problem i was hoping you could help me with though.

How would i go about entering data from, lets say edtbox.text into the sql database? Or data saved into a variable into the database? I've been searching google all day with no luck!

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add('INSERT INTO mytable (field1, field2, field3) VALUES (''value1'',''value2'','''+edtbox.txt+''')');
ADOQuery1.Execute;

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
Trix09

Trix09

    Newbie

  • Members
  • Pip
  • 8 posts
So close!!!

I have this

with q1 do begin
  Active:=False;
  SQL.Clear;
  SQL.Add('Insert INTO Marble (Username) VALUES (''+UserName.text+'')');
  ExecSQL;
end;

It runs ok, but whatever i type into the EDT box when its running, does not get saved into the database, Instead it simply saves +UserName.text+ into the database. Doh!

#6
Trix09

Trix09

    Newbie

  • Members
  • Pip
  • 8 posts
Woops just noticed my error as soon as i posted! Was missing some ' signs in there. Works a charm now, thank you SO much you have no idea how much you have helped me. I have spent countless hours on google and in books trying to find this simple answer!

Thanks once again, very much appreciated.

#7
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I'm glad I could help.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#8
Trix09

Trix09

    Newbie

  • Members
  • Pip
  • 8 posts
Hello its me again! Just a quick error that keeps popping up when trying to insert data into 2 tables. It keeps telling me

Syntax Error in INSERT INTO statement

The code i have is this

procedure TFrmRegistration.Button1Click(Sender: TObject);
begin
with q1 do
begin
  Active:=False;
  SQL.Clear;
  SQL.Add('Insert INTO Marble (Username,Password) VALUES('''+EdtUserName.text+''','''+Edit1.text+''')');
  ExecSQL;
end;

Any ideas on this one?

#9
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
What details?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#10
Trix09

Trix09

    Newbie

  • Members
  • Pip
  • 8 posts
How do you mean?

#11
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
It looks like my question got scrambled. What are the details of the error message? Also, how is the table set up?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#12
Trix09

Trix09

    Newbie

  • Members
  • Pip
  • 8 posts
Thats ok, The details i posted was the only details i got from the error message.

However, after hours and hours of trying i think i got it to work by doing this

procedure TFrmRegistration.Button1Click(Sender: TObject);
begin
with q1 do
begin
  Active:=False;
  SQL.Clear;
  SQL.Add('Insert INTO Marble VALUES(''1'','''+EdtUserName.text+''','''+Edit1.text+''')');
  ExecSQL;
end;

That seems to work fine thank god! Might not be perfect though but im just starting!

I've also thrown code together to check if the username and password entered are the same as whats in the database, with this code

procedure TFrmLogIn.BtnConfirmClick(Sender: TObject);
begin
  with q1 do
    begin
        Active:=False;
        Sql.Clear;
        SQL.Add('Select Username from Marble WHERE ID = 1');
        Open;
        Username:=(FieldByName('Username').AsString);
        Sql.Clear;
        SQL.Add('Select Password from Marble WHERE ID = 1');
        Open;
        Password:=(FieldByName('Password').AsString);

    end;
end;


Once again its not perfect but it works! If you have any comments or feedback that would be appreciated!