Tab control

G

Guest

Although I have tried to glean how-to from other postings, I am unsuccessful.
Can someone suggest a coding solution?

My form has several tabs. When the user clicks out of any tab, I wish to
test for validity before leaving and failing, not leave the tab until
corrected.

Example: User enters data tab0 and clicks tab3 to work there. However,
user left off some data. User should be returned to tab0 to correct with
focus set to the offending control.

This seems simple enough, but the most success I have had has ended in
looping code. Not good.

In hopes of help.
 
G

Guest

For those containing subforms, just use the BeforeUpdate event of the
subform. For the others I'd suggest using the Tab Control's OnChange event.

You will have to write your own validation routine and will also probably
need a form level variable to hold the selected TabPage to validate on a
change.

Steve
 
G

George Nicholson

The skeleton of an approach:

' (Form Module level or app level declaration)
Private mlngTabpage as Long
'-----------
Private Sub tbcTabControl_Change()
' Note: Me.tbcTabControl = the "new" page the user wants to move to
' mlngTabpage = the stored value of the "old" page we want to
leave

If Me.tbcTabControl = mlngTabpage Then
' No page change. Assume a forced (recursive) visit to the Change
event: Exit
Exit Sub
End If

Select Case mlngTabpage
' Check old/prior page (mlngTabpage) before allowing movement to new
page (Me.tbcTabControl)
Case 1
If somePageOneValidationTest = False Then
' Validation failed. Force user back to "previous" page &
Exit
' Note: this 'change' will cause the Change event to fire a
2nd time
Me.tbcTabControl = mlngTabpage
' (This is of course the same as: Me.tbcTabControl = 1)
Exit Sub
End If
Case 2
'........
Case Else
MsgBox "Unexpected value"
End Select

' If we got this far then Page change is being allowed. Store its value
mlngTabpage = Me.tbcTabControl

End Sub

HTH,
 
G

Guest

George, thanks very much. I am very much an amateur but I follow your code
-- but don't get it completely.

See my code below as used. My form opens with tab0 visible and clicking any
other tab returns "unexpected value". then I get "phone" and "pages same",
but tab0 (phone data) is not the focus.

Don't know about the "unexpected value" but I'm thinking the two messages
are because the revisit. How does this refocus to tab0?

Code:
Private Sub tabNewClient_Change()
Static mlngTabpage As Long
' Note: Me.tabNewClient = the "new" page the user wants to move to
' mlngTabpage = the stored value of the "old" page we want to leave

If Me.tabNewClient = mlngTabpage Then
' No page change. Assume a forced (recursive) visit to the Change event: Exit
MsgBox "pages same"
Exit Sub
End If

Select Case mlngTabpage
' Check old/prior page (mlngTabpage) before allowing movement to new Page
(Me.tabNewClient)

Case 1
If IsNull(Me.txtPhone1.Value) Or Me.txtPhone1 = 0 Then
MsgBox "phone"
' Validation failed. Force user back to "previous" page & Exit
' Note: this 'change' will cause the Change event to fire a 2nd time
Me.tabNewClient = mlngTabpage
' (This is of course the same as: Me.tabNewClient = 1)
Exit Sub
End If

Case 2
If IsNull(Me![Petsfrm].Form!txtName1.Value) Then
MsgBox "name"
' Validation failed. Force user back to "previous" page & Exit
' Note: this 'change' will cause the Change event to fire a 2nd time
Me.tabNewClient = mlngTabpage
' (This is of course the same as: Me.tabNewClient = 2)
Exit Sub
End If

Case Else
MsgBox "Unexpected value"
End Select

' If we got this far then Page change is being allowed. Store its value
mlngTabpage = Me.tabNewClient
End Sub

--
Thanks for your help,
Chris


George Nicholson said:
The skeleton of an approach:

' (Form Module level or app level declaration)
Private mlngTabpage as Long
'-----------
Private Sub tbcTabControl_Change()
' Note: Me.tbcTabControl = the "new" page the user wants to move to
' mlngTabpage = the stored value of the "old" page we want to
leave

If Me.tbcTabControl = mlngTabpage Then
' No page change. Assume a forced (recursive) visit to the Change
event: Exit
Exit Sub
End If

Select Case mlngTabpage
' Check old/prior page (mlngTabpage) before allowing movement to new
page (Me.tbcTabControl)
Case 1
If somePageOneValidationTest = False Then
' Validation failed. Force user back to "previous" page &
Exit
' Note: this 'change' will cause the Change event to fire a
2nd time
Me.tbcTabControl = mlngTabpage
' (This is of course the same as: Me.tbcTabControl = 1)
Exit Sub
End If
Case 2
'........
Case Else
MsgBox "Unexpected value"
End Select

' If we got this far then Page change is being allowed. Store its value
mlngTabpage = Me.tbcTabControl

End Sub

HTH,
 
G

Guest

I just reread my post....it needs a clarification.

As coded, Case1 does return to the tab user was attempting to leave.
However, the test is for another tab. For example: User is on tab0 and
attempts to leave when the information is incomplete on this tab. Given the
unexpected value when attempting to leave tab0 with missing data, the focus
goes to tab1; thereafter returns to tab1 with the message for tab0. In this
case "phone" and "same page."

Hope this makes sense.

--
Thanks for your help,
Chris


George Nicholson said:
The skeleton of an approach:

' (Form Module level or app level declaration)
Private mlngTabpage as Long
'-----------
Private Sub tbcTabControl_Change()
' Note: Me.tbcTabControl = the "new" page the user wants to move to
' mlngTabpage = the stored value of the "old" page we want to
leave

If Me.tbcTabControl = mlngTabpage Then
' No page change. Assume a forced (recursive) visit to the Change
event: Exit
Exit Sub
End If

Select Case mlngTabpage
' Check old/prior page (mlngTabpage) before allowing movement to new
page (Me.tbcTabControl)
Case 1
If somePageOneValidationTest = False Then
' Validation failed. Force user back to "previous" page &
Exit
' Note: this 'change' will cause the Change event to fire a
2nd time
Me.tbcTabControl = mlngTabpage
' (This is of course the same as: Me.tbcTabControl = 1)
Exit Sub
End If
Case 2
'........
Case Else
MsgBox "Unexpected value"
End Select

' If we got this far then Page change is being allowed. Store its value
mlngTabpage = Me.tbcTabControl

End Sub

HTH,
 
G

George Nicholson

Sorry. Somehow the code got garbled when i pasted it in.

The numbers following the Case statements should match the values of the
PageIndex property of the corresponding tab (the tab being moved *from* and
being tested). They should start with 0, not 1. Instead of Case 1, Case 2 it
should have been Case 0, Case 1.

Also: If you have additional tabs you should either give them their own Case
statements (can be: Case 2,3,4,5), or remove the "Unexpected value" message.

Sorry 'bout that.

HTH,


Chris said:
I just reread my post....it needs a clarification.

As coded, Case1 does return to the tab user was attempting to leave.
However, the test is for another tab. For example: User is on tab0 and
attempts to leave when the information is incomplete on this tab. Given
the
unexpected value when attempting to leave tab0 with missing data, the
focus
goes to tab1; thereafter returns to tab1 with the message for tab0. In
this
case "phone" and "same page."

Hope this makes sense.
 

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

Similar Threads


Top