Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

INSERT textbox value 1

Status
Not open for further replies.

tag141

Technical User
Oct 4, 2003
119
AU
I need my user to input a value into a textbox and when the button is pressed it will insert that value into my db.

Code:
procedure TForm1.Button1Click(Sender: TObject);
begin
ADOQuery1.Active := false;
ADOQuery1.Sql.Clear;
ADOQuery1.Sql.Text := ('INSERT INTO tblStock(reagname) VALUES (' + QuotedStr(Edit1.Text) + ')');
ADOQuery1.ExecSQL;
ADOQuery1.Active := true;
end;

It gives erros and I have tried a number of different ways but to no avail. Any pointers or help would be gratefully taken on board.

TIA
 
It always helps to say what the errors are. Are they compilation errors or run time errors?

The line
Code:
ADOQuery1.Active := true;
is not required. The ExecSQL will insert the record. You only need to set Active to true if the SQL returns a result set.

I would suggest that your table already contains a record having the same key value (NULL ?) as the one you are trying to insert. But this is only a guess as you haven't said what the error is.

Andrew
Hampshire, UK
 
Thanks Andrew. I tried to sort it a different way and it works...to some extent.

Code:
procedure TForm1.Button4Click(Sender: TObject);
begin
  with ADOQuery1 do
  begin
    Active:=False;
    SQL.Clear;
    SQL.Add('Update tblStock set kitcode=''+QuotedStr(code.text)'' where reagname=''AST''');
ExecSQL;
end;
end;

This works in as much as it updates the field kitcode, not with what is entered into the code textbox but with this +QuotedStr(code.text). If I try and remove any of the inverted commas it doesn't like it. What should I be putting in the SQL to get my text from my textbox to go into my field?
 
When posting messages to Tek-Tips, please, please, specify the actual error you get. Words like
... it doesn't like it.
do not indicate if the problem is detected at compile time or at run time. Nor do they indicate the precise error detected.

Anyway, the parameter to your SQL.Add statement is incorrect. Try something like
Code:
SQL.Add('UPDATE tblStock SET kitcode=' + QuotedStr(code.text) + ' WHERE reagame=''AST''');

Andrew
Hampshire, UK
 
Up early or up late? I know I 'should' have posted the error but...the second code didn't produce an error. It did what I wanted it to do but didn't insert what I wanted it to do. Thanks for your timely assistance, have a star and a note to self 'Post errors'
 
As a MVP you know your code. Can I ask you two questions. There are no error messages either!

In a VB SQL you can have part of the SQL like this
Code:
like '%" & txtText3 & "%'"
which (in my SQL text3 is say HOP) searches for *HOP*. So anything that contains HOP will be returned. Is there anything similar in D7?
I am also trying to add a specific number to a field that already contains a number but cannot find information on how to do it. Any pointers? I know I should spend more time over coming my carpal tunnel/RSI and search the net but sometimes you just gotta ask.

Thanks again.
 
LIKE is a keyword which is implemented in all versions of SQL.

% is the wild card symbol in SQL.

so SQL statements such as
Code:
SELECT * FROM table WHERE text3 LIKE "%HOP%"
are valid and are independent of the application language (Delphi, VB and so on).


Use the UPDATE SQL command to update a field.

Andrew
Hampshire, UK
 
Thanks for your help. Where do you learn this stuff? Trial and error or study of some description? So SQL is common across languages? Maybe not all but most of the common ones.

I sorted out my other problem:

This,
Code:
quantity = quantity + 5
should be
Code:
quantity = quantity+5
that worked.

Thanks again for your help.
 
Structured Query Language is THE standard way of querying databases and can be utilised by all programming languages I have used in the past. As Andrew has stated, it is language-independent and is in itself a language (for database interaction). To learn about SQL you could do a search for "SQL Tutorial" in the Google search engine.

A good place to start looking (particularly with regard to web-based languages) is . If you look down the navigation column on the left-hand side you will find a section called "Server Scripting", within which is a section for learning SQL (as it is commonly used by web applications for database interactions).

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Thanks Clive, will take a look. I did a heap of programming in VBA using SQL inside an Access database but I'm sure there must be some subtle differences. I've tried putting in todays date into my field, as I did in the VBA SQL, but it has an error. I WILL sort it out by finding the info...or I'm back here again!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top