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.
| First add a TDatabase component from the Data Access tab |
|
| 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. | |
| 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;
}
//---------------------------------------------------------------------------
For a copy of the complete code click here .