Need to fix VBA code for Previous Page command button (1 Viewer)

Anwar1968

Registered User.
Local time
Yesterday, 22:59
Joined
Apr 13, 2014
Messages
31
Hello everyone,
I have a subform which includes a Tab Control with 8 pages. I want to hide the pages tabs to force users navigating from a page to another and giving required data/info. So I created 2 Command Buttons (Next Page) and (Previous Page).

The Next Page code is:

Private Sub NextPage_Click()
TabMainPages.Value = (TabMainPages.Value + 1) Mod TabMainPages.Pages.Count
End Sub


Which works perfectly moving from a page to another and starts over from the first page.

While I put the code below for Previous Page command button:

Private Sub PreviousPage_Click()
TabMainPages.Value = (TabMainPages.Value - 1) Mod TabMainPages.Pages.Count
End Sub


It works moving back to previous pages, but once reaches the first page (value 0), then I get a Debug error that there is an invalid item in the code. So, how can I slightly change the code of Previous Page command button to fix this error.

Thank you so much for your help and support.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 11:59
Joined
May 7, 2009
Messages
19,243
suggest add a module wide variabke and increment or decrement its value depending which button is pressed:

dim mintPage as integer
private form_load()
mintPage=1
end sub

private sub nextpage_click()
mintpage=mintpage+1
if mintpage> tabmainpages.pages.count then mintpage=1
tabmainpages=mintpage
end sub

private sub previouspage_click()
mintpage=mintpage-1
if mintpage<1 then mintpage=tabmainpages.pages.count
tabmainoages=mintpage
end sub
 

Anwar1968

Registered User.
Local time
Yesterday, 22:59
Joined
Apr 13, 2014
Messages
31
Thank you Arnelgp for your reply.
The Next Page code you gave me works to move only to the second page and then completely stops. In other words, I guess your code works like:

tabmainpages.value=1

Again, my issue is not with the (Next Page) button but to slightly change and fix the (Previous Page) button's code which I mentioned in my original post above to stop getting the debug error.
 

missinglinq

AWF VIP
Local time
Yesterday, 23:59
Joined
Jun 20, 2003
Messages
6,423
Code:
Private Sub PreviousPage_Click()

 If Me.CurrentRecord <> 1 Then

  TabMainPages.Value = (TabMainPages.Value - 1) Mod       
  TabMainPages.Pages.Count

 End If

End Sub
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 11:59
Joined
May 7, 2009
Messages
19,243
here is another test:


Dim mintPage As Integer

Private Sub NextPage_Click()
mintPage = mintPage + 1
If mintPage > Me.TabMainPages.Pages.Count - 1 Then mintPage = 0
Me.TabMainPages = mintPage

End Sub

Private Sub PreviousPage_Click()
mintPage = mintPage - 1
If mintPage < 0 Then mintPage = Me.TabMainPages.Pages.Count - 1
Me.TabMainPages = mintPage

End Sub

Private Sub Form_Load()
mintPage = 0
End Sub
 

Anwar1968

Registered User.
Local time
Yesterday, 22:59
Joined
Apr 13, 2014
Messages
31
Perfect! Thank you so much Missinglinq. Your code solved the issue and fixed the error. However, I made a light change in the first line of your code, I replaced 1 with 0 because I cannot move back to the first page in this Tab control. My final code for both command buttons are:

Next Page button's code:

Private Sub NextPage_Click()
If Me.TabMainPages <> 7 Then
TabMainPages.Value = (TabMainPages.Value + 1) Mod TabMainPages.Pages.Count
End If
End Sub


Previous Page button's code:

Private Sub PreviousPage_Click()
If Me.TabMainPages <> 0 Then
TabMainPages.Value = (TabMainPages.Value - 1) Mod TabMainPages.Pages.Count
End If
End Sub


Again, I highly appreciate your help, Missinglinq
Anwar
 

Anwar1968

Registered User.
Local time
Yesterday, 22:59
Joined
Apr 13, 2014
Messages
31
Hi Arnelgp,
Thank you for your new testing code. I tried it but did not work. Nothing happens when I press the command button. I am not sure if I am missing something. By the way, I got a Debug error message in the beginning, and after checking I realized that the code missing (End If) which fixed the error.

I will use Missinglinq's code because it works exactly as I want, and it is less complicated and easier for me to understand and apply.

Thank you so much Arnelgp for your time, efforts, and help. Have a good day.
Anwar
 

Anwar1968

Registered User.
Local time
Yesterday, 22:59
Joined
Apr 13, 2014
Messages
31
Hi Missinglinq,

I just want to share with you that I deleted the code part
Mod TabMainPages.Pages.Count
from the whole codes, and both Next and Previous buttons still work correctly now. So I wonder what is the purpose of the code line Mod TabMainPages.Pages.Count

My final successfully working codes for both buttons are:

Next Page button's code:

Private Sub NextPage_Click()
If Me.TabMainPages <> 7 Then
TabMainPages.Value = (TabMainPages.Value + 1)

End If
End Sub


Previous Page button's code:

Private Sub PreviousPage_Click()
If Me.TabMainPages <> 0 Then
TabMainPages.Value = (TabMainPages.Value - 1)
End If
End Sub


Any future negative results because I deleted Mod TabMainPages.Pages.Count from the codes?

Thank you
 

Users who are viewing this thread

Top Bottom