Ошибка
Цитата:
// mydataDlg.cpp : implementation file
//
#include "stdafx.h"
#include "mydata.h"
#include "mydataDlg.h"
#include "afxdb.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CmydataDlg dialog
CmydataDlg::CmydataDlg(CWnd* pParent /*=NULL*/)
: CDialog(CmydataDlg::IDD, pParent)
, sdb(0)
, m_pathtobase(_T(""))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CmydataDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST1, ListBox);
DDX_Control(pDX, IDC_EDIT1, m_pathbase);
}
BEGIN_MESSAGE_MAP(CmydataDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDOK, OnBnClickedOk)
ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1)
ON_WM_CREATE()
END_MESSAGE_MAP()
// CmydataDlg message handlers
BOOL CmydataDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
m_pathbase.SetWindowText("DataBase not selected !");
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CmydataDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CmydataDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CmydataDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CmydataDlg::OnBnClickedOk()
{
// TODO: Add your control notification handler code here
CDatabase db;
CString constr,path;
m_pathbase.GetWindowText(path);
constr="DBQ="+path+";Driver={Microsoft Access Driver (*.mdb)}";
//Для вызова нопределенной БД db.OpenEx( NULL, CDatabase::forceOdbcDialog );
db.OpenEx(constr);
// Create and open a recordset object
// directly from CRecordset. Note that a
// table must exist in a connected database.
// Use forwardOnly type recordset for best
// performance, since only MoveNext is required
CRecordset rs( &db );
rs.Open( CRecordset::forwardOnly,
_T( "SELECT * FROM tPeople" ) );
// Create a CDBVariant object to
// store field data
CString varValue,Res;
// Loop through the recordset,
// using GetFieldValue and
// GetODBCFieldCount to retrieve
// data in all columns
short nFields = rs.GetODBCFieldCount( );
while( !rs.IsEOF( ) )
{
for( short index = 0; index < nFields; index++ )
{
rs.GetFieldValue( index, varValue );
//rs.GetFieldValue(index,Field);
//do something with varValue
//ListCtrl.InsertItem(LVIF_TEXT|LVIF_STATE, 1,varValue,
//LVIS_SELECTED, LVIS_SELECTED, 0, 0);
Res=Res+" "+varValue;
}
ListBox.AddString(Res);
Res="";
rs.MoveNext( );
}
rs.Close( );
db.Close( );
}
void CmydataDlg::OnBnClickedButton1()
{
char szFilters[]="Microsoft Access Files (*.mdb)|*.mdb|All Files (*.*)|*.*||";
CFileDialog filebox(TRUE, "*.*", "*.mdb",OFN_FILEMUSTEXIST| OFN_HIDEREADONLY,
szFilters, this);
if( filebox.DoModal ()==IDOK )
{
data=filebox.GetPathName();
m_pathtobase=data;
m_pathbase.SetWindowText(data);
//UpdateData(false);
//получение полного пути к файлу
//CString pathName = filebox.GetPathName();
// Implement opening and reading file in here.
//Получение имени файла
//CString fileName = filebox.GetFileTitle ();
}
}
int CmydataDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
return 0;
}
// mydataDlg.h : header file
//
#pragma once
#include "afxwin.h"
// CmydataDlg dialog
class CmydataDlg : public CDialog
{
// Construction
public:
CmydataDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
enum { IDD = IDD_MYDATA_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedOk();
CListBox ListBox;
private:
// выбранная база данных
char sdb;
CString data;
public:
afx_msg void OnBnClickedButton1();
CString m_pathtobase;
CEdit m_pathbase;
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
};
Разбирайтесь с диалогами в MFC. Вы плохо пока понимаете их идеологию. На такую мысль меня наводит, в частности, использование переменной m_pathbase (видимо, это CEdit). Очевидно, вместо неё логично иметь CString m_strPathBase. Когда поймёте, как это всё у них там организовано, всё сразу заработает.