Why is my code showing invalid outside procedure?

Anonymous
2025-01-20T13:52:44+00:00

This is the image it comes up as and I'm not sure sure how to solve the error

Microsoft 365 and Office | Access | For education | Windows

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.

0 comments No comments
{count} votes
Answer accepted by question author
  1. George Hepworth 22,300 Reputation points Volunteer Moderator
    2025-01-20T18:43:07+00:00

    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.

    1 person found this answer helpful.
    0 comments No comments

16 additional answers

Sort by: Most helpful
  1. ScottGem 68,780 Reputation points Volunteer Moderator
    2025-01-20T14:08:07+00:00

    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.

    0 comments No comments
  2. Anonymous
    2025-01-20T14:33:17+00:00

    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.

    0 comments No comments
  3. Anonymous
    2025-01-21T21:20:12+00:00

    Please could you unblock me because I'm unable to ask other questions as I'm facing other troubles as well.

    0 comments No comments
  4. Anonymous
    2025-01-21T21:22:13+00:00

    How do I sort out this issue as I want to delete the faded files?

    0 comments No comments