Use vba code to programmatically make the tab control refer to and change the current page number

wire_jp
StarLounger
Posts: 67
Joined: 19 Jan 2018, 00:00

Use vba code to programmatically make the tab control refer to and change the current page number

Post by wire_jp »

Hello,

I would like to use vba code to programmatically make the tab control refer to and change the current page number on a form. I used some vba code from a Microsoft Learn page example from the Section on " Refer to and change the current page "https://learn.microsoft.com/en-us/offic ... cts-in-vba to control a page label denoted as lblRecored_Position:

Code: Select all

Private Sub lblRecord_Position_Click()
TabControl1.Value
End Sub 
When I add a new record and save the form, the form defaults to the first record in the form. However, I want to show the form to last record which was added to the form, once the form is saved after adding a new record. Kindly advise to fix this issue. Thanks
Last edited by wire_jp on 10 Feb 2023, 19:54, edited 2 times in total.

User avatar
HansV
Administrator
Posts: 78494
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Use vba code to programmatically make the tab control refer to and change the current page number

Post by HansV »

I don't quite understand. The line

Code: Select all

TabControl1.Value
doesn't do anything - you neither change TabControl1.Value not do anything with the current value.
Best wishes,
Hans

wire_jp
StarLounger
Posts: 67
Joined: 19 Jan 2018, 00:00

Re: Use vba code to programmatically make the tab control refer to and change the current page number

Post by wire_jp »

Hi Hans,

Thank you for your response. My apologies about the title and the description. Basically, I want to add page numbers (i.e. page y out of pages z) to a form. In browsing around the web, I came across an Allen Browne's post which stated that it is not possible to chronologically number pages on a form: - http://allenbrowne.com/casu-10.html. So I used the approach which he mentioned to number entries on a form:-

Code: Select all

  =[Form].[CurrentRecord]
Last edited by wire_jp on 12 Feb 2023, 00:38, edited 1 time in total.

User avatar
HansV
Administrator
Posts: 78494
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Use vba code to programmatically make the tab control refer to and change the current page number

Post by HansV »

If you want to open the form at the last record, you can either change the record source of the form to be sorted in descending order on a relevant field (for example an AutoNumber field), or sort it in ascending order and use code in the On Load event of the form:

Code: Select all

Private Sub Form_Load()
    RunCommand acCmdRecordsGoToLast
End Sub
Best wishes,
Hans

wire_jp
StarLounger
Posts: 67
Joined: 19 Jan 2018, 00:00

Re: Use vba code to programmatically make the tab control refer to and change the current page number

Post by wire_jp »

Hi Hans,

Awesome! Thank you for the help!