How to set an Access Tab Control's page name by VBA - ms-access

I have a form in MS Access with a Tab Control (called TabCtrl). This control has several pages, each with a subform.
On Form_Open, I want to query the subforms for the total number of records and put that number in the tab's name. For example, the tab named MyTab should become MyTab (2):
Private Sub SetTabName_MyTab()
Dim i As Integer
i = CurrentDb.OpenRecordset("Select count(*) from [MyQry];").Fields(0).Value
TabCtrl.Pages("MyTab").Name = "MyTab (" & i & ")"
End Sub
However, when I run this, the last line returns Run-time error 2136 "To set this property, open the form or report in Design view". Does this mean I can't do this in code? Should I use another Event?

You are trying to change the internal name of the control, rather than caption on the tab page control.
Try:
ATabcontrol.Pages("MyTab").Caption = "MyTab(" & i & ")"
Note: I have done this before and found issues with the text not changing till the tabcontrol was manipulated.

Try:
Dim s as string
'To Get current Tab Caption
s = Me.TabCtl.Pages(Me.TabCtl.Value).Caption
'To Set current Tab Caption
s = "TabName"
Me.TabCtl.Pages(Me.TabCtl.Value).Caption = s

Related

Access - Open different report from one button

How I can open diferents reports from one button in access if that form that shows me the information has been filtred. For example, in one form I have 2 elements, "t_element"=3 means is ID and name of report is "DNI_CAB", when "t_element"=54 means is Table and name of report is "Table_CAB". And if I filtred another form maybe shows me 8 elements. Thanks!
If I'm understanding this properly, you need to make this table-driven. By that, I mean you need to set up a table with 2 fields; ID and Report Call it tblReports.
Then you would do something like this on the OnClick event of the button:
Dim db as Database
Dim rec as Recordset
Dim MyReport as String
Set db = CurrentDB
'Find the Report you're going to use by filtering by the ID you want
Set rec = db.OpenRecordset("Select Report from tblReports where ID = " & me.txtID & "")
'Fill a variable with the resulting value
MyReport = rec(0)
'Now that we have the report name, open it
DoCmd.OpenReport MyReport
This is all entirely aircode, written off the top of my head, so it probably needs to be altered a little. But it should give you an idea of how to go about writing the code to get this done.

How to display record inside form Access 2016 - VBA

