• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 401
  • Last Modified:

Can't GetDocument

I'm developing a database program. I would like to place my recordset objects in the document class. I open my recordset objects in the OnCreate function of my CMDIChild derived class, this allows me to terminate and not show the window if someting goes wrong. The problem is the call to GetDocument doesn't seem to work in the OnCreate before I call the base class. I have placed my recordset objects in the CMDIChild derived class to have access to them, but this doesn't seem like good MFC programming practice. Can anyone help?
0
BlackRussian
Asked:
BlackRussian
  • 6
  • 4
1 Solution
 
tma050898Commented:
You could attempt to create the recordset objects in the OnOpenDocument function and return FALSE if there is an error.

Tom

0
 
t004024Commented:
I have this problem too. I needed to initialize an object in the document and not create the view if it fails. The problem is in the OnCreate of the frame the document is not yet associated with the frame. But I did find out that there is an overridable called LoadFrame, which is called when the association to the document occurs and this also receives a CCreateContext pointer which has the pointer to the document. I tried checking it here and returning a FALSE, if it did not succeed, it works, but my problem from the beginning was that when the view is not created, it gives me a message "Cannot create empty document" and I have been trying to get rid of this message which I could not. U could try this if U want. I had actually done the check in the OnCreate of the view and returned a -1, if the document construction failed. I would really like to know if U succeed in this.

I do not know if the OnOpenDocument will really work, because, in that case U need to create a new template of U'r own and overide the CreateNewDocument of the template and not that of the document or else U have to override the OnOpen of the menu item and do all the document creation by U'rself, which is rather painful and why do it when MFC already does it for U.
0
 
tma050898Commented:
t004024,

I have no idea what you are talking about when you say
"... because, in that case U need to create a new template of U'r own and overide the CreateNewDocument of the template and not that of the document or else U have to override the OnOpen of the menu item and do all the document creation by U'rself, which is rather painful and why do it when MFC already does it for U."

You do NOT have to ANY of this. Simply return FALSE from the OnNewDocument (for new files) or OnOpenDocument (for opening files) and you that's it. That's what these functions were intended for.

Tom

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
t004024Commented:
tma,

OK, I have no idea why this happens. This is what I had tried. The documentation says that overridables for CDocument class is so so so.. So I tried to override the OnNewDocument function, put a break point there and ran the program, but it never comes to my break point. I have no idea why it does this, though the documentation says that it has to call OnNewDocument of CMyDoc.

Unless it happens that the CMultiDocTemplate is initializing a CDocument instead of CMyDoc, in which case, the OnNewDocument called will be that in CDocument and not that of CMyDoc... and that I do not know why it happens, because if we add the document template with CMyDoc as the document it should initialize CMyDoc...
0
 
tma050898Commented:
>>>Unless it happens that the CMultiDocTemplate is initializing a CDocument instead of CMyDoc, in which case, the OnNewDocument called will be that in CDocument and not that of CMyDoc
==============================================
For a change, the docs are right <G>.

Did you place a break point in the Document::OnNewDocument. You could do that and then check the call stack. Also, are you sure that you are passing the runtime class info of CMyDoc to the AddDocTemplate function? What does your AddDocTemplate call look like? In addition, remember that OnNewDocument is only called for NEW documents. If you are opening the last document used, you need a breakpoint in the OnOpenDocument.

Tom
0
 
t004024Commented:
I had done the development long back and I finished the product with the message still. Now that U pointed out I tried it again. What I forgot to do then was run and check for a successful initialization, actually it is true that for correct initialization it calls OnNewDocument.

This is what is happening, I set the break point in OnNewDocument and ran it for a successful initialization, it calls OnNewDocument, but I run it for an unsuccessful initialization and it does not work. No idea again why, though now I do know where it is giving me the message from and I know why it is not calling the OnNewDocument, that is because the Document pointer is not getting created. But my problem is this, I have a screen which gets the table to be initialized to, if the user chooses cancel, I want to stop the creation of the document, but I do not throw any exceptions or do any error processing, I just set a member to NULL and continue with the rest of the document processing, but why does it not create my document???
0
 
tma050898Commented:
t004024,
In all fairness to BlackRussian and in keeping with EE rules, you need to ask your question in another thread. Once you post your own separate question, I (and other experts) can help you then. Sorry, but it's not fair to BlackRussian that we continue talking about your problem when his points are paying for this thread.

BlackRussian,
Was the answer I gave you enough, or do you need any additional information?

Tom

0
 
t004024Commented:
Tom,

Sorry, that was not a question, that was just to confirm U'r answer that it does work and not as I had written before that it does not work, I think what is happening is something I can find out. Sorry, if it seemed like a question and I think it does seem so, it was just I was thinking while I was writing.. Thanx.
0
 
tma050898Commented:
No problem. I just didn't want Linda or BlackRussian to get upset with either of us.

Tom

0
 
tma050898Commented:
Why exactly did I receive a 'D' for an answer that solves your problem??? I answered your question and on at least one other post asked if you needed additional assistance. Not exactly a great way to show other experts that they should waste their time helping you, eh????
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now