[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

count the results with combobox select

Posted on 2011-05-03
43
Medium Priority
?
399 Views
Last Modified: 2012-05-11
Hi, when i select "sale ok" i am adding to the database in field [13] called "testing 33".
this adds fine
So please dont include that in the code.

I want to be able to do this following:  I know the code is wrong but it willhelp explain what i need.

if
 DB5.Text=('Sale ok') and the labelsales:=('test33')
 then begin
form1.label1.caption :=  form1.adotable2.Fields[13].AsString; < must count
  end;

==============

I want it so when it adss to the label it counts.. so if i add 10 sales thelabel reads 10
please dont use posex or query as i need this part simple.#

thnx




if i have to edit the sale and give a refund, i will make change the status of sale ok to something.. and update. I then want it to take away the count igave it in label 1. for example if i sell 10 and refund someone,i change status of sale ok, label needs to say sold 9
0
Comment
Question by:boycoder
  • 23
  • 9
  • 7
  • +2
43 Comments
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 35512339
you don't have english as your main language ???
your question is hard to follow !

and rename your components to something understandable

like
DB5 > dbSaleStatus
Label55 > lblTest33
AdoTable2 > tableSales
label1 > lblNumRecords

your identation is very odd

if dbSaleStatus.Text= 'Sale ok' and lblTest33 = 'test33' then
begin
  lblNumRecords.Caption :=  tableSales.Fields[7].AsString;
end;

0
 
LVL 4

Expert Comment

by:fromer
ID: 35512410
var
  code : Integer;
  currentValue : Integer;
begin
   Val(form1.label1.caption, currentValue, code);
  if code <> 0 then
  begin
    currentValue := 0;
  end;

  form1.label1.caption := IntToStr(currentValue + 1);

  or

  form1.label1.caption := currentValue + form1.adotable2.Fields[7].AsInteger;
end;


if you are assigning form1.label.caption := '0' at the beginning,
you can skip the code
   Val(form1.label1.caption, currentValue, code);
  if code <> 0 then
  begin
    currentValue := 0;
  end;


it's really hard to understand what you want..you may give more information about what you are trying to do..


0
 

Author Comment

by:boycoder
ID: 35512427
Hi thanks, this keyboard is very stick sorry about that and i am doing so many things at once, i should be more clear. Anyway, i tried that and it didnt work. Please dont change the components i use as they are in my program about 100 times over.

Here is the code you gave me with the correct components and the error i get is:
incompitible types string and tlabel


if DB5.Text= 'Sale ok' and labelsales = 'test33' then
begin
  form1.label1.Caption :=  adotable2.Fields[13].AsString;
end;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:boycoder
ID: 35512528
Ok i will explain what i want to do and you may have a better way, simple as possible.
I am trying to do the following:

when i select "sale ok" and also if labelsales has the word "test33"
then and only then add to my form1.label1 a value.
else.. do nothing

Each time i add a sale (when i click sale ok ) it adds another value to the form1.label1.

So, now if i add 2 sales the label would read you have sold 2

0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 35512534
if you are inside the code of TForm1
then remove all the usages of Form1.

Instead use Self.

this is a common mistake with beginning delphi programmers
(also with experienced ones ...)

in my opinion using any reference to a form variable is a mistake
(
except in the creation of a instance of a class)

Why do you use () around text ????

if DB5.Text='Sale ok' and labelsales = 'test33' then
begin
  label1.caption :=  adotable2.Fields[13].AsString; < must count
end;
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 35512553
var temp: string;
if DB5.Text= 'Sale ok' and labelsales.caption = 'test33' then
begin
  temp := form1.label1.Caption;
  if Temp = '' then Temp := '0';
  form1.label1.Caption :=  IntToStr(StrToInt(Temp) + 1);
end;
0
 
LVL 4

Expert Comment

by:fromer
ID: 35512640
DB5.Text=('Sale ok') and the labelsales:=('test33')
 then begin
 form1.label1.tag := form1.label1.tag + 1;
form1.label1.caption :=  form1.adotable2.Fields[13].AsString + IntToStr(form1.label1.tag);
  end;

i think you want this, cause you mentioned about posex :)...
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35512826


