Access Copy Value of Textbox on text change - forms

I've a form in MS access that holds a subform showing records from a table. I've got a textbox whichs content is based on the selected record form the subset. If i select a record with id 2 then the value of the textbox is set to 2.
I want to copy the value of the textbox to another textbox of the form each time the value is changed.
The onChange eventhandler on the textbox bound to the subform does not trigger, neither does any other event when i select a record in the subform.
I'd either write some vba code to copy the text or use a makro.

It probably is triggering if you change the field manually, e.g., this code works for me:
Private Sub Text0_Change()
Text2.Value = Text0.Text
End Sub
(note value and text, as what you see in the field while you are editing it is not that same as the value of the field)
but not if you update it through VBA
Setting the value of a control by using a macro or Visual Basic doesn't trigger this event for the control. You must type the data directly into the control, or set the control's Text property.
https://msdn.microsoft.com/en-us/library/office/ff821734.aspx
If the fields were named like in my example, I'd have whatever VB that updates Text0 also update Text2

Related

MS Access: Enter a specific text in a form using command button from another form

I would like to ask for your help on the following issue in MS Access.
I had created a form "CustomerListF", filled with command buttons for each client. For each button, I had created the following code:
Private Sub cmd_outlets_ABC_Click()
DoCmd.OpenForm "OrderFormF"
Forms!OrderFormF!Outlets = "ABC"
End Sub
The button would then open another form "OrderFormF" and enter "ABC" in the textbox named "Outlets". However, I realized the second line of code (Forms!OrderFormF!Outlets = "ABC") would always create a phantom record in my subform, which is located in "OrderFormF", and this record would travel to other clients' forms. This phantom record is usually created when the commandbutton is clicked twice (double clicks or subsequent clicks). It is a headache when the record starts to shift around.
enter image description here
I would like to seek your advice for vba code to edit the second line of code.
Thank you.
This approach for filling the field in opened form is not good. OrderFormF initialization and second line of your code with assigning value to the field may be executed in different sequence, they are not synchronized.
In order to avoid this, you should pass the argument to OrderFormF by another way. I would recommend to use OpenArgs parameter:
DoCmd.OpenForm "OrderFormF",,,,,,"ABC"
And then in Load event of OrderFormF assign the value to textbox:
Me.Outlets = Me.OpenArgs
In this case the "ABC" value will be assigned to first row in the form or to new record, if form is empty. Before assigning you can select the row if you need to assign value not to the first row. Also this way will protect you against double click on button, Load event executed only once during first form opening

How to have a button change the form within the subform "window" in Ms Access

I need to create my own "navigation" form in Ms Access. Basically, I have a MAIN form that has one field named LotNumSelect
Then, buttons representing each form the user would like to view.
Button1 - Customer
Button2 - Job Data
etc.
Once the customer clicks a button, the procedure would open the corresponding form in the subform window to the record matching the LotNumSelect field.
So far, I have this.
Private Sub ContactInfo_Click()
Main_Customer_Sbfm.SourceObject = "Main_Customer_Sbfm"
Forms!Form1!Main_Customer_Sbfm.Form!Lot_Number.SetFocus
This nicely switches the form within the subform. the problem is how do I get the form within the subform find the record associated with the data in the LotNumSelect Field?
I tried this..
DoCmd.FindRecord LotNumSelect.Value, acEntire, , acSearchAll
but it's giving me a runtime error of "2162"
"A macro set to one of the current field's properties failed because of an error in a FindRecord action argument.

Update event handling in Access form

I’m newbie in VB and Access. So I have table with Yes/No column „Channged”, and some text column, when I change text in text field (using form) I want to set Changet to true.
In my form, I handle Update event:
Private Sub Question_Updated(Code As Integer)
End Sub
How can I update Changed column through this method? Thanks.
You can use the following code to set your Yes/No UNBOUND field as long as it is included in your recordset for the form. Note that I am using the 'AfterUpdate event for the field you will be changing. The update will take place AFTER you move to another record or close the form.
Private Sub Question_AfterUpdate()
Me!YesNo = vbYes
End Sub

Word forms - display a form value in text below the form

