End Google Ads 201810 - BS.net 01 --> To create a secured MS Access database programmatically using MFC I followed the procedure described at http://www.lit********com/Knowhow/Ho...ccess_dat.html[^]. I have incorporated the code in one of my functions. However, while granting privileges to the newly created group exception is thrown and I am not able to create the database. The function in which I am using the code is as below.

void CSecuredAccessDlg::OnBnClickedOk()
{
if(m_fileName.IsEmpty())
{
MessageBox("No file selected!",NULL,MB_ICONERROR);
return;
}

CString strConfig,sSql;
CString strAccessDriver;
CDatabase database;

strAccessDriver="Microsoft Access Driver (*.mdb)";

if(_access("my workgroup.mdw",0)==0)// If the work group file exists, delete it
unlink("my workgroup.mdw");
if(_access(m_fileName,0)==0) // If the database file exists, delete it
unlink(m_fileName);

strConfig.Format("CREATE_SYSDB=\"my workgroup.mdw\"\0\0");
SQLConfigDataSource(NULL, ODBC_ADD_DSN, strAccessDriver,strConfig);

strConfig.Empty();
strConfig.Format("CREATE_DB=\"%s\"\0\0",m_fileName);
SQLConfigDataSource(NULL, ODBC_ADD_DSN, strAccessDriver,strConfig);

strConfig.Empty();
TRY
{
strConfig.Format("DRIVER={%s};DSN='';READONLY=FALSE;"
"CREATE_DB=\"%s\";DBQ=%s;SystemDB=%s;UID=admin;"
"ExtendedAnsiSQL=1",strAccessDriver,m_fileName,m_fileName,
"my workgroup.mdw");
if(database.OpenEx(strConfig,CDatabase::noOdbcDialog))
{
sSql.Format("CREATE GROUP %s %s","Administrators","1234ABC");
database.ExecuteSQL(sSql);

sSql.Empty();
sSql.Format("CREATE USER %s \"%s\" %s","Pani","","1234DEF");
database.ExecuteSQL(sSql);

// Add user to the group created
sSql.Empty();
sSql.Format("ADD USER %s TO %s","Pani","Administrators");
database.ExecuteSQL(sSql);

// Add user to the the admins group
sSql.Empty();
sSql.Format("ADD USER %s TO %s","Pani","admins");
database.ExecuteSQL(sSql);

database.Close();
unlink(m_fileName);

strConfig.Empty();
strConfig.Format("CREATE_DB=\"%s\"\0SystemDB=my workgroup.mdw"
"\0UID=Pani\0\0",m_fileName);
SQLConfigDataSource(NULL, ODBC_ADD_DSN, strAccessDriver,strConfig);

strConfig.Format("DRIVER={%s};DSN='';READONLY=FALSE;"
"CREATE_DB=\"%s\";DBQ=%s;SystemDB=%s;UID=Pani;ExtendedAnsiSQL=1",
strAccessDriver,m_fileName,m_fileName,"my workgroup.mdw");

if(database.OpenEx(strConfig))
{
sSql.Format("GRANT ALL PRIVILEGES ON DATABASE TO Administrators");
database.ExecuteSQL(sSql); // Exception occurs here

sSql.Format("REVOKE ALL PRIVILEGES ON DATABASE FROM admin");
database.ExecuteSQL(sSql);

sSql.Format("REVOKE ALL PRIVILEGES ON DATABASE FROM admins");
database.ExecuteSQL(sSql);

sSql.Format("REVOKE ALL PRIVILEGES ON DATABASE FROM users");
database.ExecuteSQL(sSql);

// Alter the user password to pani
sSql.Format("ALTER USER Pani PASSWORD %s \"%s\"","pani123","");
database.ExecuteSQL(sSql);

FillData(&database);
database.Close();
}

}
}
CATCH(CDBException, e)
{
if(database.IsOpen())
database.Close();
MessageBox(e->m_strError,NULL,MB_ICONERROR);
return;
}
END_CATCH
}

Can anyone provide suggestions for correcting the code?

Thanks in advance.

Pani68

modified on Thursday, April 2, 2009 3:32 AM