Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Ant_Snell is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2021
    Location
    UK
    Posts
    118

    Problem creating a TempVars

    I am trying to create a TempVars that holds the “Permission” Variable for use in code when opening forms etc. After watching numerous YouTube videos I managed to create a log in screen and save the rs variable as TempVars “CurrentUserID” that all works fine so thought it would be fairly simple task to copy the code that works parrot fashion, change a few names to create a TempVars “UserPermission” but I just cant get it to work. I confess I have very little understanding of code and have tried several things with no success.

    Any help appreciated.

    Click image for larger version. 

Name:	Capture.PNG 
Views:	13 
Size:	76.7 KB 
ID:	47222

    Code:
    Private Sub btnLogIn_Click()
    
    
        Dim rs As Recordset
        Set rs = CurrentDb.OpenRecordset("tbl_Emp_Details", dbOpenSnapshot, dbReadOnly)
        rs.FindFirst "User_Name='" & Me.txtUserName & "'"
        
        Dim ap As Recordset
        Set ap = CurrentDb.OpenRecordset("tbl_Emp_Details", dbOpenSnapshot, dbReadOnly)
        ap.FindFirst "Emp_ID='" & Me.txtUserID & "'"
            
        If rs.NoMatch = True Then
        Me.labWrongUser.Visible = True
        Me.txtUserName.SetFocus
        Exit Sub
        
      End If
        Me.labWrongUser.Visible = False
      
        If rs!User_Password <> Nz(Me.txtPassword, "") Then
        Me.LabWrongPW.Visible = True
        Me.txtPassword.SetFocus
        Exit Sub
      End If
      
        TempVars.Add "CurrentUserID", Me.txtUserID.Value
        'MsgBox TempVars![currentUserID]
        TempVars.Add "UserPermission", Me.txtPermission.Value
        'MsgBox TempVars![UserPermission]
        
        
        Me.LabWrongPW.Visible = False
        DoCmd.OpenForm "frm_Index_Main"
        DoCmd.Close acForm, "frm_Login"
    End Sub
    .

  2. #2
    moke123's Avatar
    moke123 is online now Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Is the Log In form bound to the table and txtPermission bound to the field?
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  3. #3
    orange's Avatar
    orange is online now Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    You need to start indenting your code properly?
    Pretty much required to assist with finding errors. Google Smart Indenter, to indent what you have already.

    Why two recordsets for the same table?
    An ID to me is always numeric?, so no need for the single quotes?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    Ant_Snell is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2021
    Location
    UK
    Posts
    118
    It's stopping at the yellow line. ap.FindFirst "Emp_ID='" & Me.txtUserID & "'"

    The form is not bound to any table.

    Thanks

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Quote Originally Posted by Ant_Snell View Post
    It's stopping at the yellow line. ap.FindFirst "Emp_ID='" & Me.txtUserID & "'"

    The form is not bound to any table.

    Thanks
    So nothing to do with Tempvars then?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  7. #7
    Ant_Snell is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2021
    Location
    UK
    Posts
    118
    The error message is " Data mismatch in criteria expression"

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Quote Originally Posted by Ant_Snell View Post
    The error message is " Data mismatch in criteria expression"
    I've told you the likely cause of that, even without the error message?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  9. #9
    Ant_Snell is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2021
    Location
    UK
    Posts
    118
    So nothing to do with Tempvars then? Not sure, I am trying to declare the variable ap then save it a a TempVars later on at "TempVars.Add "UserPermission", Me.txtPermission.Value" but as I say not expert at VBA and just copying parrot fashion

  10. #10
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Quote Originally Posted by Ant_Snell View Post
    So nothing to do with Tempvars then? Not sure, I am trying to declare the variable ap then save it a a TempVars later on at "TempVars.Add "UserPermission", Me.txtPermission.Value" but as I say not expert at VBA and just copying parrot fashion
    That will not work.
    You need to take the time to understand the code, to some degree at least? Otherwise it is only going to slow you down even more and will frustrate you more and more.
    Numbers do not need to be surrounded by anything when concatenating
    Text needs a single quote, as you had for the name, or triple " if the name could contain a ' like O'Leary ? So your code is going to fail there as well if that occurs.
    Dates need # and be in mm/dd/yyyy or yyyy-mm-dd format
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  11. #11
    orange's Avatar
    orange is online now Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Emp_ID and Permission are both Numeric (right justified) in your table.

    Try removing the single quotes as welshgasman suggested.

    Code:
     ap.FindFirst "Emp_ID='" & Me.txtUserID & "'"

  12. #12
    Ant_Snell is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2021
    Location
    UK
    Posts
    118
    @ Welshgasman Agreed! I signed up to a LinkedIn VBA course but did not find it very intuitive. I now tend to YouTube bits as and when needed but but that does lead to doing copying code without understanding the syntax. I did understand that "Data mismatch in criteria expression" was referring to numbers V text etc. so assumed that "Dim As Recordset" (used in the first should probably be "Dim As Integer" but then got stuck on the next line. I will push on to see if I can sort Thanks

  13. #13
    orange's Avatar
    orange is online now Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    @ant,
    There are some great vba and database tutorials on youtube.
    See the Database Planning and Design link in my signature.
    For Access and vba the tutorials series by Steve Bishop and Ronald Rost are very good.

  14. #14
    Ant_Snell is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2021
    Location
    UK
    Posts
    118
    @ orange Thanks I will look at these authors ( I do think I have seen a few of S Bishop's)

    I have now removed the signal quotations as suggested and the code as moved on to

    [code]
    empVars.Add "UserPermission", Me.txtPermission.Value
    MsgBox TempVars![UserPermission]
    [\code]

    It now errors with "invalid use null" at the message box line

  15. #15
    Ant_Snell is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2021
    Location
    UK
    Posts
    118
    TempVars.Add "UserPermission", Me.txtPermission.Value
    MsgBox TempVars![UserPermission]

    That was

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 06-23-2020, 10:37 PM
  2. How to use Tempvars in query
    By JrMontgom in forum Programming
    Replies: 3
    Last Post: 09-03-2014, 12:36 PM
  3. TempVars.add and make value ID
    By Ruegen in forum Programming
    Replies: 4
    Last Post: 12-31-2013, 06:19 PM
  4. TempVars monitoring for debug
    By chris.williams in forum Programming
    Replies: 2
    Last Post: 11-18-2011, 03:27 PM
  5. Using TempVars in query
    By jonesy29847 in forum Programming
    Replies: 3
    Last Post: 03-03-2011, 11:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums