Please do not continue reposting this same question. It's already been answered multiple times.
If you are still confused, continue the discussion in this same thread.
I have locked other attempts at the same question to prevent further confusion.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.
Please do not continue reposting this same question. It's already been answered multiple times.
If you are still confused, continue the discussion in this same thread.
I have locked other attempts at the same question to prevent further confusion.
The code is sitting out in nowhere.
Most code needs to be within a function or procedure, hence the error message.
If this code is supposed to run at the click of a button (named Command1), then it should be between the Private Sub...End Sub wrapper.
If it is supposed to be triggered by a different event, it needs a Sub...End Sub or Function...End Function wrapper.
Scott's right on the money! (No surprise there)
I suspect you accidentally drag and dropped the code from within the sub to outside the sub. Just Select all the code
txtUserName ....
....
End If
and cut it, then move your cursor below the Private Sub .... line (on the blank line) and paste.
Private Sub Command1_Click()
'Place the code here!
End Sub
now everything should be in order.
You should end up with
Option Compare Database
Private Sub Command1_Click()
txtUserName .SetFocus
...
End If
End Sub
You should get in the habit of always including Option Explicit in all your VBA Module headers, and giving your controls meaningful names. Command1 doesn't help you know which button it is. cmd_Login is very informative and will help you when reviewing code ate later dates.
I get the feeling you are new to Access and VBA, you may like to review:
https://www.devhut.net/access-best-practices-and-troubleshooting-steps/ and https://www.devhut.net/ms-access-where-start-learning-database-tutorials/
Lastly, for future reference, it is not a good idea to post images of code, instead copy/paste the code directly into your question. It greatly facilitates our ability to help you.
Please could you unblock me because I'm unable to ask other questions as I'm facing other troubles as well.