Private sub Foo()
On Error GoTo ErrHandler
'do stuff
ExitSub:
' clean up before exiting
Exit Sub
ErrHandler:
ErrorHandler.messageBox "ThisModuleName","Foo"
Resume ExitSub
End Sub
See this post:
VBA - On Error GoTo ErrHandler:
I have a simple question about error-handling in VBA. I know how to use the On Error GoTo ErrHandler statement but instead using my own code at the specified label, I would rather use a prefabricat...stackoverflow.comCode:Private sub Foo() On Error GoTo ErrHandler 'do stuff ExitSub: ' clean up before exiting Exit Sub ErrHandler: ErrorHandler.messageBox "ThisModuleName","Foo" Resume ExitSub End Sub
You define a label by adding a colon ":" after its name.I mean this label how can I defined it?
Please Don't be misunderstood, Sometimes I did not see all the words
Public Function ABKey()
On Error GoTo ErrHandler
Dim txtbox As Access.Control
Dim db As Property
Dim strErrMsg As String
Dim obj As Property
Dim strPropertyName As String
Dim varValue As String
Set db = CurrentDb
If db.Properties("AllowBypassKey") = True Then
txtbox.BackColor = vbGreen
txtbox = "On"
Else
txtbox.BackColor = vbRed
txtbox = "Off"
End If
' I get this ErrHanler from Internet
ErrHandler:
strErrMsg = strErrMsg & obj.Name & "." & strPropertyName & " not set to " & varValue & _
". Error " & Err.Number & " - " & Err.Description & vbCrLf
Resume ExitErrHandler:
End Function
Public Function ABKey()
'On Error GoTo ErrHandler
'Dim txtbox As Access.Control you don't need this line if the function runs in the form that contains a textbox control NAMED txtbox; you use Me.txtbox to refer to it; if this is in a standard module you need to use Forms!frmYourFormName!txtbox syntax
Dim db As DAO.Database 'Property
Dim strErrMsg As String, boAllowValue as boolean
'Dim obj As Property 'not used anywhere
'Dim strPropertyName As String
'Dim varValue As String
Set db = CurrentDb
'need to check if the property exists
On Error Resume Next
boAllowValue = db.Properties("AllowBypassKey").Value
Select Case Err.Number
Case 0
' The property exists
If boAllowValue = True Then
Me.txtbox.BackColor = vbGreen
Me.txtbox = "On"
Else
Me.txtbox.BackColor = vbRed
Me.txtbox = "Off"
End If
Case 3270
' The property doesn't exist
me.txtbox.BackColor = vbYellow
Me.txtbox = "Not set"
Case Else
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
End Select
On Error GoTo Err_Handler ' reset to normal error-handler
'some more code here if needed
ExitErrHandler: 'this is the missing label - you want to exit here if no error is encountered
Exit Function
' I get this ErrHanler from Internet
ErrHandler:
'strErrMsg = strErrMsg & obj.Name & "." & strPropertyName & " not set to " & varValue & _
". Error " & Err.Number & " - " & Err.Description & vbCrLf 'you are not setting the obj, strPorpertyName or varValue anywhere and you are not using this strin with a message box
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
Resume ExitErrHandler:
End Function
I got this error messageMaybe try this updated version:
Code:Public Function ABKey() 'On Error GoTo ErrHandler 'Dim txtbox As Access.Control you don't need this line if the function runs in the form that contains a textbox control NAMED txtbox; you use Me.txtbox to refer to it; if this is in a standard module you need to use Forms!frmYourFormName!txtbox syntax Dim db As DAO.Database 'Property Dim strErrMsg As String, boAllowValue as boolean 'Dim obj As Property 'not used anywhere 'Dim strPropertyName As String 'Dim varValue As String Set db = CurrentDb 'need to check if the property exists On Error Resume Next boAllowValue = db.Properties("AllowBypassKey").Value Select Case Err.Number Case 0 ' The property exists If boAllowValue = True Then Me.txtbox.BackColor = vbGreen Me.txtbox = "On" Else Me.txtbox.BackColor = vbRed Me.txtbox = "Off" End If Case 3270 ' The property doesn't exist me.txtbox.BackColor = vbYellow Me.txtbox = "Not set" Case Else MsgBox Err.Description, vbExclamation, "Error " & Err.Number End Select On Error GoTo Err_Handler ' reset to normal error-handler 'some more code here if needed ExitErrHandler: 'this is the missing label - you want to exit here if no error is encountered Exit Function ' I get this ErrHanler from Internet ErrHandler: 'strErrMsg = strErrMsg & obj.Name & "." & strPropertyName & " not set to " & varValue & _ ". Error " & Err.Number & " - " & Err.Description & vbCrLf 'you are not setting the obj, strPorpertyName or varValue anywhere and you are not using this strin with a message box MsgBox Err.Description, vbExclamation, "Error " & Err.Number Resume ExitErrHandler: End Function
Cheers,
'Dim txtbox As Access.Control you don't need this line if the function runs in the form that contains a textbox control NAMED txtbox; you use Me.txtbox to refer to it; if this is in a standard module you need to use Forms!frmYourFormName!txtbox syntax