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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

open another form in button click event in data add mode

Status
Not open for further replies.

alazarbaharu

Instructor
Apr 17, 2015
11
GB
Hello Guys i need help on how to open another form at button click event in Add Mode. To elaborate my question
i have form called frm1 and i have another form frm2 which has been created by using the form wizard and its having buttons like data navigation, add, find, edit and delete. Inside frm1 i have button and what i want is when this button clicked frm2 will be opened and inside frm2 i need users to enter data without clicking the add button(i.e i need the cmdadd to be executed when i clicked the button on frm1). Thank You
 
First, you'll need to place another button on your Frm1. The wizard doesn't do this for you. Just open the form in the form designer, click the Command Button button (in the form design toolbar), and click on the form roughly where you want the new button to appear.

(If the above is unfamiliar to you, I suggest you go back and learn the fundamentals of the form designer before going any further. The Help topics are a good place to start.)

Next, you need to write a line of code in the Click event of your new button. Double-click on that button (in the form designer). You will see a code edit window. At the top of that window, there is a drop-down labelled "Procedure". Make sure that is set to "Click".

Type this code:

[tt]DO FORM Frm2[/tt]

and then save the form.

When you run Frm1, click on the new button. You should see Frm2 opening.

You said you want Frm2 to open "inside" Frm1. That doesn't make much sense. After you do the above, both forms will be open at the same time, but they will be independent forms - not one "inside" the other. If that's not what you want, come back.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
hello Dear MikeLewis, thank you for your quick reply i have created the form and inserted button from the tool box inside frm1 and i am able to open frm2 when the button inside frm1 is clicked, however when i clicked the button frm2will be opened and it was in read only mode till i click the ADD button inside frm2 even the cursor is not blinking inside the text box. what i want is when form two opened i want the from to be ready fro data entry without the clicked the add button and the cursor should get focused on the first textboxes according to the tab order and that's what i wanted to ask.Thank You
 
I don't know enough about the Form Wizard to know how to automatically open a form in Add mode. But it should be easy to figure out. At its simplest, you can place some code in the Init method to call whatever code is in the Click event of the Add button.

So, in the Init method of Frm2, you first need to add:

[tt]DODEFAULT[/tt]

This ensures that any existing code (at the class level) is executed (this line might have already been inserted by the Wizard).

Next, add a line similar to the following:

[tt]THISFORM.cmdAdd.Click[/tt]

I'm assuming here that [tt]cmdAdd[/tt] is the name of the Add button. If it is not, adjust the above code accordingly. I'm also assuming that the button is located directly on the form's surface. If it is in a container, such as a pageframe or a command button group, you will have to adjust the reference to include the full containership path.

Putting the form in Add mode should also give focus to the first editable control. If it does not do that, you can add the following line to the Init:

[tt]THISFORM.text1.SetFocus[/tt]

Again, adjust the name ([tt]text1[/tt]) and its containership path as necessary.

Now, having said all that, I have to ask why you want to do things this way. If Frm1 has its own Add button, why do you want to launch another form to add data? I can't see any benefit in doing that.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
What you describe is like wanting a parent forms Add button create a new form instance and go directly into add/edit mode there, but that means you took the add button away or reimplemented it in your parent form. It's wishful thinking you can change the wizard forms to work in slightly different ways. You'll end up in a mess you can't control when you try to impose a new idea and a new way of thinking to the forms as created by the wizard.

You should either get used and learn, how the created forms are intended to be used - each form has "his" add button, you don't cause an added new record frmo an outside parent form, for example. Or you develop forms from scratch and add your own logic to it. From the way VFP works, it makes more sense to have a form editing a table it maintains and design it to edit any row, than having a separate new record form and a edit record form, like you try to implement.

Bye, Olaf.
 
alazarbaharu said:
i have another form frm2 which has been created by using the form wizard

I recommend that you quit using the Form Wizard to create forms.
Too often the Wizard adds things you do not want or really need and won't add things you do want.

Instead of using the Wizard, you should create your Forms manually.
In that way:
1. You learn how forms work MUCH better.
2. You will get your form to have only what you want and need.

If you are not familiar with building VFP Forms manually you might want to look at the free, online VFP Tutorial Videos
Creating a Form Manually​

Good Luck,
JRB-Bldr
 
Thanks Mike lewis your suggestions worked for me and i have a clear understanding now on how to do it and i have also understood its better to use my own form codes instead of using the wizards. Thank you
 
its better to use my own form codes instead of using the wizards

Yes, because such coding as suggested by Mike hints on the generated forms are not backed by a good class design.

Compare the suggested change of calling [tt]THISFORM.cmdAdd.Click[/tt] to automation of a mail client: You don't program [tt]Mailclient.cmdNewMail.Click[/tt], you program something like [tt]oMail = oMapiclient.CreateMail()[/tt] or create a new CDO.Message object.

Code doing something should not imitate "clicks" you would do as a user, but work on the underlying concept of the business logic, it should be able to add a new record via an add() or new() method of the data access layer class managing the entity you want to work on, for example. The wizards are not working with such a class design, they lack such concepts and work directly on DBFs via their workarea. The concept of the forms is not well designed for maintenance and extensibility, but in itself very completely implemented, eg you just first start an employee form and look at the data of the first employee, then click Add and add/enter the data of a new employee.

It's another way of thinking, if you take it as is, you can work with the wizard forms, but don't expect enjoying to expand on what the forms offer and modify them to your needs.

Bye, Olaf.
 
Olaf said:
Yes, because such coding as suggested by Mike hints on the generated forms are not backed by a good class design.

I completely agree with that. Personally, I would normally never do anything like [tt]THISFORM.cmdAdd.Click[/tt]. Much better to put the Add functionality in a separate method, and to call that both from the cmdAdd's Click event, and from wherever else you want that functionality. I only made my suggestion for simplicity and to help the OP get started.

The best advice is not to go anywhere near the form wizard. As others have said, the forms that it generates are obscure, difficult to understand, and not particularly well-designed. Nor do they help you learn how to create your own forms.

Mike






__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
again i wold like to say thank you and i want you to suggest me a good tutorial that can be used to learn how to cretae my own forms with data navigation and data manipulation buttons as available in the wizard form.
 
I have already given you to free, online VFP video tutorial link:
And I even pointed you to one of the many videos which might be of special interest to your question
Creating a Form Manually​

To do your data navigation you merely create your own buttons with Forward or Back arrow icons on them and then in the Click Method you write your own code to move the record pointer of your desired table.

However, you haven't said what you want to do once you have moved your record pointer.
Do you want to change something in a Grid?
Or what>

Regardless, once you are creating your own Forms, you can do whatever you want without having to get around any code that might be 'inherited' by using a Wizard.

Good Luck,
JRB-Bldr
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top