• Constructing a Variable Name

    Author
    Topic
    #463257

    I have four variables: glbUser1Avail, glbUser2Avail, glbUser3Avail and glbUser4Avail. I would like to set glbUserXAvail = True depending on the value of the openargs passsed to the form.

    IOW:
    If openargs = “1”, then
    glbUser1Avail = True
    End if

    If openargs = “2” then
    glbUser2Avail = True
    End if

    etc.

    How do I construct the variable name so I don’t have to do this in four If statements?

    Hope I’ve made my question clear. Thanks, in advance.

    Viewing 2 reply threads
    Author
    Replies
    • #1181939

      As you have framed the problem, you would need a single If with three more ElseIf cases. You could however do it as a Select Case statement something like:

      Code:
      Select Case openargs
         Case 1
      	  glbUser1Avail=True
         Case 2
      	  glbUser2Avail=True
         Case 3
      	  glbUser3Avail=True
         Case 4
      	  glbUser4Avail=Ture
         Case Else
      	  ' What ever you want to do went openargs is something else
      End Select

      If you can give us some insight into what you are trying to accomplish, we may be able to suggest other alternatives, such as using a table, turning on Access User Security, etc.

      • #1181943

        As you have framed the problem, you would need a single If with three more ElseIf cases. You could however do it as a Select Case statement something like:

        Code:
        Select Case openargs
           Case 1
        	  glbUser1Avail=True
           Case 2
        	  glbUser2Avail=True
           Case 3
        	  glbUser3Avail=True
           Case 4
        	  glbUser4Avail=Ture
           Case Else
        	  ' What ever you want to do went openargs is something else
        End Select

        If you can give us some insight into what you are trying to accomplish, we may be able to suggest other alternatives, such as using a table, turning on Access User Security, etc.

        I was looking to construct a variable into which the value of openargs would be inserted dynamically and then use a single statement — something like this:
        “glbUser” &openargs & “Avail” = True

        How do I construct the variable on the left side of the = sign?

        Thanks.

        • #1181945

          “glbUser” &openargs & “Avail” = True

          VBA doesn’t let you do that.
          If you don’t want to use Wendell’s code, you could use an array instead of separate variables. The declaration would look like this:

          Public glbUserAvail(1 To 4) As Boolean

          And in the On Open or On Load event of the form:

          Code:
          If Not IsNull(Me.OpenArgs) Then
            glbUserAvail(Me.OpenArgs) = True
          End If

          or if you want to make sure that the others are False:

          Code:
          Dim i As Integer
          For i = 1 To 4
            glbUserAvail(i) = False
          Next i
          If Not IsNull(Me.OpenArgs) Then
            glbUserAvail(Me.OpenArgs) = True
          End If

          Instead of glbUser1Avail, you’d refer to glbUserAvail(1) etc.

        • #1181955

          I was looking to construct a variable into which the value of openargs would be inserted dynamically and then use a single statement — something like this:
          “glbUser” &openargs & “Avail” = True

          What I should have asked is why you want to track a particular user in this fashion. Access has a reasonably sophisticated method for tracking the Current User if you activate User Security, and it is possible that would solve the issue you are working on. Your code idea seems to imply that two or more users can be active at once – but what I’m curious about is why you want to know that? Are you trying to build your own security scheme, or is it something else?

    • #1181973

      Wendell,

      Actually, I’m not tracking anything about a human “user”.

      The application is a restaurant operation system that requires modifying to interface with a new credit card processing company and software. We wrote the application 7-8 years ago and interfaced to package A. Now the client needs it to interface to package B.

      We’re using a “file drop” methodology and package B only processes transactions in a single threaded fashion. So, we essentially need to build our own queuing mechanism. The four “users” are not people; they are logins under which transactions are submitted to package B. Since there are two workstations involved, we need to know which “users” are available for processing whenever a transaction needs to be processed.

      That’s it in a nutshell!

      Thanks for your interest and your replies.

    • #1181991

      You can’t do it with global variables. It could be done if instead of global variables you used checkboxes on the form (even making them hidden). Then you could have code like this: Me(“chkUser” & me.openargs & “Avail) = True.

      • #1182002

        You can’t do it with global variables. It could be done if instead of global variables you used checkboxes on the form (even making them hidden). Then you could have code like this: Me(“chkUser” & me.openargs & “Avail) = True.

        Thanks, Mark.

        You answered my real question which was whether or not a global variable name could be constructed “on the fly” via VBA code.

        The If / Else (and the Case) construction works well so I think I’ll just leave it as it is.

        Thanks again.

    Viewing 2 reply threads
    Reply To: Constructing a Variable Name

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: