borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (Search a keyword in txt and pull the data)Next Topic (destroying instances of Excel) New Topic New Poll Post Reply
AndreaVB Forum : VBA (Access, Excel, Word, ...) : run code when closing document?
Poster Message
Richard M
Level: Trainee

Registered: 02-10-2005
Posts: 1

icon run code when closing document?

I am using the following code from another post to check a form fill in box upon close. If the textbox is null or empty I want a message saying so and then cancel the save operation.  The code is not working. What am I missing?

Private Sub Document_Close()
Dim iChk


If ActiveDocument.Saved = False Then

iChk = MsgBox("do you want to save the changes to " & ThisDocument.Name & "?", vbYesNoCancel, "check if document changed")


Select Case iChk
Case 6
If IsNull(text1) Then
MsgBox ("you need to fill in the text1")
MsgBox "Cancel"
Exit Sub
End If
MsgBox "Yes"
Case 7
MsgBox "No"
Case 2
MsgBox "Cancel"
End Select

End If


____________________________
Richard

02-10-2005 at 10:32 PM
View Profile Send Email to User Show All Posts | Quote Reply
Shady
Level: VB Guru


Registered: 08-07-2002
Posts: 305
icon Re: run code when closing document?

Hi there,

OK, looking at your code you are getting iChk to return a result from your message box, and then deciding what to do with a select case statement.  What I don't understand is why you are testing for 6,7 and 2. If it were me the code would be:-

Select Case iChk
   Case VbYes
       [Do This]
   Case VBNo
       [Do This]
   Case VBCancel
       [Do This]
Ens Select

I don't see how the code can work whilst you are testing for those numbers instead of the VB keywords.

Try it, and see if it solves your problem.

Shady

____________________________
I don't wanna die... but I ain't keen on livin' either

05-10-2005 at 03:55 PM
View Profile Send Email to User Show All Posts | Quote Reply
GeoffS
Level: VB Lord


Registered: 29-09-2004
Posts: 536
icon Re: run code when closing document?

Hi Richard,

I can see why Shady is saying to use the VBKeyWords rather than their current values - that way if Microsoft ever change the Return Values from the MessageBox your code will still work. However, there is technically nothing wrong with using the ACTUAL return values in your code. It is not this that is preventing your code from working but the fact that the Document_Close Event cannot be cancelled. You therefore need to check content before the close event is called.


____________________________
multi-tasking - the ability to hang more than one app. at the same time.

06-10-2005 at 09:07 AM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VBA (Access, Excel, Word, ...) : run code when closing document?
Previous Topic (Search a keyword in txt and pull the data)Next Topic (destroying instances of Excel) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder