Results 1 to 7 of 7

Thread: Help with code

  1. #1
    Join Date
    May 2015
    Posts
    8

    Answered: Help with code

    What I am trying to do is when the form opens it checks to see who is logged in then automatically fills in the users last name into current database from the system log in information I keep getting a runtime error 3251

    Here is the code
    No code has to be inserted here.

    No code has to be inserted here.

  2. Best Answer
    Posted by Sinndho

    "
    Code:
    Private Sub OfficerLast()
    
        Dim dbs As DAO.Database
        Dim rst As DAO.Recordset
        
        Set dbs = CurrentDb
        Set rst = dbs.OpenRecordset("System Login")
        rst.FindFirst "[Login Status] = '*Yes*'" ' Can the [Login Status] contain the litteral value *Yes* or do you mean:
                                                 '
                                                 ' rst.FindFirst "[Login Status] Like '*Yes*'" ?
                                                 '
        If rst.NoMatch Then
            MsgBox "No Officer Logged In to the System"
        Else
            [Login Status] = Yes                 ' What's [Login Status]
                                                 '
                                                 ' a) A column in the [System Login] table
                                                 '    In such a case you should use:
                                                 '
                                                 '    Set rst = dbs.OpenRecordset("System Login", dbOpenDynaset)
                                                 '    - - -
                                                 '    Else
                                                 '        rst.Edit
                                                 '        rst![Login Status] = "Yes"    ' Unless Yes is the name of a variable.
                                                 '        rst.Update
                                                 '   - - -
                                                 '
                                                 ' b) A control on the form
                                                 '    In such a case and to prevent any ambiguity you should use:
                                                 '
                                                 '    Me![Login Status] = "Yes"    ' Unless Yes is the name of a variable.
                                                 '
            rst.FindFirst "[Officer Last Name] = [Officer Last Name]"   ' We can suppose that [Officer Last Name] is the name of a column in the table [System Login].
                                                                        ' It can also be asserted that this column is defined as Text.
                                                                        ' Then and to the very least, the statement should be written:
                                                                        '
                                                                        ' rst.FindFirst "[Officer Last Name] = '[Officer Last Name]'"
                                                                        '
                                                                        ' However, it's very unlikely that the column [Officer Last Name]  would actually contain the
                                                                        ' litteral value [Officer Last Name]. So we are back to the same question as previously:
                                                                        ' What's the second [Officer Last Name]?
                                                                        '
                                                                        ' If it's a control on the form, you should use:
                                                                        '
                                                                        ' rst.FindFirst "[Officer Last Name] = '" & Me![Officer Last Name] & "'"
                                                                        '
                                                                        ' Otherwise, what can it be?
                                                                        '
            Me.txtOfficerLast.Value = [Officer Last Name]               ' If you want to assign the contents of the column [Officer Last Name] (from the recordset)
                                                                        ' to the control txtOfficerLast, you should use:
                                                                        '
                                                                        ' Me.txtOfficerLast.Value = rst![Officer Last Name]
                                                                        '
        End If
        rst.Close
        Set rst = Nothing
        Set dbs = Nothing
    
    End Sub
    "


  3. #2
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    What code do you use :

    Here is the code
    No code has to be inserted here.

    No code has to be inserted here.
    Have a nice day!

  4. #3
    Join Date
    May 2015
    Posts
    8
    Sorry about that. Here the code.

    Private Sub txtOfficerLast_Enter()
    Call OfficerLast
    End Sub

    Private Sub OfficerLast()
    Dim dbs As DAO.Database
    Dim rst As DAO.Recordset

    Set dbs = CurrentDb
    Set rst = dbs.OpenRecordset("System Login")

    rst.FindFirst "[Login Status] = '*Yes*'"

    If rst.NoMatch Then
    MsgBox "No Officer Logged In to the System"
    GoTo Cleanup

    Else: [Login Status] = Yes
    rst.FindFirst "[Officer Last Name] = [Officer Last Name]"
    Me.txtOfficerLast.Value = [Officer Last Name]
    End If

    Cleanup:
    rst.Close
    Set rst = Nothing
    Set dbs = Nothing

    End Sub

  5. #4
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    Do you mean:
    Code:
    rst.FindFirst "[Officer Last Name] = '" & [Officer Last Name] & "'"
    Have a nice day!

  6. #5
    Join Date
    May 2015
    Posts
    8
    I tried that piece of code and I still get a error. Am trying to call information from a table that has the Officer set values and when then login it changes the status to yes then when they open the form it gets the officer last from that table and fills in a text box on the forms they load so they don't have to type that information over and over. I have tried different codes but still get the run time error.

  7. #6
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    and the error is reported on what line?
    Im assuming the error is still RTE 3251
    presumably 'System Login' is the name of a table, what columns exist in this table (name and datatype)
    is it a linked table, if so you will almost certainly need to defien what ype of recordset it is... look at the options for the openrecordset method
    I'd rather be riding on the Tiger 800 or the Norton

  8. #7
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    Code:
    Private Sub OfficerLast()
    
        Dim dbs As DAO.Database
        Dim rst As DAO.Recordset
        
        Set dbs = CurrentDb
        Set rst = dbs.OpenRecordset("System Login")
        rst.FindFirst "[Login Status] = '*Yes*'" ' Can the [Login Status] contain the litteral value *Yes* or do you mean:
                                                 '
                                                 ' rst.FindFirst "[Login Status] Like '*Yes*'" ?
                                                 '
        If rst.NoMatch Then
            MsgBox "No Officer Logged In to the System"
        Else
            [Login Status] = Yes                 ' What's [Login Status]
                                                 '
                                                 ' a) A column in the [System Login] table
                                                 '    In such a case you should use:
                                                 '
                                                 '    Set rst = dbs.OpenRecordset("System Login", dbOpenDynaset)
                                                 '    - - -
                                                 '    Else
                                                 '        rst.Edit
                                                 '        rst![Login Status] = "Yes"    ' Unless Yes is the name of a variable.
                                                 '        rst.Update
                                                 '   - - -
                                                 '
                                                 ' b) A control on the form
                                                 '    In such a case and to prevent any ambiguity you should use:
                                                 '
                                                 '    Me![Login Status] = "Yes"    ' Unless Yes is the name of a variable.
                                                 '
            rst.FindFirst "[Officer Last Name] = [Officer Last Name]"   ' We can suppose that [Officer Last Name] is the name of a column in the table [System Login].
                                                                        ' It can also be asserted that this column is defined as Text.
                                                                        ' Then and to the very least, the statement should be written:
                                                                        '
                                                                        ' rst.FindFirst "[Officer Last Name] = '[Officer Last Name]'"
                                                                        '
                                                                        ' However, it's very unlikely that the column [Officer Last Name]  would actually contain the
                                                                        ' litteral value [Officer Last Name]. So we are back to the same question as previously:
                                                                        ' What's the second [Officer Last Name]?
                                                                        '
                                                                        ' If it's a control on the form, you should use:
                                                                        '
                                                                        ' rst.FindFirst "[Officer Last Name] = '" & Me![Officer Last Name] & "'"
                                                                        '
                                                                        ' Otherwise, what can it be?
                                                                        '
            Me.txtOfficerLast.Value = [Officer Last Name]               ' If you want to assign the contents of the column [Officer Last Name] (from the recordset)
                                                                        ' to the control txtOfficerLast, you should use:
                                                                        '
                                                                        ' Me.txtOfficerLast.Value = rst![Officer Last Name]
                                                                        '
        End If
        rst.Close
        Set rst = Nothing
        Set dbs = Nothing
    
    End Sub
    Have a nice day!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •