Hide Command Button if Field Is Null

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alnino
    New Member
    • Dec 2009
    • 12

    Hide Command Button if Field Is Null

    Hi,

    I have a command button on a form that the user can use to browse to a file and the user can select that file and a hyperlink to that file is stored in a txtfield for that record.

    I then have a command button that a user can click to open and view the associated file.

    This all works fine. BUT:

    I would like the command button that allows the user to view the file to be hidden if no hyperlink was added. In other words the txtfield for the hyperlink IsNull.

    Here is what I have tried with no luck:

    Code:
    Private Sub txtSelectedFile_AfterUpdate()
    If (Me.[txtSelectedFile]) = IsNull Then
    cmdExplore.Visible = False
    Else
    cmdExplore.Visible = True
    End If
    
    End Sub
    Thank you!!!
  • MMcCarthy
    Recognized Expert MVP
    • Aug 2006
    • 14387

    #2
    Try this ...

    Code:
      
    Private Sub txtSelectedFile_AfterUpdate() 
        If NZ(Me.[txtSelectedFile], "") = "" Then 
            cmdExplore.Visible = False 
        Else 
            cmdExplore.Visible = True 
        End If 
      
    End Sub

    Comment

    • alnino
      New Member
      • Dec 2009
      • 12

      #3
      Thank you for the reply... But no luck. Command Button still visible when field is blank.

      Comment

      • MMcCarthy
        Recognized Expert MVP
        • Aug 2006
        • 14387

        #4
        Add the Me! reference just in case there is a problem with the button name.

        Code:
          
        Private Sub txtSelectedFile_AfterUpdate() 
            If NZ(Me!txtSelectedFile, "") = "" Then 
                Me!cmdExplore.Visible = False 
            Else 
                Me!cmdExplore.Visible = True 
            End If 
          
        End Sub
        You should always add it anyway and if you run the debug option it will tell you if there is a problem with the reference. Otherwise check the textbox txtSelectedFile and see if it is holding any blank spaces.

        Comment

        • nico5038
          Recognized Expert Specialist
          • Nov 2006
          • 3080

          #5
          I created a hyperlink table field named "x" and used in the InCurrent event:
          Code:
          Private Sub Form_Current()
          Me.x.Visible = Not IsNull(Me.x)
          Me.Repaint
          End Sub
          And my x field was invisible when Null...

          Nic;o)

          Comment

          • alnino
            New Member
            • Dec 2009
            • 12

            #6
            Thank you for all of the responses.

            It now works with a combination of code, using the Form_Current():
            Thank you!!!
            Code:
            Private Sub Form_Current()
            
                If Nz(Me!txtSelectedFile, "") = "" Then
                    Me!cmdExplore.Visible = False
                Else
                    Me!cmdExplore.Visible = True
                End If
              
            End Sub

            Comment

            • alnino
              New Member
              • Dec 2009
              • 12

              #7
              Thank you all for your help....

              One last question:

              The screen only refreshes if I advance a record and then come back. Is there a way to "refresh" or "repaint" when this txt field is either null or contains data?

              I am not sure what event to use or which field to call for such an event.

              Thank you

              Comment

              • MMcCarthy
                Recognized Expert MVP
                • Aug 2006
                • 14387

                #8
                In the after update event of the txtSelectedFile textbox try ...

                Code:
                Form_Current
                This will re-run any and all code in the forms current event.

                Comment

                • alnino
                  New Member
                  • Dec 2009
                  • 12

                  #9
                  I have tried this but no luck

                  Code:
                  Private Sub txtSelectedFile_AfterUpdate()
                  Form_Current
                  End Sub

                  Comment

                  • MMcCarthy
                    Recognized Expert MVP
                    • Aug 2006
                    • 14387

                    #10
                    Me.Refresh will clear out any unbound text boxes. If that is what you are trying to do. If you explain what it is you want to happen I might be able to help better.

                    Comment

                    • alnino
                      New Member
                      • Dec 2009
                      • 12

                      #11
                      msquared,

                      I have command button on a form that allows a user to browse to a file (doc, pdf, etc..)
                      The user chooses that file and a hyperlink for that file is stored in a txt field for the table

                      Then on the form there is a command button that allows the user to click and open the hyperlinked file.

                      Currently it is set up so if the hyperlink field is empty, the command button to view a file is not visible

                      This all works fine.

                      The ONLY issue is if:
                      The user clicks and adds the hyperlink; the “view” button is not visible until the user advances one record on the form and then goes back to the record in question.

                      Comment

                      • MMcCarthy
                        Recognized Expert MVP
                        • Aug 2006
                        • 14387

                        #12
                        Put the Form_Current command at the end of the code that adds the hyperlink to the field. It doesn't work in the AfterUpdate event because you never tab into that field so the event never triggers.

                        Comment

                        • alnino
                          New Member
                          • Dec 2009
                          • 12

                          #13
                          msquared,

                          I have put the Form_Current at the ned of the code to add the hyperlink... since no luck making the "View File" command button visible unless advance record and go back...umm...

                          Code:
                          Private Sub CmdBuildingAdd_Click()
                          
                          Me!txtSelectedFile = BrowseFiles()
                             variable = Me!txtSelectedFile
                           
                          Form_Current 
                          
                          End Sub

                          Comment

                          • TheSmileyCoder
                            Recognized Expert Moderator Top Contributor
                            • Dec 2009
                            • 2322

                            #14
                            While what you have posted will certainly work, it might clutter up things latter if you wish to add other code to your Form_Current that you might not like running after uses adds his hyperlink.

                            Instead add a procedure to your form:
                            Code:
                            Private Sub setLinkVis()
                                If Nz(Me!txtSelectedFile, "") = "" Then 
                                    Me!cmdExplore.Visible = False 
                                Else 
                                    Me!cmdExplore.Visible = True 
                                End If 
                            End Sub
                            Then change your code to:
                            Code:
                            Private Sub CmdBuildingAdd_Click() 
                              
                            Me!txtSelectedFile = BrowseFiles() 
                               variable = Me!txtSelectedFile 
                               call setLinkVis
                              
                            End Sub
                            And in your Form_Current:
                            Code:
                            Private Sub Form_Current() 
                               call setLinkVis
                            End Sub

                            Comment

                            • alnino
                              New Member
                              • Dec 2009
                              • 12

                              #15
                              TheSmileyOne,

                              That did it. THANKS!!!

                              Comment

                              Working...