Hope you're enjoying your Friday afternoon!
I've got a form on MS Access which searches a linked table for a barcode (which is entered by the user)
If the barcode exists in the table I'd like to show the user the record which contains the matched barcode, if not the code moves on elsewhere.
Here is what I have so far:
DoCmd.DeleteObject acQuery, "Query2"
Set qdef = CurrentDb.CreateQueryDef("Query2", s)
Me.sbfMatchedRec.SourceObject = "Query2"
I get an error when trying to change the SourceObject to Query2.
If I open Query2 it has the exact information I need in it. I can also go via the form properties and change the SourceObject to Query2 which then displays what I need, but I just need to know how to change this in VBA as above.
Thanks!
You could requery the subform with the barcode passed in from the form's textbox as the WHERE criteria in the subform's .RecordSource property.
For example, say this table (tblProducts) has your list of barcoded records:
You could then add a subform to your form that will show records from tblProducts. Also add a textbox to the form for the user to supply a barcode and a command button for the user to initiate a search:
Before hooking-up the search button with the VBA, take a look at the .RecordSource property for the subform...
...switch the query builder to SQL View:
Copy the sql as you can use this as the basis to filter the records in the subform using VBA.
Go back to the form in design view and go in to the search button's On Click event:
The SQL you've copied can then be used to create an SQL string that takes the value entered in the form's barcode textbox (I've called the text box on the form txtBarcode) as part of the WHERE clause. This sql string can then be applied to the subform's .RecordSource property and then the subform can be requeried to show the result set of the new SQL definition in the subform:
Private Sub cmdSearch_Click()
Dim strSql As String
strSql = "SELECT tblProducts.Barcode, tblProducts.ProductName" _
& " FROM tblProducts"
If _
Me.txtBarcode > "" _
Then
strSql = strSql & " WHERE tblProducts.Barcode=" & Me.txtBarcode
End If
Me.tblProducts_sub.Form.RecordSource = strSql
Me.tblProducts_sub.Form.Requery
End Sub
So if we put 1001 in the text box on the form, and click the search button, the subfom only shows that matching record:

Navigating to a different tab in navigation subform

In an ms access 2010 database, I have a listbox whose afterupdate procedure needs (among other things) to navigate to a specific tab in a navigation subform. I can get it to change the SourceObject property of the navigation subform, but the selected tab does not get changed, so the user ends up seeing the right source object with the wrong tab selected. This looks unprofessional. How can I change both the selected tab and the source object?
I uploaded a simplified database that recreates the problem to this file sharing site.
The list box that needs its afterupdate method changed is called lstbxClients. Here is my current draft of its afterupdate method, which is currently throwing an error:
Private Sub lstbxClients_AfterUpdate()
Dim rst
Set rst = Me.RecordsetClone
rst.FindFirst "ClientNumber = " & lstbxClients.Column(0)
Me.Bookmark = rst.Bookmark
'Forms!Main!NavigationSubform.Form!NavigationSubform.SourceObject = "qryListCommunicationForms"
DoCmd.BrowseTo acBrowseToForm, "qryListCommunicationForms", "Forms!Main!NavigationSubform.Form!NavigationSubform"
Form.NavigationSubform " "
'Forms!Main!NavigationSubform.Form!NavigationSubform.SelectedTab = "CommFormsNavBtn"
Set rst = Nothing
End Sub
How do I change the code above so that it changes both the selected tab AND the source object of the navigation subform when the user clicks on a different record in the listbox?
Access gave a relatively decent explanation of what the proper syntax of the path is.
So your BrowseTo command should look like this:
DoCmd.BrowseTo acBrowseToForm, "qryListCommunicationForms", "Main.NavigationSubform>FindClientsNavigation.NavigationSubform"
Use the syntax below
DoCmd.BrowseTo acBrowseToForm, "qryListCommunicationForms", "Main.NavigationSubform>findClientsNavigation.NavigationSubform", "ClientNumber = " & lstbxClients.Column(0)
and
DoCmd.BrowseTo acBrowseToForm, "ListAddresses", "Main.NavigationSubform>findClientsNavigation.NavigationSubform", "ClientNumber = " & lstbxClients.Column(0)
The only change is the second parameter syntax , use
"Main.NavigationSubform>findClientsNavigation.NavigationSubform"
Hope this helps
The easiest way to do it :
Form_NavFormName.NavigationSubform.SourceObject = "FormName"

MS access add item to combo box if non in list

I have an access form, i want to know how to add item in combo box if there is not in there.
my combo box is in value mode.
Unfortunately you cannot change the rowsource permanently without changing the form to design mode and adding the new value. It is possible to do this with code, but it is not a good idea when people are working. The easiest thing is to create a small table and add the values to that. New values will then be saved when you close the form.
Allen Browne has a description of how to do this : http://allenbrowne.com/ser-27.html
This is one of the ideas he shows:
Private Sub CategoryID_NotInList(NewData As String, Response As Integer)
Dim strTmp As String
'Get confirmation that this is not just a spelling error.
strTmp = "Add '" & NewData & "' as a new product category?"
If MsgBox(strTmp, vbYesNo + vbDefaultButton2 + vbQuestion, "Not in list") = vbYes Then
'Append the NewData as a record in the Categories table.
strTmp = "INSERT INTO Categories ( CategoryName ) " & _
"SELECT """ & NewData & """ AS CategoryName;"
DBEngine(0)(0).Execute strTmp, dbFailOnError
'Notify Access about the new record, so it requeries the combo.
Response = acDataErrAdded
End If
End Sub
You'll need to set the property limit to list to true.
Then add some code in the On Not In List event that potentially adds the value to the combo box. Here is a tutorial. Or you can view the other answer.
Please note that it is usually better to utilize a table that stores the values for your combo box. With a value list, unless you disable the shortcut menus a user can right click on the combo box select Edit List Items... and modify the list even if it is set to limit to list... which effectively defeats any limitation you are trying to place on the field.

Go To Record + Specific Tab

I have created a search criteria form that allows the user to search for specific branches that our company deals with. Now - I have created a button that displays with each entry found by the search.
I want this button to open to the specific 'ID' that goes with each record, but here is my problem: this search criteria form (Tab1) updates the records information (Tab2,Tab3,Tab4) But doesnt jump to the tab after being pressed - Simply because I'm not sure how to code it so that it automatically jumps to Tab2 (the first tab of data).
Here is my code below:
Module1
Public Function viewDetails(frmID As Integer)
Dim rs As Object
DoCmd.OpenForm "Form1"
'Directs to the selected record
Set rs = Forms!Form1.RecordsetClone
rs.FindFirst "ID = " & frmID
Forms!Form1.Bookmark = rs.Bookmark
Set rs = Nothing
and
Private Sub Command46_Click()
viewDetails (Me.ID)
End Sub
Thanks :)
The .Value property of the TabControl is a read/write property that you can use to get the currently selected page or set the currently displayed page. It is the default property of the tab control so you do not need to explicitly refer to it. Note that the page index is a zero-based array so the second tab is 1 in the index:
Forms!Form1.TabCtl0 = 1 'Jump to the second tab; form must already be open