how to display a value from MS Word Form.
Eg:
I have a form with one dropdown list with following values:
DropDown1: a, b, c
When I select value a, I want to display the value in plain text. Eg:
Value of the selected field is: a.
I would prefere not to use vba.
You can use the (assuming DropDown1 is a ComboBox control) ComboBox's Change routine.
Private Sub DropDown1_Change()
Selection.TypeText DropDown1.Value
End Sub
If it is a "legacy form" (not a form with ActiveX controls or content controls, then
a. set the properties of the dropdown to calculate on exit, and the "bookmark" to (say) dropdown1.
b. put a { REF dropdown1 } field in the document where you want the value
c. protect the form in the usual way.
The value will only appear when the user tabs out of the dropdown.
It's not exactly "plain text", but it's not a form field either. If you need "genuine plain text", you would need code.
If it is a content control, then you would need to set up a Custom XML part and link the dropdown to it, then insert a plain text content control that was also linked to the same element/attribute in that part. That requires code at design time (or a third party add-in, or Word 2013) but not at run time.

How do you edit records from a VBA form that you want to interactively select?

I have a set of ComboBox's in an MS Access 2003 DB that are all bound to fields in a single table. However, the data that they allow you to select doesn't come from that table and instead comes from various other tables. This works fine for the record creation story but now I want to be able to edit the record retroactively. The problem is that I can't figure out how to refill the form elements without writing a bunch of custom code.
My initial inclination is to provide a combo box that limits your choices to record IDs and then do a custom query and use that to set the selected values in all of different form elements. However, I feel like I should be able to do something as simple as DoCmd.GoToRecord , , , ID and the form should repopulate just fine. I'm not opposed to doing the busy work but I'm sure I'm just missing something in my relatively puny knowledge of VBA and Access.
Just to add to the mix, I would offer two approaches, one recommended, the other not.
Approach 1: If you've bound your form to the whole data table (this is the non-recommended approach), you can use the combo box wizard to navigate to the requested record, but I wouldn't recommend it in recent versions of Access:
a. it doesn't allow you to properly name the combo box before it creates code.
b. the code is just WRONG.
Here's the code I just produced in my test database:
Dim rs As Object
Set rs = Me.Recordset.Clone
rs.FindFirst "[InventoryID] = " & Str(Nz(Me![Combo2], 0))
If Not rs.EOF Then Me.Bookmark = rs.Bookmark
This is wrong in so many ways it's just remarkable. This is what the code should be:
With Me.RecordsetClone
.FindFirst "[ID]=" & Me!cmbMyComboBox
If Not .NoMatch Then
If Me.Dirty Then Me.Dirty = False
Me.Bookmark = .Bookmark
Else
MsgBox "Not Found!"
End If
End With
There is no need to clone the form's recordset when the RecordsetClone already exists.
There is no reason to use an object variable when you can just directly use the pre-existing object.
There needs to be a check for a dirty record before departing the record because if you don't force the save, errors in the save process can lead to lost data.
But the better approach is this:
Approach 2: Use the combo box to change the form's underlying recordsource.
The AfterUpdate event of your combo box would look something like this:
If Not IsNull(Me!cmbMyComboBox) Then
Me.Recordsource = Me.Recordsource & " WHERE [ID]=" & Me!cmbMyComboBox
End If
Now, this only works the first time, as on the second resetting of the Recordsource, you end up with two WHERE clauses, which is not good. There are two approaches:
a. assuming that the form opens without a WHERE clause, store the opening recordsource value in a module-level variable in the form's OnLoad event:
Private Sub Form_Load()
strRecordsource = Left(Me.Recordsource,Len(Me.Recordsource)-1)
End Sub
And at the module level, define strRecordsource accordingly:
Dim strRecordsource As String
Then in the combo box's AfterUpdate event, you have this instead:
Me.Recordsource = strRecordsource & " WHERE [ID]=" & Me!cmbMyComboBox
Now, if your form opens with a WHERE clause already defined, it gets more complicated, but I'll not go into that and leave it as an exercise to the reader what the best approach might be.
I presume that you've already set up the row sources for each combo box. So long as you haven't limited the combo box to that list; it should display what you have stored in that column.
However, if your Combo Box changes its list for each row you can do something like this in the record's OnCurrent event or the field's GotFocus event:
Me.combo_box_name.Requery
After re-reading your question, I think I see what you are trying to achieve. You're on the right track with GotoRecord, although I would probably use OpenForm in this case, because it has a WhereCondition property that allows you to use SQL to specify exactly what record to open. It sounds like you want to implement a "jump to record" type functionality in your form, where the user selects a record ID from a list and the form changes to display the selected record.
One possibility is to switch to the new record each time the user selects an item in the ComboBox. You can handle this in the ComboBox's Click event.
I'll use a simple example: suppose you have a Students table, and a StudentForm for viewing/editing records in the Students table. The StudentForm has a ComboBox cboStudentID that is bound to the Students.ID column via it's RowSource property. When you select a student ID in the ComboBox, the StudentsForm will switch to display the corresponding student record.
In the Click event handler for the ComboBox, you can code this "jump to record" functionality with something like the following:
Private Sub cboStudentID_Click()
Dim recordID As Long
'The ItemData property will return the value of the bound'
'column at the specified index.'
recordID = cboStudentID.ItemData(cboStudentID.ListIndex)
'Jump to the record. This assumes we want to use the same form.'
'You can change the form name if you want to open a different form when'
'the user selects an ID from the ComboBox.'
DoCmd.OpenForm "StudentForm", WhereCondition:="Student.ID=" & recordID
End Sub
As David W. Fenton points out in the comments, you can shorten the following line:
recordID = cboStudentID.ItemData(cboStudentID.ListIndex)
to this:
recordID = Me!cboStudentID
or just:
recordID = cboStudentID
since the default value of the ComboBox in this case will be the value of the bound column at the current ListIndex. In this case, you could just remove recordID altogether and code the Click event as follows:
Private Sub cboStudentID_Click()
DoCmd.OpenForm "StudentForm", WhereCondition:="Student.ID=" & cboStudentID
End Sub