Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NEWBIE, Please help with using variables 1

Status
Not open for further replies.

dizzyh

Programmer
Jul 12, 2004
4
ZA
Hi all

I am a reasonable newbie to VC++ 6.0.
I am busy with a program that accesses a db, how do I access the db in different dialogs eg: User login Dialog has db access, and the create account has db access.

How do I access the same db for two different dialogs, but in 1 program.

Any help will be appreciated.

Thanks
 
Put the db handle in the main app object, then both dialogs should be able to access it.
 
Would the main app object be in the CBSSApp::InitInstance ?
Currently this is how I am connecting to the db:

void CBSSDlg::OnLogonBtn()
{
// TODO: Add your control notification handler code here
CDatabase* m_pDB = new CDatabase();
CRecordset* m_pRS = new CRecordset(m_pDB);

CString m_UserName, m_Password, m_AccessLevel;

try
{
CString strID;
m_pDB->Open("BSS");
m_pRS->Open(CRecordset::forwardOnly, "Select * from UserDetails ");
m_pRS->GetFieldValue(short(0), strID);
m_nUserID = atol(strID);

m_pRS->GetFieldValue(short(1),m_UserName);
m_pRS->GetFieldValue(short(2),m_Password);
m_pRS->GetFieldValue(int(3),m_AccessLevel);


UpdateData(TRUE);

}
catch(CDBException *pErr)
{
TCHAR errBuff[255];
pErr->GetErrorMessage(errBuff, 255);
CString strError(errBuff);
AfxMessageBox(strError);
}

if(((m_UserName)==(m_strUserName))&&((m_Password)==(m_strPassword))&&((m_AccessLevel)==(m_strAccessLevel)))
{

AfxMessageBox("User Logged On Successfully!");
}
else
{
AfxMessageBox("Please enter a user name, password and accesslevel");
}

Would I put this code in the CBSSApp::InitInstance ?
 
m_pDB and m_pRS need to be members of class CBSSApp, not just local variables in the InitInstance method. The "m_" usually means "member variable". If you add them to the class as member variables, you should be able to access them anywhere you can access the app. In Visual C++ the app is always declared a global variable called "theApp".
 
Would it be like so:
class CBSSApp : public CWinApp
{
public:
CRecordset* m_pRS;
CDatabase* m_pDB;
CBSSApp();



Forgive me if I sound a bit dumb! Like I said, Im not to clued up on VC++.
 
Yes, exactly. I hope this works for you. You don't sound dumb, don't worry about it. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top