954,582 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to ensure perfect error handling

Hi there all

I'm looking for a way to ensure that users don't exit my application before they complete all outstanding calendar entries. It is a job book application we use in office. The thing is that users only complete the day's entry in the calendar, and then leave the rest out.

In my code you will see this, but is there a way to ensure that user first completes all his outstanding entries, before closing the form?Please?

Coding:

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

If UnloadMode = vbFormControlMenu Then

If TMcnt <> 0 Then

If CDate(TMdata(TMcnt - 1).EDate) < DateValue(Now) - 1 Then

TimeManage.Calendar1.Value = DateValue(CDate(TMdata(TMcnt - 1).EDate)) + 1
Call refresh_tmdata
MsgBox "Please complete outstanding LogBook entries before you can continue", , "Important!"
Cancel = True
Else

TimeManage.Hide
JobBook.Show

End If

Else

TimeManage.Calendar1.Value = DateValue(CDate("2009/06/01"))
Call refresh_tmdata
MsgBox "Please complete outstanding LogBook entries before you can continue", , "Important!"
Cancel = True
End If

End If

End Sub

TheDocterd
Junior Poster
138 posts since Oct 2010
Reputation Points: 17
Solved Threads: 0
 

Give us some more info on your named instances. What is TMData, TMcnt etc.

What code do you have under refresh_tmdata?
What calender control is Calender1?

AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

TMData and TMcnt is just local variables i declared like this:

Public TMdata() As TM
Public TMcnt As Integer

The code i have under refresh_tmdata is like this:

Private Sub refresh_tmdata()

Dim yn As VbMsgBoxResult

EntryList.Clear

etime = 0

For i = 0 To TMcnt - 1

If Calendar1.Value = CDate(TMdata(i).EDate) Then


If TMdata(i).OT <> "" Then
etime = etime + CSng(TMdata(i).OT)
End If

If TMdata(i).ST <> "" Then
etime = etime + CSng(TMdata(i).ST)
End If

EntryList.AddItem (TMdata(i).JobCode & " ," & TMdata(i).TMDate)

End If


Next i

Tlabel.Caption = etime & " / 8.5"

If etime < 8.5 Then
Tlabel.ForeColor = vbRed
Else
Tlabel.ForeColor = vbWhite
End If

End Sub

The calendar is just n normal calendar i inserted into the application via the toolbox of visual basic 6. And it has the name of calendar 1

TheDocterd
Junior Poster
138 posts since Oct 2010
Reputation Points: 17
Solved Threads: 0
 

Does this part here below change the label fore color to white if a task has been completed, or it stays red if a task is still outstanding?

If etime < 8.5 Then
Tlabel.ForeColor = vbRed
Else
Tlabel.ForeColor = vbWhite
End If
AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

it changes to white if the working hours are completed (in this case 8.5 hours), if not completed it stays red

TheDocterd
Junior Poster
138 posts since Oct 2010
Reputation Points: 17
Solved Threads: 0
 

In other words, if a calender entry is still outstanding, the label will be red?

AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

that is correct ;)

TheDocterd
Junior Poster
138 posts since Oct 2010
Reputation Points: 17
Solved Threads: 0
 

Cool, then your problem is very easily resolved.

In a module add the following code...
Option Explicit

Public Sub CloseMyApplication(frm As Form)

Dim Control As Control

 For Each Control In frm.Controls
        If TypeOf Control Is Label Then
            If Label.ForeColour = vbRed Then
               'Code here NOT to close the app
                   Else
               'All entries or tasks is completed, close the application
            End If
        End If
       
    Next Control
End Sub

'In your form the following...
Call CloseMyApplication (Me)


Let me know if this helps.:)

AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

Thanks for the code it looks like it might just work but I get an error that says: 'object required'. And when I debug it it goes to the part where the code says: 'If Tlabel.ForeColor = vbRed Then'

This is the code I did....

