Article ID: 184805
Article Last Modified on 3/11/2005
VARIANT _Application::Run(LPCTSTR Procedure, VARIANT* Arg1, VARIANT*
Arg2, ......., VARIANT* Arg30)
The example demonstrates a way to ensure values passed in as Arg1, Arg2,
and so on, can be modified and reflected in the calling application code.
Function MyFunction(strParam1 As String, strParam2 As String)
strParam1 = "NewValue1"
strParam2 = "NewValue2"
End Function
178749 HOWTO: Create an Automation Project Using MFC and a Type Library
#include "msacc8.h"
// Start Microsoft Access.
_Application* pAccess = new _Application;
VERIFY(pAccess->CreateDispatch("Access.Application"));
// Open a database.
pAccess->OpenCurrentDatabase("c:\\mydatabase.mdb", FALSE);
// Initialize an array of variants for the Run method.
VARIANT aExc[30];
for (int i=0;i<30;i++)
{
VariantInit(&aExc[i]);
aExc[i].vt=VT_ERROR;
aExc[i].scode=0x80020004; // indicates parameter isn't used.
}
// Set up some arguments.
CString pAV1 = "FirstParam"; //Initial value for Arg1.
CString pAV2 = "SecondParam"; //Initial value for Arg2.
BSTR bParam1 = pAV1.AllocSysString();
BSTR bParam2 = pAV2.AllocSysString();
// Initialize parameters 1 and 2.
aExc[0].pbstrVal=&bParam1;
aExc[1].pbstrVal=&bParam2;
aExc[0].vt=VT_BSTR | VT_BYREF;
aExc[1].vt=VT_BSTR | VT_BYREF;
// Call Application.Run
try
{
pAccess->Run("MyFunction",
&aExc[0],&aExc[1],&aExc[2],&aExc[3],&aExc[4],
&aExc[5],&aExc[6],&aExc[7],&aExc[8],&aExc[9],
&aExc[10],&aExc[11],&aExc[12],&aExc[13],&aExc[14],
&aExc[15],&aExc[16],&aExc[17],&aExc[18],&aExc[19],
&aExc[20],&aExc[21],&aExc[22],&aExc[23],&aExc[24],
&aExc[25],&aExc[26],&aExc[27],&aExc[28],&aExc[29]);
//After the function has been called,
//the values of bParam1 and bParam2 have changed.
//Display the new values.
AfxMessageBox(CString("bParam1 = ") + CString(bParam1));
AfxMessageBox(CString("bParam2 = ") + CString(bParam2));
}
catch(CException* e)
{
TCHAR szErrorMessage[1024];
UINT nHelpContext;
if (e->GetErrorMessage(szErrorMessage, 1024, &nHelpContext))
{
AfxMessageBox(szErrorMessage, MB_OK, nHelpContext);
}
e->Delete();
}
// Free any allocated strings.
::SysFreeString(bParam1);
::SysFreeString(bParam2);
if (pAccess)
{
pAccess->Quit(0);
pAccess->ReleaseDispatch();
delete pAccess;
pAccess = NULL;
}
Additional query words: 6.2
Keywords: kbinfo kbinterop kbautomation KB184805