Set focus to specific field in Access 2010 - ms-access

How do I set the focus on a particular field (in this case, the "Ref:" field which is located in the Form Header) when viewing a form in Form View?

You could use SetFocus in Form_Active
Private Sub Form_Activate()
yourField.SetFocus
End Sub
or you could change the order tab and put your field into first place of the list

Related

how to create a combo box in access which can be converted to text box to enable writing if the user needs to add a new option to the existings?

I was wondering how I can define a combo box in a form where the user can select an option from the existing list, and if he can't find the desired option, a "add a new option" can be accessed which converts the combo box to a text box for writing the new option?
You can put a button next to the combo, for an 'add option' which is triggered when the combo 'not in list' event is hit - and the button then would call a popup form - or in your suggestion, changes the visibility of an overlayed text box that allows for entry. I suggest a popup form, however, because then you can validate the input for length, content, etc - and then once the form or text box is filled in - exiting the text box, or submitting the form would save to your table for the drop down, refresh the combo box source query, and assign the value. I do this in VBA alot with popups.
Sorry my bad english :-(
You can directly use the "On Not in List" event. Look at the examples at
https://learn.microsoft.com/en-us/office/vba/api/Access.ComboBox.NotInList
This is the scenario: A combobox called cb and a textbox called tb. Visible property of tb is false. Combobox and Textbox have the same data source. When you type a value that is not in the combobox list, that value goes to the textbox; when you finished editing textbox, it is hidden again.
Private Sub cb_NotInList(NewData As String, Response As Integer)
' Suppress error message and undo changes.
Response = acDataErrContinue
Me!cb.Undo
' Prompt user to verify they wish to add new value.
If MsgBox("Value is not in list. Add it?", vbOKCancel) = vbOK Then
Me!tb.Visible = True
Me!tb.Value = NewData
Me!tb.SetFocus
End If
End Sub
Private Sub tb_AfterUpdate()
' Hide again texbox
Me!tb.Visible = False
' Update combobox items
Me!cb.Requery
End Sub

How to pass value from subform to mainform ms access

I have a form with a subform (Datasheet view form). When i click a row in subform, i want pass that row value to text box in main form . In subform even wrote this
Private Sub Form_Click()
MsgBox (Noloan)
End Sub
But i don't know how to pass that subform form_click no_loan to mainform.textbox. Thanks for your help.
Additional data:In my db , i have a table which consist field No,NoLoan,Name. NoLoan Field value as i wanna clicked .From that table i create a subform/datasheet view ,subform name is T_Loan1. Then I create Main Form, name is FindLoan2. In that main/parent form i create text box, called Text7, and put T_Loan1 in subform object at footer.
I put this
=[Forms]![FindLoan2]![subformTLOAN].[Form]![Noloan]
in my Text7 Control and It's work.
Thank's for all attention.
Goto to the subform and click on the field which has the row value
Select Field property - click on events tab --> On Click --> Code Builder
Put below code in the Click event where Form_frmMain is your form name and Me.ID is the field.
Private Sub ID_Click()
Forms!FindLoan2.Text7 = Me.ID
End Sub
The best way is to write code that ignores the forms!names. If you copy the form, or change the sub form, or even drop the sub form into a different form, then all your code breaks.
To reference the textbox in the parent form from code, use:
me.Parent!Text7 = me!id
I am not sure which version of MS Access you are using, but it is also possible to use Tempvars to store values for use elsewhere if you have a complicated application which makes referencing another form/field/control difficult.
To create:
Tempvars.Add "TempVarName", TempvarValue
To use:
Tempvars!TempVarName
To remove:
Tempvars.Remove "TempVarName
I try to limit my use of them, as it becomes hard to manage. I am always careful about when I set the values and I ensure that they are removed after I am finished with them to avoid confusion or issues with values persisting when they shouldn't.

Creating Message Boxes

In Access 2010, I need to have a Message Box pop-up when the user selects specific text (Real Estate) in a combo-box (ProductType).
In short...if the [ProductType] entered by the user is any one of the multiple "Real Estate" options available, then show a Message Box that states "USER MUST COMPLETE RESPA TRACKING" and then direct them to that page (tab) to complete additional text fields.
I've attached the following code to the BEFORE UPDATE function of the combo box. But that only creates the message, it does not direct them to the tab/fields that need to be entered.
Private Sub ProductType2_BeforeUpdate(Cancel As Integer)
Dim strPrompt As String
strPrompt = "The Product Type selected is Real Estate. The RESPA Tracking tab MUST be completed at time of App Entry and validated during Underwriting."
If Not IsNull(Me.ProductType2.Value) Then
If Me.ProductType2.Value Like "Real Estate*" Then
Cancel = (MsgBox(strPrompt, vbOK) = vbNo)
End If
End If
End Sub
It seems you've solved the creating a MsgBox issue, and now your remaining challenge is to "direct them to the tab/fields that need to be entered".
Use the SetFocus method to place focus on a specific page in your tab control, or on a control within that page.
This command button example shifts focus to the first control on a page named Page1 ...
Private Sub cmdPage1_Click()
Me!Page1.SetFocus
End Sub
But my guess is you will prefer focus on a specific control within the page. My Page1 contains a text box named txtMemo_field. This version places the focus on that text box. Notice the page name is not mentioned in this version; SetFocus is called on the text box directly ...
Private Sub cmdPage1_Click()
Me!txtMemo_field.SetFocus
End Sub

Populate textboxes with value from listbox selection on child form

I realise this question has been asked before and I have tried numerous suggestions, but I guess I am just too much of a novice when it comes to VBA coding.
Here is my scenario:
I have an Access 2007 "Application" with a few forms. On the main form, I have 2 text boxes. For simplicity let's call them textbox1 and textbox2.
textbox1 is used to enter a style code. a button on the main form then opens another in modal/dialog mode and runs a query with the style code as the where clasue. the modal popup gives me a list box which is populated from the database based on the query that was passed. I then need to select one of the products in the list and upon closing the popup, populate textbox2 with the brand (column2 in the list from the popup) number.
Please remember that I am a novice. Any help would be greatly appreciated.
I would put a button on the second form, and in the _Click() function, put something like this
If ListBox1.ListIndex = -1 Then
MsgBox "Nothing Selected!"
Exit Sub
End If
UserForm1.TextBox1.Text = ListBox1.List(ListBox1.ListIndex)
Unload Me
Where ListBox1 is the list box containing the content you need the user to select from, where UserForm1 is the name of the calling form, and TextBox1 is the name.
Explanation
The ListIndex property of the list box returns the index of the selected list item. If nothing is selected, it returns a -1.
To reference an item on one from from another, you reference the form, the object and the property.
In The example I gave, the form is UserForm1, the object is TextBox1 and the Property is Text. While typing, intellisense should auto complete the object and properties once you type the period.

VBA get previous active control from parent form

I am usgin Access 2010 and I have a form that has a sub form, let's call it sform. I want to print the parent form's active control When I hit a button in sform. I tried
debug.print Me.parent.ActiveControl
But it prints the name of the sub form's container control, while I wanted the one that was active before it. Is that even possible?
Thanks
The active control on the parent form would be the sub form. To figure out what control had the focus before the sub form you may have to do something like the following:
Add a text box to the form called txtFieldThatHadLastFocus or whatever and hide it.
Then in the On Lost Focus event of all your controls on the parent form add:
Private Sub txtMyField_LostFocus()
txtFieldThatHadLastFocus.Value = "txtMyField"
End Sub
Then within the sub form you can figure out what control had focus before the sub form via:
Forms!ParentFromName!txtFieldThatHadLastFocus.value