Login Event

Introduction

After you have worked with databases for a while, you may not want Builder to prompt you for the database user name and password. You may also want to control the password-user connection and determine where to find the password. To do this you must add a Database object to your form and connect that object with your tables. After this is done, the Database object determines if the login dialog will appear and if it does appear how the program will handle the information collected.

Making the Connection

First add a TDatabase component from the Data Access tab . Next set the properties of this component in the Object Inspector. You should set the name of the TDatabase, the alias, create a database name (that associates this component with the datasets in your program), and if you don't want the login dialog to appear set login to false, and the login dialog is disabled.
Before this takes effect, you have to associate your database component with your dataset. In the case pictured here, the dataset is a TTable. You need only select the database name you created for your TDatabase component. Now any login events will be handled by the Database component.

The OnLogin Handler

You will want to create a dialog that retrieves the user name and password. Remember to set the PasswordChar to * in the Password edit box. One problem with this dialog, if you load the database when you initialize your program, the create function for the dialog crashes. To fix this, use the project options form tab and move this dialog from Auto Create to Make Available. This means in the OnLogin event handler you create the dialog and then process the information (this is up to you). You may then set the password and user name to the values entered or just to blanks if you want default passwords for Access.

The following is a sample of an OnLogin handler.

void __fastcall  TMainForm::Database1Login(TDatabase *Database,
      TStrings *LoginParams)
{
//handle the login  
   TPasswordDlg *PasswordDlg=new  TPasswordDlg(this);
   PasswordDlg->Name->Text="" ;
   PasswordDlg->Password->Text ="" ;
   if (PasswordDlg->ShowModal()==IDOK)
   {
     //process the password information 
     LoginParams->Values["user name" ] = PasswordDlg->Name->Text;
     LoginParams->Values["password" ]= PasswordDlg->Password->Text;
   }
   delete  PasswordDlg;
}
//---------------------------------------------------------------------------

Complete Code

For a copy of the complete code click here .