Solved Determine AllowBypassKey is On or Off

How can I define the label?
 
you don't define it as a label is a type of control; you add it to your form in design view, you give it name using the Properties window then you can refer to it in VBA.
 
Apologies, I see now, the OP was referring the the missing "ExitHandler" VBA label....
 
1664397918756.png

I mean this label how can I defined it?
 
I did this HandlerExit
Hander_Exit
same problem I always get Label not defined
 
See this post:
Code:
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:
Code:
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
1664399226729.png
 
Post your code (using the code tags in the forum toolbar) instead of posting screenshots and someone will correct it for you, we don't want to have to type it all from scratch....
 
  • Like
Reactions: Kha
@theDBguy @bastanu
I really admire you for continuing to reply. OP keeps posting images one after another without a word. And you still keep your cool and trying your best to explain. I can feel my blood pressure rising.
 
@theDBguy @bastanu
I really admire you for continuing to reply. OP keeps posting images one after another without a word. And you still keep your cool and trying your best to explain. I can feel my blood pressure rising.
Please Don't be misunderstood, Sometimes I did not see all the words
I put the picture instead of the words, because I thought the picture was better, clearer and faster to understand
I apologize to everyone, and I thank you for your understanding, and not being misunderstood
 
I try to put colon : but still I got same error message that Label not define, can you help me please to fix the following code
Code:
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
 
Maybe 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,
 
Maybe 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,
I got this error message
1664483994230.png
 
Because you are not running the function from the form that has the txtbox text box control. Read the comment I wrote that I see you deleted:
'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
 
@Kha - here are a couple of tips for you.

The syntax of Me.txtbox.backcolor = vbGreen is actually correct BUT it has to occur in the place where that txtbox exists because the Me. prefix works ONLY in a class module (associated with a form or report). Anyplace else, Me. has no meaning. So technically your syntax was right but your semantics was wrong.

You were tossing sample code all over the place. You were getting errors about "property not found" because you were performing what is called an overload of the word "txtbox" in those samples. If there is a control on your form that is named "txtbox" then Me.txtbox is accurate and has the properties of a txtbox. BUT when, in one of your samples you defined txtbox as a string (and in another, as a property) you hid the "real" txtbox behind the variable name that you created - and simple variables don't have the properties associated with controls. I said you hid the real txtbox control because the definition of a variable named txtbox is programmatically closer to that code than the definition of the control named txtbox, and in Access VBA, the closest definition wins.

First, you need to chill, step back, and think about what you really want to do. Second, tell us what you wanted to do in WORDS, because posting code doesn't tell us your goal. Instead it tells us your approach, which is different - particularly when you appear to not be 100% comfortable with the syntax of what you are attempting.

We can probably help you - but you have to help us by providing clarity in what you wanted to do. Posting code without explaining the intent isn't going to do it.
 
When I open the login form "On Open event" it will check the "AllowBypassKey" , if it is "True" the txtbox will be green and when it is False the txtbox will be Red, In this case, I will never forget to disable "AllowBypassKey"
Please find the attached file as sample copy
 

Attachments

Please review the updated file. Note that I copied the code into the form module so you could refere to the textbox using the Me.txtbox. I also modified the public function you have in the standard module. You can switch between them in the Open event of the form (removed the macro and used VBA code instead) by commenting one out, you'll see they work the same.

Cheers,
 

Attachments

  • Like
Reactions: Kha

Users who are viewing this thread

Back
Top Bottom