Results 1 to 3 of 3
  1. #1
    Join Date
    May 2014
    Posts
    35
    Provided Answers: 2

    Answered: Open Form if Query returns a set value

    I have a form that on close runs a Query(Users) looking for records in a table(tblUsers), Works great returns 1 record. I then want it to look in a field of that record (UsersRights) and if 1 is in the field then it should open a form (Admin).

    Can not get this to work.

    Code:
    Private Sub Form_Close()
      
    
    DoCmd.OpenQuery "Users"
    If [tblUsers]![UsersRights] = "1" Then
        DoCmd.OpenForm "Admin"
    End If
    End Sub
    Any ideas?

  2. Best Answer
    Posted by Doug60M

    "Thanks for the help actually solved this one on my own. The code I provided worked once I had it look in a form that showed the results of the query."


  3. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    and the query is?
    is the datatype of userrights numeric or string
    the value of [tblUsers]![UsersRights] is

    have you declared option explicit or forced variable declaration as part of the Access files parameters/settigns

    personally I would expect to use a dlookup for this sort of task, not that Im a fan of domain functions, but in thsi case it may be the right thing to do.
    myusername = 'Doug60M'
    if (dlookup('usersrights','tblusers','username="' & myusername & '"') = 1)
    ...assuming that you have a variabel or control called myusername with an alphanumeric name

    or if the thing you are looking up is numeric
    myUserID = 123
    if (dlookup('usersrights','tblusers','userID=' & myuserID) = 1)

    you may need to handle NULLS (the resutl you'd get from a dlookup where the specified value isn't found

    if (nz(dlookup('usersrights','tblusers','username="' & myusername & '"'),0) = 1)

    replace myusername with whatever is the name of the control that holds the unique user information
    and whatever your column names are in place of username and or userid
    ..y'get the picture


    also as an aside. I don't like to create forms with names like admin... if you have a malicious user or someone snooping its a red rag to a bull..... if you must have these sort of forms / queries and so on giove them some anodyne name that looks boring.

    also consider placing some code in the form / reports event that also checks the that the user has permission to open the form. there is feck all point in testing permissions to open a form if a user can open the form through other means.. either directly from the project window / object view or by knocking up a quick and dirty form that opens your form.

    also if its worth protecting then dont' allow users to open an unencrypted version of the db, only allow encrypted access (ACCDE or MDE, NOT ACCDB or MDE)
    Last edited by healdem; 01-04-16 at 15:14.
    I'd rather be riding on the Tiger 800 or the Norton

  4. #3
    Join Date
    May 2014
    Posts
    35
    Provided Answers: 2
    Thanks for the help actually solved this one on my own. The code I provided worked once I had it look in a form that showed the results of the query.

Posting Permissions

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