Question

I want to build query to db getting parameters from texboxes by clicking button. Have such class definition:

DEFINE CLASS myForm as Form
Height = 200
Width = 300
visible = .t.
ADD OBJECT insertBut AS COMMANDBUTTON;
WITH Caption = "insert", width = 70, height = 20, top = 165, left = 10

ADD OBJECT lbl1 as label WITH caption = 'Title', left = 10, top = 10
ADD OBJECT text1 AS TEXTBOX WITH left = 10, top = 25
ADD OBJECT lbl2 as label WITH caption = 'Amount', left = 10, top = 45
ADD OBJECT text2 as textbox WITH left = 10, top = 60
ADD OBJECT lbl3 as label WITH caption = 'Price', left = 10, top = 80
ADD OBJECT text3 as textbox WITH left = 10, top = 95
ADD OBJECT lbl4 as label WITH caption = 'Manufacturer id', left = 10, top = 115
ADD OBJECT text4 as textbox WITH left = 10, top = 130

ADD OBJECT lbl5 as label WITH caption = 'Id', left = 120, top = 10
ADD OBJECT text5 as textbox WITH left = 120, top = 25

PROCEDURE insertBut.click
    USE stock
    INSERT INTO stock (title, price, amount, man_id) values(text1.text, text3.text, text2.text, text4.text)
    browse
ENDPROC

ENDDEFINE

and procedure that i call via command line

PROCEDURE tform
t = CREATEOBJECT("myform")
t.show
READ events
return
ENDPROC

After I clicked button insertBut I've got an error "alias TEXT1 is not found". What am i doing wrong?

Était-ce utile?

La solution

insertBut.click runs in the context of myForm.insertBut, not myForm, which has text1 et al.

You need to prefix the calls to the form's other objects with THISFORM.


A better solution, btw, is to data-bind the text boxes to either a cursor or a data object, so that you get a better seperation of concerns. You can either add a custom object explicitly in the class description, or just launch or create a cursor directly.

Either open or check for the stock cursor on the Init method of myForm, and then just set each control's dataSource property as you create it. For example:

ADD OBJECT text1 AS TEXTBOX WITH ;
  left = 10, ;
  top = 25, ;
  dataSource = 'stock.title'
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top