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
Related
I'm using a sub-form to assign multiple tasks at the same time. This sub-form is placed on a form that has common details applicable to all the tasks there. For instance: Customer, Product Code, Part Code etc., Both the form and the sub-form feed the data to the same table. These forms are linked with one of the keys - Line Item, which is present on both the form and the sub-form. The other two keys are Task Title - placed on the sub-form, and Stage ID placed on the main form. Line Item is configured in a way that it populates the value from another open form for both the main form and the sub-form.
But Access isn't allowing me to add any details whatsoever to the sub-form. It gives me the error "You must enter a value for 'TableName.LineItem' field.
Kindly advise.
Two Approaches:
Select Table A and hit create form on the ribbon. Then do the usual prettification like deleting id columns, setting the forms format to continuous forms, and replacing textboxes with combo- boxes where appropriate.
First Approach is put unbound controls and a button in the header. Then insert the values from the controls in the button click event
Private Sub cmbAddRecord_Click()
Dim db As Database
Set db = CurrentDb
Dim rs As DAO.Recordset
Set rs = db.OpenRecordset("TableA")
rs.AddNew
rs!A = Me.txtA
rs!B = Me.txtB
rs!C = Me.txtC
rs.Update
Me.Requery 'show changes
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
End Sub
You can also insert values while actively entering the data for instance
Private Sub A_AfterUpdate() 'A is the TextBox in the detail section that is bound to A in TableA and so forth
Me.B = B.Parent!txtB
Me.C = C.Parent!txtC 'Tip use Me.MyControlName.SetFocus to select a control
Me.Refresh 'show changes
'tip you can also put this code in hotkeys https://learn.microsoft.com/en-us/office/vba/api/access.form.keydown
End Sub
'
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
I want:
To be able to open a form, select an item from a control box, click a button to open a new form and be able to input new records that have the previously selected item added to their fields.
What I have done so far:
I have made two forms, added the controls in both, added a command button.
I have used MS Access wizard to add this code:
Private Sub CommandNext_Click()
On Error GoTo Err_CommandNext_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "FormX"
DoCmd.OpenForm stDocName, , , "[BatchID] = " & Me![ListBatch], acFormAdd
Exit_CommandNext_Click:
Exit Sub
Err_CommandNext_Click:
MsgBox Err.Description
Resume Exit_CommandNext_Click
End Sub
The third line is mine.
As a result:
If the second form isn't already open, pressing the button on the first form will open the second one with empty data (the text field that should show the value of the parameter is empty).
If the second form is already open, pressing the button on the first form will change focus to the second one and it will show an old, existing record that matches the parameter I selected
I want to open the second form without any old records showing but with the selected parameter appearing in the designated text field.
In both cases in the second form the Filter property gets populated with the parameter I've sent using the button, but that isn't what I am aiming for.
Are you aware the 4th argument for the open form command is the where condition or filter to apply?
If you use the last parameter which is called open arguments (OpenArgs) then you can write code in the 2nd forms open event to set the default value for the column in question.
The open form code would look something like this:
DoCmd.OpenForm stDocName, , , , acFormAdd, , Me![ListBatch]
You could then put code similar to the following in this 2nd forms open event:
Dim defaultID as Long
defaultID = CLng(Nz(Me.OpenArgs, 0))
If defaultID = 0 Or IsNull(Me.OpenArgs) Then
Cancel = True
Exit Sub
End If
Me.TextBoxBatchID.DefaultValue = defaultID
Or if you do not need the ID to be set for every new record you could just set the current value of the control to the defaultID variable.
Me.TextBoxBatchID = defaultID
Please note the code above would not open the form if the open argument turned out to be empty or 0.
I can not get the OpenForm method to open a form with the correct record loaded. I'm going to do my best here to provide the details:
The 'source' form is based off Table A, and it is read-only. A button on the source form is being used to open a 'target' form that is used for editing records, also from Table A.
I have tried using the wizard to create the button that opens the form based on primary key equality. The result from the wizard is that no matter what record is in context on the source form, only the first record in Table A is ever loaded by the target form.
I have tried using a procedure with the following variations:
Dim frm As String, whr As String
frm = "Target Form"
whr = "Forms![Source Form]!ID = Forms![Target Form]!ID"
'whr = "[ID] = [ID]"
'whr = "[ID] = Forms![Target Form]!ID"
DoCmd.OpenForm frm, acNormal, , whr, , , 1
I could either get only the first record to load in the target form or get a new record to load in the target form. But I can not get the source form's loaded record to determine the record loaded into the target form.
Thanks for any help
I think you got close. You want the WhereCondition to be the same as a WHERE clause for a query of Target Form's record source, but without the word WHERE.
Dim frm As String, whr As String
frm = "Target Form"
whr = "[ID] = " & Me.ID
Debug.Print "whr: " & whr
DoCmd.OpenForm frm, acNormal, , whr
I intended Me.ID as the value of a control whose name is ID and is bound to a field in the current record of Source Form. Me is shorthand for "this form". One reason it's useful is because you wouldn't have to revise your code if you later decide to give the form a different name.
I added the Debug.Print statement so you can switch to the Immediate Window (Ctrl+g), and copy the whr string, then paste it into a new query based on the same record source which Target Form uses. That could be helpful in case you still don't get the correct record displayed when Target Form opens.
Your version also included 1 as OpenArgs to OpenForm. I didn't see how you were using that, so left it off. If Target Form includes an event procedure which does something with OpenArgs, make sure it doesn't override your WhereCondition.
I have an Access 2003 form with one subform, set to continuous forms, in a subform control. For one record in the main form, 1 to many records will appear in the sub form. The data displays properly.
The main form is named Widgets and the sub form is named Transactions. There are 5 textbox controls that display the data in the subform. The one in question is ReceiptDate.
What I would like to do is look at the values and determine if there was a receipt for the year 2009, and if so then change the background of that row to yellow so it stands out when the user encounters that condition. Maybe even change the date field's font to boldface..
I tried many ways of referencing the subform's controls. When I have tried Me.Transactions.ReceiptDate I have only received the first record in that subform. I'd like to be able to loop through them and see if the condition is met. I tried Me.Transactions.ReceiptDate(1) and Me.Transactions.ReceiptDate(0) and so forth.
I tried the For Each ctl In Form.Controls route as well. It worked for a few iterations and then I received a run-time error 2455 "You entered an expression that has an invalid reference to the property Form/Report".
I had the subform in "datasheet" mode but thought that was causing me not to be able to read through an array of subform controls. So I changed it to "continuous" mode. I get the same errors for either.
Is there a way to reference specific "rows" in the subform and do something based on a value found? Also, I am performing this in the On Current event as I dont know where else to put the code. The subform loads before the parent form so its possible that these controls arent even fully "there" but then I do get the first row's date when I try it in the Immediate Window.
UPDATE 12.23.2010:
With the gracious help of #Remou I am able to debug.print the ReceiptDate fields from the RecordSet. This is great because now I can evaluate the data and do certain things based on the values.. The #Remou's code helped me put this into the OnCurrent event:
Dim i As Long
Dim frm As Form
Dim rs As DAO.Recordset
' Get the form and its recordset.
Set frm = Me.Form
Set rs = frm.RecordsetClone
' Move to the first record in the recordset.
rs.MoveFirst
' Move to the first selected record.
rs.Move frm.SelTop - 1
' Enumerate the list of selected records presenting the ReceiptDate field
For i = 1 To rs.RecordCount
Debug.Print rs![ReceiptDate]
rs.MoveNext
Next i
So now that I am able to know which row in my subform has a receipt from 2009, I need to be able to highlight the entire row or rows as I come across them in that for loop. How can I reference the actual row? Datasheet view or Continuous Forms view - I have tried both.
Conditional Formatting is great but it only allows me to highlight one particular record and I'd much rather be able to do this via VBA because...... from here I will want to give the use the ability to click on any record in the subform and get the receipt details and potentially print them.
Any ideas?
In this situation, it is best to use conditional formatting.
To refer to a control on a subform, refer to the subform control by name, then the form property to get the form contained, then the control by name:
Me.MySubformControlName.Form.MyControl
See: http://www.mvps.org/access/forms/frm0031.htm
I have finally got it. The frm.SelTop = x will set the selected record and from there I can set the background or font style, etc.. Very cool. A simple test for 2009 and setting the selected record:
Dim i As Long
Dim frm As Form
Dim rs As DAO.Recordset
' Get the form and its recordset.
Set frm = Me.Form
Set rs = frm.RecordsetClone
' Move to the first record in the recordset.
rs.MoveFirst
' Move to the first selected record.
rs.Move 0
' Enumerate the list of selected records presenting
' the CompanyName field in a message box.
For i = 1 To rs.RecordCount
If Year(rs![ReceiptDate]) = 2009 Then
frm.SelTop = i '<-----------------------------
End If
rs.MoveNext
Next i
In order for me to get the email off the bottom of my continuous form, I used this much simpler code (as I avoided the RecordsetClone code)
Me.[email subform].Form.SelTop = Me.[email subform].Form.Count 'selects the last row
str = Me.[email subform].Form.Email 'capture the value of the last row
MsgBox str