Public Sub CloseMyApplication(frm As Form)

Dim Control As Control

For Each Control In TimeManage.Controls

If TypeOf Control Is Label Then

If Tlabel.ForeColor = vbRed Then

MsgBox "Please complete all outstanding entries", , "Message"
Cancel = True

Else

If Tlabel.ForeColor = vbWhite Then

JobBook.Show
TimeManage.Hide

End If
End If
End If

Next Control

End Sub

Perhaps it could be the part of 'Option Explicit'. Where should I put that piece of code?

TheDocterd
Junior Poster
138 posts since Oct 2010
Reputation Points: 17
Solved Threads: 0
 

Oh and i also put the code where I call this method in the form so all that is correct.

TheDocterd
Junior Poster
138 posts since Oct 2010
Reputation Points: 17
Solved Threads: 0
 

My bad!!! I mixed up the code a bit. You have declared Control as the handler. We then used label to put the call to it, shame on me:)

Paste the following in your module -

In a module add the following code...
Option Explicit
 
Public Sub CloseMyApplication(frm As Form)
 
Dim MyLabel As Control
 
 For Each Control In frm.Controls
        If TypeOf Control Is Label Then
            If MyLabel.ForeColour = vbRed Then
               'Code here NOT to close the app
                   Else
               'All entries or tasks is completed, close the application
            End If
        End If
 
    Next Control
End Sub
 
'In your form the following...
Call CloseMyApplication (Me)


Or, to use yours, the following -

Public Sub CloseMyApplication(frm As Form)

Dim MyLabel As Control

For Each Control In TimeManage.Controls

If TypeOf Control Is Label Then

If MyLabel.ForeColor = vbRed Then

MsgBox "Please complete all outstanding entries", , "Message"
Cancel = True

Else

If MyLabel.ForeColor = vbWhite Then

JobBook.Show
TimeManage.Hide

End If
End If
End If

Next Control

End Sub
AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

I still get the same error..Just one question...

The 'MyLabel', is that the name of the label in my form??

TheDocterd
Junior Poster
138 posts since Oct 2010
Reputation Points: 17
Solved Threads: 0
 

No, that reference all the labels in your Form. It will only effect the labels with a white or red backcolour though.

What error do you get and on which line?

AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

Okay I got that one now thanks. The error i get is in the following line:

If MyLabel.ForeColor = vbRed Then

The error message is:

'Run-time error 01', 'Object Variable or With Block not set'

And lastly I call the mothod in my form with the following:

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Call CloseMyApplication(Me)
End Sub

TheDocterd
Junior Poster
138 posts since Oct 2010
Reputation Points: 17
Solved Threads: 0
 

Use the code exactly as it is here. It works fine. Do NOT change your form name, label names etc.:)

Public Sub CloseMyApplication(frm As Form)
 
Dim MyLabel As Control
 
For Each MyLabel In frm.Controls
    If TypeOf MyLabel Is Label Then
        If MyLabel.ForeColor = vbRed Then
            MsgBox "Please complete all outstanding entries", , "Message"
            Cancel = True
                Else
        End If
    
    If MyLabel.ForeColor = vbWhite Then
        JobBook.Show
        TimeManage.Hide
    End If
    End If
 
Next MyLabel
End Sub
AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

That works perfectly :) thanks a lot for the help!

I work through a calendar. This means that each day there has to be 8.5 hours completed. Is there perhaps a way I can add code handling from lets say January 2010 up to the current day??. In other words the code should check from January 2010 up to the current day if all hours are 8.5 before the form closes.

We just have to modify the code you have just guven me.

Thanks

TheDocterd
Junior Poster
138 posts since Oct 2010
Reputation Points: 17
Solved Threads: 0
 

It was a pleasure. Please mark this thread as solved and open a new thread with your calender question. We'll take it from there then. The reason being to keep questions and their solutions together, thanks.:)

AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You