testing for association

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • omozali
    New Member
    • Nov 2006
    • 36

    testing for association

    Hi,

    How can I test if a variable was associated with an object or not

    for example
    dim mytextbox as textbox
    if mytextbox = Nothing then <=== this produces error
    mytextbox = textbox0
    else
    set mytextbox = Nothing
    endif
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Originally posted by omozali
    Hi,

    How can I test if a variable was associated with an object or not

    for example
    dim mytextbox as textbox
    if mytextbox = Nothing then <=== this produces error
    mytextbox = textbox0
    else
    set mytextbox = Nothing
    endif
    You can use the IsEmpty() Function which returns a Boolean value indicating whether or not a Variable has been initialized:

    Dim MyTextBox As TextBox
    Set MyTextBox = Me![txtTest]
    Debug.Print

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      Originally posted by omozali
      Hi,

      How can I test if a variable was associated with an object or not

      for example
      dim mytextbox as textbox
      if mytextbox = Nothing then <=== this produces error
      mytextbox = textbox0
      else
      set mytextbox = Nothing
      endif
      You can use the IsEmpty() Function which returns a Boolean value indicating whether or not a Variable has been initialized:
      Code:
      Dim MyTextBox As TextBox
      Set MyTextBox = Me![txtTest]     'valid Text Box on frmTest
      Debug.Print IsEmpty(MyTextBox) ===> False
      
      Debug.Print IsEmpty(MyTextBox2) ==> True

      Comment

      • omozali
        New Member
        • Nov 2006
        • 36

        #4
        Originally posted by omozali
        Hi,

        How can I test if a variable was associated with an object or not

        for example
        dim mytextbox as textbox
        if mytextbox = Nothing then <=== this produces error
        mytextbox = textbox0
        else
        set mytextbox = Nothing
        endif
        I found out that typename(mytext box) before association returns Nothing
        and after association returns TextBox

        The weird thing is this still produces error

        if typename(mytext box) = Nothing then

        Comment

        • omozali
          New Member
          • Nov 2006
          • 36

          #5
          Originally posted by ADezii
          You can use the IsEmpty() Function which returns a Boolean value indicating whether or not a Variable has been initialized:
          Code:
          Dim MyTextBox As TextBox
          Set MyTextBox = Me![txtTest]     'valid Text Box on frmTest
          Debug.Print IsEmpty(MyTextBox) ===> False
          
          Debug.Print IsEmpty(MyTextBox2) ==> True

          But if you set mytextbox2 as textbox isnull would still returns true even if you don't associate it to a valid text box

          Comment

          • omozali
            New Member
            • Nov 2006
            • 36

            #6
            Originally posted by omozali
            I found out that typename(mytext box) before association returns Nothing
            and after association returns TextBox

            The weird thing is this still produces error

            if typename(mytext box) = Nothing then

            If found the answer

            if Typename(mytext box) = "Nothing" WORKS

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32633

              #7
              Originally posted by omozali
              Hi,

              How can I test if a variable was associated with an object or not

              for example
              dim mytextbox as textbox
              if mytextbox = Nothing then <=== this produces error
              mytextbox = textbox0
              else
              set mytextbox = Nothing
              endif
              You need to check 'Is Nothing' and you need to assign the object using 'Set'.
              To rewrite your code above :
              Code:
              Dim mytextbox As TextBox
              If mytextbox Is Nothing Then  <=== this shouldn't
                  Set mytextbox = textbox0
              Else
                  Set mytextbox = Nothing
              EndIf

              Comment

              Working...