if (DB5.Text= 'Sale ok') and (labelsales.caption = 'test33') then
begin
   form1.label1.Caption :=  IntToStr(StrToIntDef(form1.label1.Caption, 0) + 1);
end;
0
 

Author Comment

by:boycoder
ID: 35512861
Thanks for the code both, but i amusing form1.label1 as i am working on form13.

I tested both codes and both give different error's.
Thanks for your help so far, but still not working or compiling Cheers

btw, i put () around text when i want to add  a further explanation, just basic english grammar.
0
 

Author Comment

by:boycoder
ID: 35512874
testing new code now
0
 

Author Comment

by:boycoder
ID: 35512978
thanks the code works great, however if i click sale ok and click submit, it obviously counts correct.

But, if i click on the customer again and read some details and re-click the button again it is going to add it twice as a value. Is there a way that once i click sale ok on a customer's form, it doesnt read it again if i reclick on him and hit the submit button again.

I have another field of a username? would this help? so we can add the sale ok + labesales + username ?

Would that work?
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35512987
Hi Boycoder,
Let's take it step by step please.
I assume DB5 is a combobox and the labelsales is a Label.
You want to check if "DB5" is 'Sale OK' and if the "labelsales" is 'test33' but what are the possible values that you might have in DB5 and labelsales?
What I am trying to know here is this do those words 'Sale OK' and 'test33' exist as a whole word or among other words? If yes, then the following code should show you the message 'Increase one'.
if (pos(uppercase('Sale ok'), uppercase(DB5.Text))>0) and (pos(uppercase('test33'), uppercase(labelsales.caption))>0) then
  begin
    showmessage('increase one');
  end;

Open in new window


Do you get that message?

Now this is a step 1, let us work on it and then we move to the second part (increase and decrease) OK?
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35513030

I think you need to re-evaluate your database design. You want to store the total for each customer but you can not do it on a label. you atleast need a filed in your database called TotalSales

In which case you will have your code as

if (DB5.Text= 'Sale ok') and (labelsales.caption = 'test33') then
begin
   form1.label1.Caption :=  IntToStr(form1.adotable2.FieldByName('TotalSales').AsInteger + 1);
end;

This way the total is stored for each customer separately.
This is my suggestion so you dont have to do all this unnecessary conversions from string to integer and your data is stored properly
0
 

Author Comment

by:boycoder
ID: 35513257
Jimmy,thanks mate i will check yours very soon as i actually want to test what you did as i can learn alot from that, so much. Cheers.

Ewangoya, thanks. That is the right idea, and yes i have a databse entry for that. However, i test it and when i click submit i have the error "test33 is not a valid integer value"

thanks very much for your input mate. Nearly there :)
0
 

Author Comment

by:boycoder
ID: 35513279
The code i am using to keep you updated is:

if (DB5.Text= 'Sale ok') and (labelsales.caption = 'test33') then
begin
   form1.label3.Caption :=  IntToStr(form1.adotable2.FieldByName('sales').AsInteger + 1);
end;
0
 

Author Comment

by:boycoder
ID: 35513287
However in the "sales" part of the table, the caption in the properties obj inspector is "Type of sale" if that helps.
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 35513308
your approach is wrong by design

when you i enter a new sale in my system :
  > new sale header record > status > entry
  > add new sale items
  > after finishing, change the sale header status to finished

> after the sale status is changed to finished, use a trigger to increment a day count for the sales of that day for that user

this is all database work because of the possible multi-user environment
and because the person entering the sale items can jump from 1 terminal to another

don't ever think that somebody will remain on only 1 workstation in a shop
sometimes they have to follow the customer around the shop entering 1 item at a time from different workstations
0
 

Author Comment

by:boycoder
ID: 35513420
This is not a shop its just a small company that i run from my laptop. Its only going to ever stay on the laptop.
I cant use a trigger like that as the query is not reliable for date/time i need to to be accurate.
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 35513460
you can't have it any more accurate or reliable except in a trigger
you really need to work out your design
0
 

Author Comment

