Update a control when a new page is opened in a form

G

Guest

I want to make a command button visible so the user can perform an action
(say, print a letter) when a certain text box is filled in. I was finally
successful in doing this. However, when I open a new page in the form, I
can't figure out how to make the command button invisible again -- the
command button remains visible even though the text box is empty. How do I
get the page (whether I am clicking forward or backward) to make the command
button visible (if the text box is filled in) or make the command button
invisible (if the text box is empty)?

I am very new to Access. I am learning as I go along and sometimes the
simplest thing takes a while to figure out but this one has me stumped.

Can anyone help? Thank you so much
 
A

Allen Browne

Use the AfterUpdate event procedure of the text box to show or hide the
command button. Call the same code from the Current event of the form, so it
runs each time you change record.

Use error handling in case it tries to hide the command button while it has
focus.

This is untested aircode, but illustrates the kind of thing:

Private Sub Text1_AfterUpdate
On Error Goto Err_Handler
Dim iErrorCount As Integer
Dim bShow As Boolean

bShow = Not IsNull(me.Text1)
With Me.Command1
If .Visible <> bShow Then
.Visible = bShow
End If
End With

Exit_Handler:
Exit Sub

Err_Handler:
Select Case Err.Number
Case 2164&, 2165& 'Can't disable/hide the control with focus.
iErrorCount = iErrorCount + 1
If iErrorCount < 3 Then
Me[SomeOtherControl].SetFocus
Resume
Else
Resume Exit_Handler
End If
Case Else
MsgBox "Error " & Err.Number &": " & Err.Description
Resume Exit_Handler
End Select
End Sub

Private Sub Form_Current()
Call Text1_AfterUpdate
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top