Share via

Access 2016 VBA question SET RST command

Anonymous
2024-08-26T13:26:49+00:00

I am trying to create a login form in MS Access 2016, Enterprise Edition. Successful login should go to an unbound form, which serves as a main menu. I am having trouble with the VBA on this...it doesn't seem to recognize the main menu object. In particular, the SET RST command seems to be sticking.

Microsoft 365 and Office | Access | Other | Other

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

8 answers

Sort by: Most helpful
  1. Tom van Stiphout 40,211 Reputation points MVP Volunteer Moderator
    2024-08-26T18:36:14+00:00

    I don't understand the question.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2024-08-26T18:11:03+00:00

    Would this replace all the existing code? or just some of it?

    Was this answer helpful?

    0 comments No comments
  3. Tom van Stiphout 40,211 Reputation points MVP Volunteer Moderator
    2024-08-26T17:04:36+00:00

    ' If rst.EOF Then

    MsgBox prompt:="Incorrect username/password. Try again.", buttons:=vbCritical, title:="Login Error"

    Me.txt_username.SetFocus

    Else

    MsgBox prompt:="Hello, " & rst.Fields(0).Value & ".", buttons:=vbOKOnly, title:="Login Successful"

    DoCmd.Close acForm, "frm_login", acSaveYes

    End If

    Do you see it? You comment out the first line, and must also comment out the rest of this IF block.

    Also: add "Option Explicit" to the top of every module, and use VBA window > Tools > Options > Require variable declaration to make it the default for new modules.

    Then Debug > Compile and fix any issues (mostly: add variable declarations).

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2024-08-26T16:02:28+00:00

    Here is the actual code

    Option Compare Database

    Private Sub cmd_cancel_Click()

    DoCmd.Quit acQuitSaveAll

    End Sub

    Private Sub cmd_login_Click()

    Dim db As DAO.Database

    Dim rst As DAO.Recordset

    Dim strSQL As String

    ' mdl=forms!opening screen

    If Trim(Me.txt_username.Value & vbNullString) = vbNullString Then

    MsgBox prompt:="Username should not be left blank.", buttons:=vbInformation, title:="Username Required" 
    
    Me.txt\_username.SetFocus 
    
    Exit Sub 
    

    End If

    If Trim(Me.txt_password.Value & vbNullString) = vbNullString Then

    MsgBox prompt:="Password should not be left blank.", buttons:=vbInformation, title:="Password Required" 
    
    Me.txt\_password.SetFocus 
    
    Exit Sub 
    

    End If

    'query to check if login details are correct

    strSQL = "SELECT strName FROM tbl_login WHERE Username = """ & Me.txt_username.Value & """ AND Password = """ & Me.txt_password.Value & """"

    ' Set db = CurrentDb

    DoCmd.OpenForm ("Opening Screen")

    ' Set rst = db.OpenRecordset(strSQL)

    ' Set rst = db.OpenForm("Opening Screen")(strSQL)

    ' If rst.EOF Then

    MsgBox prompt:="Incorrect username/password. Try again.", buttons:=vbCritical, title:="Login Error" 
    
        Me.txt\_username.SetFocus 
    
    Else 
    
        MsgBox prompt:="Hello, " & rst.Fields(0).Value & ".", buttons:=vbOKOnly, title:="Login Successful" 
    
        DoCmd.Close acForm, "frm\_login", acSaveYes 
    
    End If 
    

    Set db = Nothing

    Set rst = Nothing

    End Sub

    Private Sub Form_AfterUpdate()

    End Sub

    Private Sub txt_username_AfterUpdate()

    End Sub

    Error Message:

    Note that I am a really novice user of VBA, although I have a long history with Access itself. I am trying to go line by line, ensuring that object names are correct, etc. I have a simple database that will have about 5 users, and occasional guests. I am trying to create a login screen that goes to an unbound form once successful login occurs. There is no back end (and won't be), as I'm not concerned with users modifying objects.

    Thank you for your help.

    Heide

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2024-08-26T15:24:01+00:00

    Can you provide the full code with which you are experiencing difficulty.  Also, is the problem raising an error, and if so, what is the error message?

    Was this answer helpful?

    0 comments No comments