[RESOLVED] Help In ListBox delete portion
Hi everyone,
I Have build one of the example of my application which is attach in this thread.
The Problem is that when I Add a item in the listbox and want to delete it immediately, it cannot delete it completely. For example, I add an item call Test_4 and want to delete it immediately, I click the delete button and Test_4 is deleted in the Listbox. If you close form and open it again, Test_4 is still there.
What is the problem lies within.
Note: Inside the attach file, there is a database table call test.mdb in it.
Thank in Advance.
# 1 Re: [RESOLVED] Help In ListBox delete portion
I don't have my IDE here at home so I can't get a good look at the code, but what's with the MovePrev in your delete code?
GCDEF at 2007-11-10 23:06:29 >

# 2 Re: [RESOLVED] Help In ListBox delete portion
I don't have my IDE here at home so I can't get a good look at the code, but what's with the MovePrev in your delete code?
Thanks GCDEF for replying,
Delete button Source Code
void CTestForm::OnButton2() // --> Delete Button
{
CTestDoc* pDoc = GetDocument();
if(MessageBox("Are you sure deleting this record?","Delete Record", MB_YESNO | MB_ICONQUESTION) == IDYES)
{
int index = m_listbox.GetCurSel();
UINT ID = m_listbox.GetItemData(index);
UINT IDs = pDoc->m_Test.m_Test_ID;
if (ID == IDs)
{
pDoc->m_Test.MovePrev();
pDoc->m_Test.Delete();
UpdateData(FALSE);
}
m_listbox.DeleteString(index);
}
}
The deletion only works with it need to close the form and open it again.
For example, I add an item call ABC in the Listbox, I cannot delete ABC immediately. I need to close the form and open it again to delete ABC completely.
Attach picture to have a clearer view
# 3 Re: [RESOLVED] Help In ListBox delete portion
I don't understand what you're trying to do with it. Are you sure you're deleting the right record. With that code, I don't see how you could be.
GCDEF at 2007-11-10 23:08:29 >

# 4 Re: [RESOLVED] Help In ListBox delete portion
Sorry, I dunno how to explain to you. You need to execute my Application to see a better view
# 5 Re: [RESOLVED] Help In ListBox delete portion
Without executing your code, I think you can be pretty sure you're not deleting the right database record. The reason the record is still there when you reload the listbox is you've deleted the wrong record from the database.
GCDEF at 2007-11-10 23:10:33 >

# 6 Re: [RESOLVED] Help In ListBox delete portion
Thanks GCDEF for troubling you again.
I change the coding to this in the delete button
void CTestForm::OnButton2() // --> Delete Button
{
m_Test1 = &GetDocument()->m_Test1;
CTestDoc* pDoc = GetDocument();
if(MessageBox("Are you sure deleting this record?","Delete Record", MB_YESNO | MB_ICONQUESTION) == IDYES)
{
int index = m_listbox.GetCurSel();
UINT ID = m_listbox.GetItemData(index);
while(!pDoc->m_Test1.IsEOF()) // Loop Recordset
{
UINT IDs = pDoc->m_Test1.m_Test_ID;
if (ID == IDs)
{
pDoc->m_Test1.Delete();
UpdateData(FALSE);
}
pDoc->m_Test1.MoveNext();
}
pDoc->m_Test1.Requery();
m_listbox.DeleteString(index);
MessageBox("Record Deleted");
}
}
I still can't delete added item on the spot.
Pls guide me
Thanks in advance GCDEF.
# 7 Re: [RESOLVED] Help In ListBox delete portion
Any Help
Thanks.
# 8 Re: [RESOLVED] Help In ListBox delete portion
Have you stepped through it using the debugger?
I would guess that your recordset is already at EOF since you would have used it to populate the listbox.
You have a few things you're not doing correctly. First, learn how to use m_strFilter and Requery to find the record you want. Sequentially searching like that is very inefficient and kind of defeats the purpose of a database.
Second, calling UpdateData after deleting the database record accomplishes nothing.
Third, calling Requery after the Delete accomplishes nothing.
Along with learning how to properly position yourself on a database record, I'd recommend getting familiar with the debugger at this point too.
GCDEF at 2007-11-10 23:13:40 >

# 9 Re: [RESOLVED] Help In ListBox delete portion
Have you stepped through it using the debugger?
I would guess that your recordset is already at EOF since you would have used it to populate the listbox.
You have a few things you're not doing correctly. First, learn how to use m_strFilter and Requery to find the record you want. Sequentially searching like that is very inefficient and kind of defeats the purpose of a database.
Second, calling UpdateData after deleting the database record accomplishes nothing.
Third, calling Requery after the Delete accomplishes nothing.
Along with learning how to properly position yourself on a database record, I'd recommend getting familiar with the debugger at this point too.
Thanks for the reply,
Any ideas where can I get help from the above things you mention.
# 10 Re: [RESOLVED] Help In ListBox delete portion
Thanks for the reply,
Any ideas where can I get help from the above things you mention.
MSDN. Start by getting a really good understanding of how CRecordsets work. You're not using them correctly yet.
GCDEF at 2007-11-10 23:15:39 >
