Results 1 to 2 of 2
  1. #1
    wazimu13 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Oct 2014
    Posts
    4

    Unhappy Dynamic query to show each user only records relevant to them on logging in

    I have an access 2010 web database, modified from a microsoft template (Desktop inventory database) that several users will need to log in to. The database contains a table of products.
    The challenge is, every user needs to only see a subset of these products and never see the whole list.
    At the moment i have some code to modify an existing query based on the logged in user's details. As they log in, some tempvars are created and these are used to modify the query criteria.
    This works well when the first user logs in, but the moment the next user logs in, the query is modified again and the product list refreshes and now his products are shown and not the first users! Im thinking i need to dynamically create a permanent query for each user on log in?
    Or is a better way to accomplish what im trying ? im quite new to access and struggling. Can anyone assist please?




    Here is my code so far:

    Button on login form has the following code that collects the user's details

    Code:
    Private Sub cmdLoginMine_Click()
    Dim ID as long, strEmpName as string,strZondsc as string,strgrpdsc as string
    
    ID = DLookup("ID", "Employees", "Login='" & Me.txtUser.Value & "'")
    strEmpName = DLookup("FullName", "Employees", "Login='" & Me.txtUser.Value & "'")
    strgrpdsc = DLookup("MyGrpdscs", "Employees", "Login='" & Me.txtUser.Value & "'")
    strzondsc = DLookup("MyZondscs", "Employees", "Login='" & Me.txtUser.Value & "'")
    
    TempVars.Add "tmpEmployeeID", ID
    TempVars.Add "tmpEmployeeName", txtUser.Value
    I then call a function that modifies the existing query, populating it with this users details for the criteria


    Code:
    qryEdit strgrpdsc, strzondsc, ID
    
    
    Sub qryEdit(strgrpdsc As String, strzondsc As String, ID As Long)
    Dim qdf As DAO.QueryDef
    Dim qdfOLD As String
    
    Set qdf = CurrentDb.QueryDefs("InventoryQryforDS")
        With qdf
            .SQL = "SELECT Products.ProductCode, Products.ProductName, Products.GRPDSC, Categories.Category, Inventory.Available " & _
                   "FROM (Categories INNER JOIN Products ON Categories.ID = Products.CategoryID) INNER JOIN Inventory ON Products.ID = Inventory.ProductID " & _
                   "WHERE Products.GRPDSC in (" & strgrpdsc & ") and Categories.Category in (" & strzondsc & ") and products.ownersid =" & ID & _
                   " ORDER BY Products.ProductCode"
    
    
        End With
    Set qdf = Nothing
    
    End Sub
    The results of the query are shown on a form, which is what is currently requerying and showing the wrong data.
    THe data is shown on a form, linked to one of the new style navigation buttons as shown.Therecordsource property of the form is the query that's populated as described above.

    thanks

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I would use this method to see how the SQL is ending up:

    BaldyWeb-Immediate window

    that should show you what value is wrong in it.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 10-09-2014, 12:25 AM
  2. Replies: 17
    Last Post: 07-31-2013, 11:35 AM
  3. Replies: 6
    Last Post: 06-26-2012, 12:16 PM
  4. User Logging System
    By GraemeG in forum Programming
    Replies: 2
    Last Post: 03-25-2011, 03:27 AM
  5. Show past records for same user
    By l3111 in forum Database Design
    Replies: 3
    Last Post: 03-03-2011, 10:57 AM

Tags for this Thread

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