by:boycoder
ID: 35513825
The design i have works perfect and is what i need. Ive been using this program i made for over a year now and tweaked to perfection, i just need to do some new updates to it that i am doing in these few weeks. This being one of them.

ewangoya's example looks similar to what i want as labelsales.caption just reads from the tablefield anyway.
0
 

Author Comment

by:boycoder
ID: 35513931
if (DB5.Text= 'Sale ok') and (labelsales.caption = 'test33') then
begin
   form1.label3.Caption :=  IntToStr(form1.adotable2.FieldByName('sales').AsInteger + 1);
end;


Update: If i remove the l(abelsales.caption = 'test33')  and try it , it gives the same error, like it's telling me that the text inside adotabe2.byfieldname('sales') is not a correct integer. The text in there is the same as the labelsales.

0
 
LVL 24

Expert Comment

by:jimyX
ID: 35513990
What is the datatype of the field "Sales" is it Integer or Real?
If it's Real then:
if (DB5.Text= 'Sale ok') and (labelsales.caption = 'test33') then
begin
   form1.label3.Caption :=  FloatToStr(form1.adotable2.FieldByName('sales').AsFloat + 1);
end;

Open in new window

0
 
LVL 24

Expert Comment

by:jimyX
ID: 35514035
>   "The text in there is the same as the labelsales"

Can you show a sample value of what the field "Sales" might be.

Can you show what you have in the label when you do this:
if (DB5.Text= 'Sale ok') and (labelsales.caption = 'test33') then
begin
   form1.label3.Caption :=  form1.adotable2.FieldByName('sales').AsString;
end;

Open in new window

0
 

Author Comment

by:boycoder
ID: 35514100
Yea sure.

I wont bother with the test33 thing, ill just use the actual code.

The field entry inside adotable2.FieldByName('sales') is '3 Months Membership'

You think the "3" is throwing it off as a value ?


labelsales.caption reads from the database inside "sales" when the form is opened. This reads as "3 Months Membership"

0
 
LVL 24

Expert Comment

by:jimyX
ID: 35514634
Is "3" is the number that you are looking for to add 1 to it whenever sales is made?
like when doing "form1.label3.Caption :=  form1.adotable2.FieldByName('sales').Value + 1" you want the form1.label3.caption to be 4. Is that true?

0
 

Author Comment

by:boycoder
ID: 35514697
depending on what the customer wants, "3 Months Membership" or "15 Months Membership" and "1 Months Membership". These are added to the databse when the serial codes are added. These are located inside tabel2 in field "sales". So when i click on for example , a 3 month form to send him a serial code, when the form loads it pulls the text from "sales" into sales.caption. In this example, it is "3 Months Membership"

I want it so, after i fill the customers address in and other info and when "sale ok" is selected from the combobox and i click submit.

It shows on form1.label3 a value so i can keep track of how many i sell.. so each sale i make of a 3 month membership keeps adding up on form1.label3.

10 sales would be label3.caption  10

0
 
LVL 24

Expert Comment

by:jimyX
ID: 35514752
I need more details please.

Does that relate to the value of the field Sales? I mean do you use the "3 month membership" whenever you are increasing the sales counter?

Because what you can do is having a counter for sales and whenever a sales is made you increase the counter. Something like:
var
  counter:integer=0;
...

if (DB5.Text= 'Sale ok') and (labelsales.caption = 'test33') then
begin
  inc(counter);
  form1.label3.Caption :=  IntToStr(counter);
end;

Open in new window


But what happens when you close your application an reopen it, does that label still remember the value in it?
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 35514836
does it have to add these values in memory
or keep a counter even when you shutdown your laptop ?

keep in mind that not naming your components/controls to something better than label1, form1, edit15, adotable624567
will always make people ask questions what each component is for

just imagine when you would give this to somebody else to extend
and have 300 forms with each 20 components on
or ask help on a site like this ... it would be hell
but that's what programming is ... isn't it ?
0
 

Author Comment

by:boycoder
ID: 35515259
Thanks for the help again, noi t doesnt need to keep them in memory.
But, jim when i compile that it gives an error, but when i remove the 0 from the counter:integer=0;
it compiles, counts, but doesnt count above 1. I know i need to keep the correct code just to keep you updated.

