在IDC_COMQUE的组合框中输入文本,然后查询
数据库自动匹配,显示在下拉列表框中
void CDwqDlg::OnEditchangeComque()
{
// TODO: Add your control notification handler code here
GetDlgItemText(IDC_COMQUE, quebuff, 20);
QueryDB(quebuff);
((CComboBox*)GetDlgItem(IDC_COMQUE))->UpdateWindow();
((CComboBox*)GetDlgItem(IDC_COMQUE))->ShowDropDown(FALSE);
((CComboBox*)GetDlgItem(IDC_COMQUE))->ShowDropDown(TRUE);
((CComboBox*)GetDlgItem(IDC_COMQUE))->SetEditSel(-1, 0);
((CComboBox*)GetDlgItem(IDC_COMQUE))->UpdateWindow();
::SetCursor(::LoadCursor(NULL,IDC_ARROW)); //重新显示鼠标
}
long CDwqDlg::QueryDB(char *str)
{
CDatabase cdb;
CRecordset crs(&cdb);
char strConnect[256];
char fullpath[256];
GetCurrentDirectory(256, fullpath);
strcat(fullpath, "\\dwq.mdb");
sprintf(m_strConnect, "Driver={Microsoft Access Driver (*.mdb)};Dbq=%s;Uid=Admin;Pwd=;", fullpath);
sprintf(fullpath, "select * from %s", m_curtb);
CComboBox *pCmb = ((CComboBox*)GetDlgItem(IDC_COMQUE));
int nCount = pCmb->GetCount();
for (int i = nCount - 1; i >= 0; i--)
{
// pCmb->DeleteItem(i);
pCmb->DeleteString(i);
}
pCmb->AddString(quebuff);
if(cdb.OpenEx(m_strConnect, 0))
{
char strSql[64] = "select * from dwq"; //SQL语句
sprintf(strSql, "select * from %s where floor like \'%%%s%%\'", m_curtb, str);
crs.Open(NULL, strSql); //执行
Sql语句(可添加 删除 查询等)
if(crs.IsOpen())
{
CString cstr;
if (crs.IsBOF())
{
return 0;
}
crs.MoveFirst();
// int i = 0;
m_count = 0;
while(!crs.IsEOF())
{
//读取记录
crs.GetFieldValue(_T("floor"), cstr);
strcpy(strConnect, cstr.GetBuffer(cstr.GetLength()));
pCmb->AddString(strConnect);
m_count++;
crs.MoveNext();
}
}
return TRUE;
}
crs.Close();
return 0;
}