Results 1 to 3 of 3
  1. #1
    ismith is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2011
    Posts
    39

    How to check if a form is open.

    Hello,



    Im making an order system with a multi log in user page. The owner and staff can log on. Atm ive got it working so that if a staff logs on it goes to their main menu and if the owner logs on it goes to the owner menu.

    To do this ive used the following code:

    Code:
    Public Sub btnLogin_Click()
    
        'This defines the attributes that are going to be used for the login page.
        'dbs is set as the Database, ownerPwd as a Record and matchFound as Boolean
        'This is so that I can assign values to them accordingly.
        Dim dbs As Database
        Dim ownerPwd As Recordset
        Dim staffPwd As Recordset
        Dim matchFound As Boolean
        Dim staffMatchFound As Boolean
        
        'dbs is set to the Database that I have created in Access.
        'I created a query in access so ive used that query to be set as ownerPwd.
        Set dbs = CurrentDb
        Set ownerPwd = dbs.OpenRecordset("qryUserPass")
        Set staffPwd = dbs.OpenRecordset("tblStaffLogin")
        
        'Set this as False so I can change it to True when the If statement is met.
        matchFound = False
        staffMatchFound = False
        'This checks in the Table to see if there is any data. It then moves to
        'the first row of data in the table.
        If ownerPwd.RecordCount > 0 Then
        ownerPwd.MoveFirst
        
        If staffPwd.RecordCount > 0 Then
        staffPwd.MoveFirst
        
        'This loop carries on while ownerPwd is False.
        'It looks into the query for ownerPwd and checks to see if the Username and Password
        'field and the same as what has been entered in the text boxes.
        'If it is the same it sets matchFound to True and breaks out of the loop.
        'If it is not then it moves onto the next row and does the loop again.
        Do While ownerPwd.EOF = False
        If ownerPwd![UserName] = Form_frmLogin.txtUsername.Value And ownerPwd![Password] = Form_frmLogin.txtPassword.Value Then
            matchFound = True
            Exit Do
        End If
            ownerPwd.MoveNext
        Loop
     End If
     
         Do While staffPwd.EOF = False
        If staffPwd![UserName] = Form_frmLogin.txtUsername.Value And staffPwd![Password] = Form_frmLogin.txtPassword.Value Then
            staffMatchFound = True
            Exit Do
        End If
            staffPwd.MoveNext
        Loop
     End If
     
     'This tells the program to close the Login form and open the main menu IF
     'the Username and Password is correct.
     If matchFound = True Then
        DoCmd.Close acForm, "frmLogin"
        DoCmd.OpenForm "frmMainMenu"
    
    'If the Username and Password is incorrect then it shows the following message in a dialogue box.
     ElseIf staffMatchFound = True Then
        DoCmd.Close acForm, "frmLogin"
        DoCmd.OpenForm "frmStaffMainMenu"
        
        Else
        
        MsgBox "Incorrect Username or Password, Please try again", vbCritical, "Error"
    End If
    
    'This closes the database that I opened at the start
    ownerPwd.Close
    staffPwd.Close
    
    End Sub
    Now thats working.

    What im doing now is that ive got a few buttons on both Menus which link to the same page, Such as:

    The owner and staff can place Order, Add Product, Add customer etc.

    So im going to link the menu to the same page for both logins.
    ----------------------------------------------------------------------
    The part im stuck on atm is

    When i click on the product page and then close it. It always goes back to the owners main menu even though im logged on with the staff logon.

    Is there a way i can put coding on the close button on each of the forms such as Order, Product and Customer to say:

    When btnClose.onclick
    If frmStaffMainMenu is open then
    Docmd.close "frmProduct"
    Docmd.Open acForm, "frmStaffMainMenu"

    Else

    Docmd.close "frmProduct"
    Docmd.Open acForm, "frmMainMenu"

    End if

    Is there anyway i can do something like this so the system knows if the staff is logged on and the close button is pressed then it should take them to the staff main menu and if the onwer is logged on as the close button is pressed then it should open the owner log on.


    Really appreciate the help.

    Thanks.

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Do some research on IsLoaded.
    I think that is the property you need.

    Here is some code showing usage
    Function frmProperties()
    Dim obj As AccessObject, dbs As Object
    Set dbs = Application.CurrentProject
    For Each obj In dbs.AllForms
    If obj.name = "frmEvent" Or obj.IsLoaded = True Then
    Debug.Print obj.name, obj.DateCreated, obj.DateModified
    End If
    Next obj
    End Function

  3. #3
    ismith is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2011
    Posts
    39
    thank you for that. It worked..

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

Similar Threads

  1. Replies: 11
    Last Post: 12-28-2011, 04:27 AM
  2. Form Load Check
    By Kapelluschsa in forum Forms
    Replies: 3
    Last Post: 07-06-2011, 07:21 AM
  3. VBA to check if a database is open
    By FSCHAMP in forum Programming
    Replies: 1
    Last Post: 04-28-2011, 08:20 AM
  4. To check or Un-Check all Boxes in a form
    By devcon in forum Forms
    Replies: 7
    Last Post: 05-01-2010, 12:03 AM
  5. Replies: 2
    Last Post: 02-26-2010, 08:14 AM

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