Sometimes when using ADO (Active-X Data Object) you will need to know how to connect your ADO project to a database "on-the-fly," i.e. have the user set up the database connection information so your program is not locked into a single provider or database.
Microsoft has provided an API called
IDBPromptInitialize:
romptDataSource. This API opens the Data Link Property dialog box which allows the user to select such items as the data provider, database, user, etc. You can search MSDN for more information on this.
There is an also undocumented VCL that encapsulates this API. Called
PromptDataSource, it only needs your app's handle and a widestring. It returns a widestring with the connection information.
Code:
WideString PromptDataSource(int Handle, WideString ConnectionString);
Your app's handle can be found by (int)Application->Handle or (int)MyForm->Handle. The connection string can be a blank ("") or an actual connection string. If the passed connection string is valid, the called dialog box will show the data object as the default.
To use this function, you will need to include ADODB.hpp in your app's header file. You can then use a button on your app to call the VCL.
Code:
void __fastcall TMyForm::DialogButtonClick(TObject *Sender)
{
WideString WCEStr = Edit1->Text;
WideString WResult = PromptDataSource((int)Application->Handle, WCEStr);
Edit1->Text = WResult;
}
//---------------------------------------------------------------------------
In the above example, the VCL takes the app's handle and a string from an edit box. The returned string is then placed back into the edit box. This allows any subsequent calls to the VCL to use the same connection info. [color red]NOTE:[/color]Any password entered into the dialog box can be passed back to the calling app and is available as plain text in this example.
Your ADO component can then use the returned widestring as its connection string.