I am currently trying to get the text from a DBLookUpComboBox but it wont let me retrieve it i have a number and 2 strings in the box which is an ID and first and last name. I cannot retrieve anything from the box unless i use a edit box but i cannot imput it into a database which is using the type number.
Please help if you need more info or example of the code i can do this.

I am currently trying to get the text from a DBLookUpComboBox but it wont let me retrieve it i have a number and 2 strings in the box which is an ID and first and last name. I cannot retrieve anything from the box unless i use a edit box but i cannot imput it into a database which is using the type number.
Please help if you need more info or example of the code i can do this.

Dear My Friend
your question contain of tow parts
1- using DBLookup Combobox for retrieving data
2- using Edit box for Input data to database
for first part of your question I say that DBLookupComboBox can not retrieve data from database automatically and don't let you to put data into its text property at run time by program code for example you can not write "DBLookupComboBox1.Text:=table1.FieldByname('firstName').asstring;"
in your Program. but you can use "Volga" Components.
Volga Components have a component Named VolgaDBEdit that can use as DBLookupComboBox it have some features that may be helpful for you

about second part of your question I Write sample Code as below

Table1.insert; //Or Table1.Edit;
Table1.FielsByname('ID').asstring:=Trim(Edit1.Text);
Table1.FielsByname('FirstName').asstring:=Trim(Edit2.Text);
Table1.FielsByname('LastName').asstring:=Trim(Edit3.Text);
Table1.Post

For Retrieving data from Data Base you can use this Below code

Edit1.Text:=Table1.FieldByname('ID').asstring;  
Edit2.Text:=Table1.FieldByname('FirstName').asstring;  
Edit3.Text:=Table1.FieldByname('LastName').asstring;

if you have any problem with this codes Please tel me More information For Example :
What is your Data Base engine , What is the Error message etc.

What i am looking for is a way of getting one peice of data out of the DBLookUpComboBox this is the code i am using

procedure TForm6.Button1Click(Sender: TObject);
begin
adotable1.Close;
with adoquery3 do
begin
  active:=false;
  SQL.Clear;
  SQL.Add('insert into StudentPayments(Student_ID)' +
          'values(:p1)');
  parameters.paramvalues['p1']:=DBLookupComboBox1.Text ;  
  execSQL;
end;
adotable1.active:=true;
end;

Once executed the error i am getting is "Data Type Mismatch In Criteria expression." The parameter is simply defined as "p1" with a value of "%"

Also about the Volga Component where can i find this? and woudl i go about installing it?

Edited 6 Years Ago by HelpMeIT: n/a

Dear My Frind
you can change your code to one of the tow below code
1- Using a String Variable

procedure TForm6.Button1Click(Sender: TObject);
Var
    STD_ID:String;
begin
    STD_ID:=Trim(DBLookupComboBox1.Text);
    adotable1.Close;
    with adoquery3 do
    begin
        active:=false;
        SQL.Clear;
        SQL.Add('insert into StudentPayments(Student_ID)' +
        'values(:p1)');
        parameters.paramvalues['p1']:=StrToInt(Trim(STD_ID)) ;
        execSQL;
    end;
    adotable1.active:=true;
end;

2- Directly Use DBLookupCombobox

procedure TForm6.Button1Click(Sender: TObject);
begin
    adotable1.Close;
    with adoquery3 do
    begin
        active:=false;
        SQL.Clear;
        SQL.Add('insert into StudentPayments(Student_ID)' +
        'values(:p1)');
        parameters.paramvalues['p1']:=DBLookupComboBox1.KeyValue;
        execSQL;
    end;
    adotable1.active:=true;
end;

In this method at first you must assign Corresponding field to Key Field of DBLookupComboBox1 by same type of STudent_ID Type (For Example "Numeric")
All of tow mwthod is tested and work properly .

About Volga DB Library : It is a Free Component and you can download it from this address http://wareseeker.com/screenshot/volgadb-controls-library-3.2.exe/385330

Hi,

I haven't used paramvalues till now, so I write my way to do this:

procedure TForm6.Button1Click(Sender: TObject);
begin
adotable1.Close;
with adoquery3 do
begin
  if Active then Close; 
  SQL.Clear;
  SQL.Add('insert into StudentPayments(Student_ID)' +
          'values(:p1)');
if StrToIntDef(DBLookupComboBox1.Text, -1) <> -1
  Parameters.ParamByName('p1').Value:= StrToInt(DBLookupComboBox1.Text)   //value is a variant, but, to be sure you will not get any error, make the explicit conversion
else 
   begin
        //some safety code
        Exit;
   end;
  execSQL;
end;
adotable1.active:=true;
end;

Cheers,
Ionut

Edited 6 Years Ago by Ionelul: n/a

Thanks fayyaz,
i used the second solution and it worked perfectly!
any ideas on how to insert a data using similar code as above only selecting a date from a edit box? im using strtodate to insert but it still isnt working?

procedure TForm2.Button1Click(Sender: TObject);
begin

adotable1.Close;
with adoquery1 do
begin
  active:=false;
  SQL.Clear;
  SQL.Add('insert into StudentDetails(StudentFirstNameForeign,StudentLastNameForeign,' +
          'StudentFirstNameEnglish,StudentLastNameEnglish,' +
          'StudentsForeignAddress,StudentsEnglishAddress,DOB,Gender)' +
          'values(:p1,:p2,:p3,:p4,:p5,:p6,:p7,:p8)');
  parameters.paramvalues['p1']:=Edit1.Text ;
  parameters.paramvalues['p2']:=Edit2.Text ;
  parameters.paramvalues['p3']:=Edit3.Text ;
  parameters.paramvalues['p4']:=Edit4.Text ;
  parameters.paramvalues['p5']:=Edit5.Text ;
  parameters.paramvalues['p6']:=Edit6.Text ;
  parameters.paramvalues['p7']:=strtodate(trim(DBEdit1.Text)) ;
  parameters.paramvalues['p8']:=ComboBox1.Text ;
  execSQL;
