Results 1 to 6 of 6
  1. #1
    silverspr is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2011
    Posts
    19

    Switchboard open form based on function value

    I can't seem to get the syntax right to call a function in the macro used to open an existing form:



    If [Command]=2 And (FindUserId())=True then

    openform....etc

    finduserid is a function that evaluates to true if the userid is stored in the urser id table.

  2. #2
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    I assume that you have to pass the current user's ID to the function so that it can evaluate it. You'll have to post the code for us to be sure.

    If [Command]=2 And (FindUserId(userID))=True then...

    So the issue then becomes, where can you get the current user's ID? From where are you executing the macro? Are you having the user's login to the database via a form? Do you leave the form open so that you can reference the control on the form that holds the user's ID?

  3. #3
    silverspr is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2011
    Posts
    19
    This IF statement runs on the click event of an option button on the switchboard form, no parameters are specified for the funtion:

    If [Command]=2 And (FindUserId())=True then.....
    the problem is the above line of code is not evaluating, which makes me think there is something wrong with the syntax used.

    The userid function evaluates to true if the userid is found in the userid table.

    Function FindUserId()as Boolean
    Dim db As Database
    Dim rs As Recordset
    Dim lngCount As Integer
    Dim strCriteria As String
    Set db = CurrentDb
    Set rs = CurrentDb.OpenRecordset("tblUserId", dbOpenDynaset)

    x = GetNTUser
    strCriteria = "[userid]= '" & x & "'"
    rs.FindFirst strCriteria
    If rs.NoMatch Then
    FindUserId = False
    Else
    FindUserId = True
    End If
    End Function

  4. #4
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    I see, you are using a global variable instead of having to pass the ID to the function directly.

    What I would do would be to put a debug.print statement in where shown in blue to make sure the userID is being populated correctly.

    Code:
    Function FindUserId()as Boolean
    Dim db As Database
    Dim rs As Recordset
    Dim lngCount As Integer
    Dim strCriteria As String
    Set db = CurrentDb
    Set rs = CurrentDb.OpenRecordset("tblUserId", dbOpenDynaset)
    
    x = GetNTUser
    strCriteria = "[userid]= '" & x & "'"
    debug.print strCriteria
    rs.FindFirst strCriteria
    If rs.NoMatch Then
    FindUserId = False
    Else
    FindUserId = True
    End If
    End Function
    You would attempt to run the function and then check the immediate window in VBA to see what is returned by the debug.print statement

  5. #5
    silverspr is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2011
    Posts
    19
    Hi
    the function works fine when called from the debug window, its only here in the "If [Command]=2 And (FindUserId())=True then....." that it doesn't evaluate correctly.

    Am I calling the function correctly from within this IF statement? This IF statement is part of the embedded macro triggered on the onclick event of the option button of the switchboard form.

    I want the function to evaluate the userid only when the [Command]=2 option button is clicked.

    Not sure how else to explain this better.

  6. #6
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    The IF...THEN is structured properly, and if the function appears to be working properly then perhaps something is wrong with the [command]=2.

    First I would try me.[command]=2. Also, I would guess that the word command is a reserved word in Access & VBA, so you might change the name of the option button to something else. Other than that I cannot guess what else might be the cause.

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

Similar Threads

  1. Replies: 4
    Last Post: 04-20-2013, 10:12 AM
  2. Calling A Module Function To Open A Form
    By orcinus in forum Modules
    Replies: 3
    Last Post: 09-29-2010, 04:43 PM
  3. Open form based on query results
    By RobbertH in forum Queries
    Replies: 1
    Last Post: 02-10-2010, 08:13 AM
  4. Replies: 3
    Last Post: 01-14-2010, 08:32 AM
  5. Open form based on Subform criteria
    By Suzan in forum Programming
    Replies: 0
    Last Post: 04-25-2006, 02:28 PM

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