Problem with enable property of tab control

M

magicdds-

I am having trouble changing the enable property of a tab control to NO.

I have a command button with the on click property running the following code:
Me.TabCtl73.Pages(0).Enabled = False

When I click on page 0, the text boxes are disabled. But this is not what I
am looking to accomplish.

What I want to do is click on the command button and prevent the user from
being able to navigate away from the current page.

I tried
Me.TabCtl73.Enabled = False
but got an error message "You can't disable a control while it has the focus"

I then tried:
Me!SubformName.SetFocus
Me.TabCtl73.Enabled = False

and I got the same error message.

Is there a way to, with certain conditions being met on the page that the
user is on, disabling the tabs at the top of the page, so the user cannot
move from that page, until he satisfies the conditions to allow him to move
to other pages.

Thanks
Mark
 
R

Rob Parker

Hi Mark,

There is no tab control property that you can set to prevent changing page
without disabling all controls on the tab control.

However, you can probably accomplish your ultimate goal with some code in
the tab control's Change event. This event does not have a Cancel
parameter, but you can code to test for the condition(s) you want to be met,
and if they are not, then simply change back to the previous page.
Something like this:

Private Sub TabCtl73_Change()
If IsNull(txtRequiredEntry) Then
MsgBox "Required field has no entry"
TabCtl73.Value = 0 'set this value to the index of the page containing
the txtRequiredEntry control
End If
End Sub

Change the name of the control you wish to check appropriately. You can
include other controls either in a multiple condition in the If statement,
or by repeating the If ... End If code for each control/condition you wish
to check; the second method will allow you to deliver a custom message for
each control/condition. Note that, if you include the MsgBox line, the
message will appear twice, since the Change event occurs once when you move
to a different page, and again when you return to the original page. You
can prevent this by using a module-level variable to track whether the
message has been shown, thus:

Dim blnMessageShown As Boolean 'this line must be at the top of the
module, immediately after any Option lines

Private Sub TabCtl73_Change()
If IsNull(txtRequiredEntry) Then
blnMessageShown = False
If Not blnMessageShown Then
MsgBox "Required field has no entry"
blnMessageShown = Not (blnMessageShown)
End If
TabCtl73.Value = 0
End If
End Sub

The message will appear once, and the tab control will not (appear to) move
to another tab page until the condition is met.

By default, blnMessageShown will be set to False when the form opens. If
you are changing record while the form is open, you will need to reset
blnMessageShown to False in the form's current event, so that this behaviour
will be triggered for each record.

Also, if you are using separate If ... End If section to test multiple
controls/conditions, you will need to define a separate boolean variable for
MessageShown in each section.

HTH,

Rob
 
M

magicdds-

With one minor modification, this worked great.

In order to not have the MESSAGE appear twice I had to use the following code:

Private Sub TabCtl73_Change()

If IsNull(txtRequiredEntry) Then
If Not blnMessageShown Then
MsgBox "Required field has no entry"
blnMessageShown = Not (blnMessageShown)
End If
TabCtl73.Value = 1
blnMessageShown = False
End If

End Sub

Thank you very much for your help!!!!
Mark
 

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