I will be refreshing the program each day anyway, so i only need to count sales during the day.
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35515423
>   "but when i remove the 0 from the counter:integer=0; it compiles"

The initialization of the counter should be in the global VAR as follows:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm1 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  counter:integer=0;

implementation

{$R *.dfm}

...

Open in new window


If that is not giving you the result you are expecting, could you please demonstrate your idea by applying it in a scenario where the values are entered and the expected results are shown. For example, show the current values and when and operation (such as sales) is performed update the values showing what's expected to happen.
0
 

Author Comment

by:boycoder
ID: 35515470
That works, thanks very much that is excellent. However, if i relick on the customer to view the details and if i change the combobox from sale ok to another selection, and click update, the caption takes the value away from the label ?

for example, say i have to refund someone after the sale is counted .
0
 

Author Comment

by:boycoder
ID: 35515555
sorry i mean to say "can the caption label change from 2" to "1" if i sold 2 but have to refund someone.
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35515586
For refund use:
  dec(counter);
  if counter >= 0 then  // to avoid negative values unless it is required
    form1.label3.Caption :=  IntToStr(counter);

Open in new window

0
 
LVL 24

Expert Comment

by:jimyX
ID: 35515609
Update:
  dec(counter);
  if counter >= 0 then  // to avoid negative values unless it is required
    begin
      form1.label3.Caption :=  IntToStr(counter);
    end
  else
    counter := 0;

Open in new window

0
 

Author Comment

by:boycoder
ID: 35515718
In cant seem to get it to count again mate, this is the code i am using.


if (DB5.Text= 'Sale ok') and (labelsales.caption = '3 Months Membership') then
begin
  inc(counter);
  form1.label3.Caption :=  IntToStr(counter);
  dec(counter);
  if counter >= 0 then  // to avoid negative values unless it is required
    begin
      form1.label3.Caption :=  IntToStr(counter);
    end
  else
    counter := 0;
0
 

Author Comment

by:boycoder
ID: 35515726
should i add an else end begin in there?
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 35515784
it doesn't have to keep the count in memory
but you don't want a solution which does it in the database ...
  because you don't want it with a query ... or a update statement ...

it's either one or the other

you are getting solutions in memory from jimyX, direction ... but the design is inadequate
you haven't any idea what you really want ... get pen and paper and write down what you expect

you aren't going anywhere if you don't have a clear direction where you want to go
0
 
LVL 24

Accepted Solution

by:
jimyX earned 2000 total points
ID: 35515789
You can't use both of inc and dec in the same place otherwise you get wrong value.
When a sales is made you increase the counter:
if (DB5.Text= 'Sale ok') and (labelsales.caption = '3 Months Membership') then
begin
  inc(counter);
  form1.label3.Caption :=  IntToStr(counter);
end;

Open in new window


And now, where do you click to make refund? Or how do you make refund?
At the moment you refund any sales you decrease the counter:
  dec(counter);
  if counter >= 0 then  // to avoid negative values unless it is required
    begin
      form1.label3.Caption :=  IntToStr(counter);
    end
  else
    counter := 0;

Open in new window

0
 

Author Comment

by:boycoder
ID: 35516022
When i make a refund i use the combobox1.
sale ok is located in there too.

If i select "3 Months Membership" from combobox1 that would refund.
0
 

Author Comment

by:boycoder
ID: 35516037
Sorry when i say combobox1, i mean DB5.TEXT
0
 

Author Comment

by:boycoder
ID: 35516054
if (DB5.Text= '3 Months Membership') then
  dec(counter);
  if counter >= 0 then  // to avoid negative values unless it is required
    begin
      form1.label3.Caption :=  IntToStr(counter);
    end
  else
    counter := 0;


that works!!!
0
 

Author Comment

by:boycoder
ID: 35516062
To all who helped, thanks very much, =)

Jim, thanks very much mate i really appreciate what you have done. Thanks for your expert help in this matter.
0
 

Author Closing Comment

by:boycoder
ID: 35516156
A++++++++++++++++
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question