|
Can a MS Access database be password protected? And if so, how do deal with that when trying to read the database in your program?
|
|
|
|
|
Hi, please help.
I'm a novice coder and the database connection method identified on this page is practically exactly what i needed for my group's application. The following code is an attempt to connect to an access database on my hard drive, check a value in a certain field of a certain table, update that field if its value is 'no' and add data into some other fields. Here's the code
#include <stdio.h>
#include <stdlib.h>
#include <afxdb.h>
extern "C" {
#include <odbcinst.h>
}
void main()
{
CDatabase database;
CString sDriver = "Microsoft Access Driver (*.mdb)";
CString sDsn;
CString sFile = "c:\\WPS\\WPSData.mdb";
CString SqlString, SqlString1;
CString varValue;
sDsn.Format("ODBC;DRIVER={%s};DSN='';DBQ=%s",sDriver,sFile);
TRY
{
database.Open(NULL,false,false,sDsn);
CRecordset recset(&database);
SqlString = "SELECT * FROM Member1234";
recset.Open(CRecordset::forwardOnly,SqlString);
while(recset.IsEOF()!=1)
{
recset.MoveNext();
}
recset.GetFieldValue("ParkCheck", varValue);
if(varValue = "No")
{
SqlString1.Format("UPDATE Member1234 SET [ParkCheck] = #%s#", "Yes");
database.ExecuteSQL(SqlString1);
SqlString1.Format("INSERT INTO (ParkID, Date, ParkInTime) VALUES (1, '7/04/2004', '8.30pm')");
database.ExecuteSQL(SqlString1);
if(!recset.Update())
{
AfxMessageBox("Record not added");
}
}
else
{
SqlString1.Format("INSERT INTO (ParkOutTime) VALUES ('8.30pm')");
database.ExecuteSQL(SqlString1);
if(!recset.Update())
{
AfxMessageBox("Record not added");
}
}
database.Close();
}
CATCH(CDBException, e)
{
AfxMessageBox("Database error: "+e->m_strError);
}
END_CATCH;
}
I get the following unresolved external linker errors and i have NO idea why??? There's 33 of them, so please bear with me
[Linker Error] Unresolved external '__stdcall CException::operator delete(void *)' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CDBException::~CDBException()' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CException::~CException()' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external '__stdcall CObject::operator delete(void *)' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CObject::~CObject()' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CString::~CString()' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CDatabase::~CDatabase()' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CPtrList::~CPtrList()' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external '__stdcall AfxTryCleanup()' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CRecordset::~CRecordset()' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CMapPtrToPtr::~CMapPtrToPtr()' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CDatabase::CDatabase()' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CString::CString(const char *)' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CString::CString()' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'AFX_EXCEPTION_LINK::AFX_EXCEPTION_LINK()' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CDatabase::Open(const char *, int, int, const char *, int)' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CRecordset::CRecordset(CDatabase *)' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CRecordset::MoveNext()' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CRecordset::IsEOF() const' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CRecordset::Open(unsigned int, const char *, unsigned long)' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CRecordset::CanAppend() const' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CRecordset::GetFieldValue(const char *, CString&)' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CString::operator =(const char *)' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CString::Format(const char *, ...)' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CDatabase::ExecuteSQL(const char *)' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CRecordset::Update()' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CDatabase::Close()' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CDBException::classCDBException' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CObject::IsKindOf(const CRuntimeClass *) const' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external '__stdcall AfxAssertFailedLine(const char *, int)' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external '__stdcall operator +(const char *, const CString&)' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external 'CString::operator const char *() const' referenced from C:\WPS\LISTENING.OBJ
[Linker Error] Unresolved external '__stdcall AfxMessageBox(const char *, unsigned int, unsigned int)' referenced from C:\WPS\LISTENING.OBJ
Any help or solution would be greatly appreciated. Thankyou in advance
|
|
|
|
|
Anyone knows how to resolve this problem ?
When I Set
CRecordset.m_strFilter = "Date=#8/01/2004#"; (or other date)
it don`t works
It not give any records
Thanks
|
|
|
|
|
You have to use:
CRecordset.m_strFilter = "Date >= #8/01/2004 00:00:00# AND Date <= #8/01/2004 23:59:59#";
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
if the Data is date\time style?
if so, the date 8/01/2004 should include hour,minute and second.
|
|
|
|
|
i have problems linkin the edit box member variables to my database, how to i display the data into the edit box? i am using Crecordset and added m_pSet variable....Urgent!
|
|
|
|
|
hi,
There is a small error in this program and it is recoverable.
Description:- For iterating through the table to go to next record author used
while(!recset.IsEOF)
but this is wrong. If you try this you will only be able to fetch only one record. The solution to this problem is simple
while(recset.IsEOF()!=1)
happy coding bye, Have a nice Day!
cobra
|
|
|
|
|
You must be doing something (else) wrong. The statement:
while (! recset.IsEOF())
is correct in all aspects. If it is only returning one row for you, something else is wrong.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
Which are:
1) Can you change the the colour (UK English ) of each row of a list control?
2) How can be serialised?
about Visual C++ being complicated
|
|
|
|
|
int fileid=1;
SqlString = "SELECT * "
"FROM Artefact "
"WHERE Id= fileid";
the 'WHERE' clause don't seem to be able to 'extract' out my variable. How do i go abt extracting the integer from my variable? PLs help!
I am who you think I am
|
|
|
|
|
Just type:
"WHERE = %d", fileid;
or something like this.
This might work.
|
|
|
|
|
This will work:
// Build the SQL statement
SqlString = "SELECT CatID, Category "
"FROM Categories "
"WHERE CatID = 4";
|
|
|
|
|
You have to use:
CString strQuery;
strQuery.Format("SELECT * FROM Artefact WHERE Id = %d", fileid);
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
David, I realise this is old material (shows how far behind I am), but Which is best to use with a MS Access DB, ADO or CRecordset? or are they the same. I have a MS Access DB (.mdb) that I want to use in a program; but I have no idea what to use, or how? Is there any good examples in Codeproject (or elsewhere) that you could point me to?
A C++ programming language novice, but striving to learn
|
|
|
|
|
Larry Mills Sr wrote: ...Which is best to use with a MS Access DB, ADO or CRecordset?
I've never used ADO so I can't say.
Larry Mills Sr wrote: or are they the same.
No, they are not the same.
Larry Mills Sr wrote: I have a MS Access DB (.mdb) that I want to use in a program; but I have no idea what to use, or how? Is there any good examples in Codeproject (or elsewhere) that you could point me to?
Other than the article this thread belongs to? How about this one?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
David, I downloaded your project and then created one of my own using my database(.mdb) You show using Listboxes and static controls, I tried placing a Listbox in my "Form" and the compiler keeps giving me errors when I tried to create the "SetColoums()"I'll give you bits of code so you can see what's happening:(the code compiles but crashes in operation)
void CCookItDBView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
DDX_Control(pDX, IDC_INGRED_LC, m_cIngred_LC);// m_cIngred_LC is the List Control varable
}
void CCookItDBView::SetColumsIngred()
{
//CString csWorkDate = "";
CString csAmt = "Amount:";
CString csUnit = "Unit:";
CString csIngred = "Ingredients:";
// insert two columns (REPORT mode) and modify the new header items
CRect rect;
m_cIngred_LC.GetClientRect(&rect);// errors here
int nColInterval = rect.Width()/7;
m_cIngred_LC.InsertColumn(0, csAmt, LVCFMT_LEFT, nColInterval*2);
m_cIngred_LC.InsertColumn(1, csUnit, LVCFMT_LEFT, nColInterval*2);
m_cIngred_LC.InsertColumn(2, csIngred, LVCFMT_LEFT, nColInterval*2);
}
Any ideas?
A C++ programming language novice, but striving to learn
|
|
|
|
|
Larry Mills Sr wrote: m_cIngred_LC.GetClientRect(&rect);// errors here
You failed to indicate what error(s) were happening. Depending on where SetColumsIngred() is called from, I suspect the window that m_cIngred_LC belongs to does not yet exist.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Here's my header and file data:
class CCookItDBView : public CRecordView
{
protected: // create from serialization only
CCookItDBView();
DECLARE_DYNCREATE(CCookItDBView)
public:
enum{ IDD = IDD_COOKITDB_FORM };
CMyDB* m_pSet;
// Ingredients LC
CListCtrl m_cIngred_LC;
// Overrides
public:
virtual CRecordset* OnGetRecordset();
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
virtual void OnInitialUpdate(); // called first time after construct
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
.....
};
class CMyDB : public CRecordset
{
public:
CMyDB(CDatabase* pDatabase = NULL);
DECLARE_DYNAMIC(CMyDB)
.....
};
void CCookItDBView::OnInitialUpdate()
{
SetColumsIngred();
CRecordView::OnInitialUpdate();
}
void CCookItDBView::SetColumsIngred()
{
//CString csWorkDate = "";
CString csAmt = "Amount:";
CString csUnit = "Unit:";
CString csIngred = "Ingredients:";
// insert two columns (REPORT mode) and modify the new header items
CRect rect;
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
m_cIngred_LC.GetClientRect(&rect);// crashes here at runtime not compile time
int nColInterval = rect.Width()/7;
m_cIngred_LC.InsertColumn(0, csAmt, LVCFMT_LEFT, nColInterval*2);//crashes here at runtime not compile time
m_cIngred_LC.InsertColumn(1, csUnit, LVCFMT_LEFT, nColInterval*2);//crashes here at runtime not compile time
m_cIngred_LC.InsertColumn(2, csIngred, LVCFMT_LEFT, nColInterval*2);//crashes here at runtime not compile time
}
A C++ programming language novice, but striving to learn
|
|
|
|
|
Have you tried calling SetColumsIngred() from OnUpdate() instead?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
I only had to change the path name of the database file, and everything worked fine!!
Very Good!!
William
|
|
|
|
|
i don't know how to save variant which is int or bool to corresponding field in access2000.my program is using ADO
Who knew the key?tell me please. good thanks.
|
|
|
|
|
While using ADO, to save bool (small case) :
pRecordset->Fields->GetItem("FieldName")->Value = (_bstr_t)"1"; // true, "0" : false.
To save int:
pRecordset->Fields->GetItem("FieldName")->Value = (_bstr_t)"12345"; // Your number.
Maxwell Chen
Lets make bugs better!
|
|
|
|
|
It's a nice tutorial...but it does't tell anything about how to edit/Delete/Insert data..Please help me to do the same...
Koshy Panicker J
|
|
|
|
|
Yes.
You need to use the CRecordset class
For that you have the CRecordset::Edit, CRecordset::AddNew, CRecordset::Update, CRecordset::Delete functions...
best Regards
Carlos Antollini.
www.wanakostudios.com
Sonork ID 100.10529 cantollini
|
|
|
|
|
Can you provide an example of how to do this in the author's context? Notice he creates a generic CRecordset not an inherited one. Are you saying to use a regular CRecordset or a derived one? Can you give a source code example? According to the MSDN, the AddNew() member function gets called and then you set the record set's variables. This won't work unless you have a derived record set that has the variables that you need. Can you explain what you mean?
-Matt
------------------------------------------
The 3 great virtues of a programmer:
Laziness, Impatience, and Hubris.
--Larry Wall
|
|
|
|