end;
adotable1.active:=true;


end;

The line that is causing a problem is

parameters.paramvalues['p7']:=strtodate(trim(DBEdit1.Text)) ;

i have also tried just

parameters.paramvalues['p7']:=DBEdit1.Text ;

as i do have a DBEdit which is connected to the table and the date field unfortunatly i cannot get it to insert the date i recieved an error in executing with the second code and using the first code i am recieving an error that it is not a date but the date i am inserting is 25/02/1910 i have also tried other dates but they will not work.

thanks

Edited 6 Years Ago by HelpMeIT: n/a

Dear My Friend
your code is Correct But I should hint you tow points
1- When you use SQL Statements(that I think it is a very good method for working by Database) you should better to use MaskEdit Instead Edit Or DBEdit for user interface
2- the error that you detect to is not because of your Code rather than it is because of Date Display Format of your Computer for Example on my Computer the date display format is "yyyy/mm/dd" and "1910/2/25" is a correct date but "25/2/1910" is not a correct date
you can change date format from your "Control panel->Regional and language option->Regional options->Customize->Date"

Best regards

I am wondering where can i find maskedit?
also i have tried changing the date options but they are correct after following your instructions and it still didnt work

I am wondering where can i find maskedit?
also i have tried changing the date options but they are correct after following your instructions and it still didnt work

Dear Friend

1- about MaskEdit you can find it in Aditional palet of Delphi IDE

2- About your problem with date format although I Dont know what is your Data base System (SQLServer , Paradox , Oracle ,...)? but I still think that by seting date format in control panel correctly your program will work properly
you should set "calendar Type" , "Short date format" and "Long Date format" in control panel. I test it on SQLServer Tables and get a good result.

The date is now working after following your advice again i did the same thing but it has decided to work.

also i found the mask edit but i cannot see why i would replace dbedit with maskedit? as i cannot link maskedit to my database and what is the advantage of using the maskedit?

Dear friend

the usage of DBEdit is for entering data to a table immediately for example when you have a table with ID,FName,LName you can use 3 DBEdit on your form and connect them to tables fields.
with this method if you enter data to DBEdits the data will save to table immediately and it is not necessary to writing any code for inserting or editing data in to table
by this method you can locate to a record (the contents of corrent record fields will be appear in DBEdits) and chage DBEdits values this change replace to Table immediately it is very easy But I don't like that becouse by this method you can not control validation of Entered data Befor Saving to table

I saw that you use SQL Statement for inserting Data to table so I think that you dont need to use DBEdit you can use MaskEdit
The advantage of mask edit is that you can format input data for example you can configur Editmask Property by Typing ####/##/##
for Date Data entry this seting couse that user can not enter alphabet in date position.
MaskEdit can not connect to Database tables but it have meny validation control that you can use it
however using MaskEdit is only an offer and you can use anythig else that is easy for you.

Edited 6 Years Ago by fayyaz: n/a

I was wondering if when using a MaskEdit which i am considering using would i be able to show £#####.## but only select the numbers? like would i select the '£' or would i have to trim it to remove the '£'?

I was wondering if when using a MaskEdit which i am considering using would i be able to show £#####.## but only select the numbers? like would i select the '£' or would i have to trim it to remove the '£'?

If you want to show £ in MaskEdit but it don't appear in entered data you must remove it before using entered data for example you can use this Code before using entered data

TempVar:=Trim(MaskEdit1.Text);
TempVar:=Copy(TempVar,2,Length(TempVar)-1);

(TempVar Is a String Variable)
By this Code you can remove £ from entered data.

I decided to just use a label and put it to the side as it helps to avoid more errors.

I am getting a problem after using a parameter twice in the program and it keeps giving me an error. the first time it works without a problem but the second time i receive an error telling me that the parameter is imcomplete amongst other reasons which i cannot remember off the top of my head but if you need more information ask. here is the code that is having the problem.

procedure TForm13.Button1Click(Sender: TObject);
begin
dbgrid4.visible := true;
q1.parameters.parambyname('p1').value:='%';
q1.parameters.parambyname('p2').value:='%';
with q1 do
begin
close;
sql.add('Select * FROM StudentDetails ' +
        'WHERE StudentFirstNameForeign = :p1 ' +
        'AND StudentLastNameForeign = :p2');
parameters.parambyname('p1').value:=edit2.text;
parameters.parambyname('p2').value:=edit3.text;

open;
end;

thanks

I decided to just use a label and put it to the side as it helps to avoid more errors.

I am getting a problem after using a parameter twice in the program and it keeps giving me an error. the first time it works without a problem but the second time i receive an error telling me that the parameter is imcomplete amongst other reasons which i cannot remember off the top of my head but if you need more information ask. here is the code that is having the problem.

procedure TForm13.Button1Click(Sender: TObject);
begin
dbgrid4.visible := true;
q1.parameters.parambyname('p1').value:='%';
q1.parameters.parambyname('p2').value:='%';
with q1 do
begin
close;
sql.add('Select * FROM StudentDetails ' +
        'WHERE StudentFirstNameForeign = :p1 ' +
        'AND StudentLastNameForeign = :p2');
parameters.parambyname('p1').value:=edit2.text;
parameters.parambyname('p2').value:=edit3.text;

open;
end;

thanks

you must add bellow code after Close; and befor sql.add(..
sql.Clear;

This article has been dead for over six months. Start a new discussion instead.