We help IT Professionals succeed at work.

Make a command button invisible on a form based on one of its text box

Last Modified: 2013-11-28
Hi experts-

i have a form frmDataForm that has its record source property set to a view linked to SQL server(ODBC).  The form has about 6 fields(textboxes).  I have a command button on this form that opens up another form frmComments capturing some of Dataform values.  What i want is to make this button invisible under certain circumstances i.e.  I check forAssetnum of the Dataform value (which is unique in each case) to assetnum in a table called UserTable. If the Assetnum from the Dataform exists in the UserTable then i want my command button to be invisible, if it doesn't exist it should become visible and if a user clicks on it it opens up the form frmcomments.  I'm not sure how to go about doing this..Thanks in advance
Watch Question

Aaron FeledyDrupal Developer and Consultant

Not really following what you want exactly, I'm assuming that if the textbox contains any text, display the button.

In the button's properties, set the visible property to false. Then, whenever you want to check your textbox, run this code:

If len(Textbox1.Text) <> 0 Then
     Button1.visible = true
     Button1.visible = false
End If

If you are looking for specific text in the box, change the if to this:

If Textbox1.Text = "Desired Text" Then
Mike EghtebasDatabase and Application Developer

In the OnCurrent event of your form have:

If Dcount("*","UserTable", "assetnum ='" & Cstr(txtAssetnum) & "'")>0 Then
    cmdButton.Visible = False
    cmdButton.Visible = True
End If


Can you give the code of your form? and the View?

Here is my quick thought, you will need to create a variable to store the AssetNum in the OnLoad section of the Form like this;
Dim strAssetNum
strAssetNum = me.assetnumfield.value

You also need a variable to store whether or not the recordset returns any data,
 dim strData

Then create a recordset that select the data from the UserTable with a WHERE CLAUSE that includes
the strAssetNum,

If the recordset returns no data then set your strData to 0, and if it does return data then set it to 1

Then write the code

If strData = 0 Then
me.commandbutton.visible = FALSE
else me.commandbutton.visible = true
end if

I could write the exact code, but I do not know any of your code behind the form, but Ihope that gives you the jist of it.


below is my code for the DataForm

Private Sub Form_Load()

Dim db As Database
Dim record As DAO.Recordset
Dim i As Integer
Dim j As Integer

 i = 0
 j = 0
Set db = CurrentDb()
Set record = db.OpenRecordset("SELECT assetnumFROM usertable")
 With record
        Do Until .EOF
            If Me.AssetNum.Value = .Fields("assetnum") Then
                i = i + 1
            Else: j = j + 1
End If

End With

If i = 1 Then
    Me.cmdbutton.Visible = False
    Me.cmdbutton.Visible = True
End If
End Sub

and the DatForm has just a select * from myview in its record source property..which just basically populates the form..i'm not sure how to tweak the above code to make it work...thanks
This one is on us!
(Get your first solution completely free - no credit card required)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.