Obtaining textbox value in change event handler - ms-access

I've written a form that performs queries asynchronously as text is typed into a textbox, however I somewhat arbitrarily seem to get the following error thrown: "You can't reference a property or method for a control unless the control has focus."
The immediately relevant code is:
Dim UpdateRequested As Boolean
Dim qryText As String
.
.
.
Private Sub txtBox_Change()
qryText = txtBox.Text
UpdateRequested = true
End Sub
Some place in the ellipses is the code that handles dynamically loading an ADODB record set, populating a local table, and refreshing a sub form. However, even when I disable this code, the problem persists: Sometimes I get the error. Sometimes I do not.
This seems to be persistent through closing the database and reopening it. Every time it starts working again, it's because I've been fooling around with code in the debugger, but I'm not sure what exactly is causing it to magically "just work" or what is causing it to not work at all.
Update
Just to make things more puzzling, I added a couple of simple event handlers:
Private Sub txtBox_GotFocus()
MsgBox "Got focus"
End Sub
Private Sub txtBox_LostFocus()
MsgBox "Lost focus"
End Sub
I run the form. I click in the test box. I receive the "Got focus" message. As soon as I type I see the error as described above. If I re-open the form, I can click between the text box in question (which itself is unbound) and a bound text box in the sub form and see both "Got focus" and "lost focus" messages as one would expect. Furthermore, showing a message box with the current value of "Screen.ActiveControl.Name" shows the expected name just before the Text property is accessed.

I know this is an old thread but it's the first I found when I had the same problem. None of the answers helped except Kaganar's own solution, which pointed me in the right direction. I'm guessing the reason people had trouble reproducing the error is there are some important details missing from Kaganar's description:
The Textbox was in the form header (or footer).
The form did not allow additions.
Because I believe the full answer is...
The Text property of any control is inaccessible when the form has a record source with no records to edit
I think there is part of Access that does not realise the textbox exists :) To understand how that might come about...
Put the unbound TextBox in the detail of the form
Do not allow additions
Set the recordsource to return no records
Open the form.
Hey presto! No Textbox.
Return a record, or allow additions, or delete the recordsource, et Voila! There is your Textbox with it's Text.

I added a text box named txtFoo to my form. Here is the procedure for its change event.
Private Sub txtFoo_Change()
Debug.Print "Value: " & Nz(Me.txtFoo.value, "*Null*") & _
"; Text: " & Nz(Me.txtFoo.Text, "*Null*")
End Sub
Then, with nothing in txtFoo (IOW its value is Null) when I type "abc" into txtFoo, here is what I see in the Immediate window.
Value: *Null*; Text: a
Value: *Null*; Text: ab
Value: *Null*; Text: abc
Basically, each character I add to the text box triggers its change event and prints the text box's current contents to the Immediate window.
As far as I understand, you want to do something similar ... except you want a different action in place of Debug.Print. Take another look at your change event procedure and compare it to mine.
Private Sub txtBox_Change()
qryText = txtVendorName.Text
UpdateRequested = true
End Sub
That is the change event for a control named txtBox. Within that procedure you reference the .Text property of a control named txtVendorName. However txtBox is the active control at the time its change event code runs ... so you can not access the .Text property of txtVendorName because it is not the active control.
Given that this problem surfaces for only the one form, but not on other new forms, I would suspect the problem form has become corrupted. Read the 2 answers to this SO question and try decompile to cure the corruption: HOW TO decompile and recompile. Decompile is often recommended as a routine practice during development.
You could also use the undocumented Application.SaveAsText method to save your form as a text file. Delete the bad form, and use Application.LoadFromText to import the saved text copy.
Make sure you have a backup copy of your db file in case anything goes wrong.

To set or return a control's Text property, the control must have the focus, or an error occurs.
To move the focus to a control, you can use txtBox.SetFocus or DoCmd.GoToControl "txtBox".
Also, the Text property is not always available:
While the control has the focus, the Text property contains the text data currently in the control; the Value property contains the last saved data for the control. When you move the focus to another control, the control's data is updated, and the Value property is set to this new value. The Text property setting is then unavailable until the control gets the focus again.

The form had a lingering data source. I'm not sure why this would cause to the behavior described above, especially considering the text box controls are unbound, however since removing the data source the text boxes are behaving as expected.

You said "somewhat arbitrarily" I think if everything is fine you must get the error when your form's recordset is empty.
In fact it's a know bug in Access and this error can occur if these conditions are met:
a) The control is in the Form Header or Form footer section
b) The form is filtered such that no records match (or there are no records)
c) No new record can be added.
In this case, the Detail section of the form goes blank. The controlis still
visible, but Access gets really confused and can throw the error you
describe.
More info:
http://allenbrowne.com/bug-06.html

I know my answer is out of date. Yet you just can set focus three times. On TextBox in header, on any texbox in detail space and On TextBox in header again. I use access 2003.

Related

Textbox with reference to dropdown returns nothing

I have cmbCountry on a form as an unbound dropdown. The dropdown works as expected. I have setup a textbox called txtCM_ID on the same form, in which I want to display the ID that was selected in the dropdown.
When I enter the record source for the textbox as =Forms![frm_ClientModel]!cmbCountry.Column(0) access automatically changes it to read =[Forms]![frm_ClientModel]![cmbCountry].[column](0)
My version returns the correct information when i test it in the VBE Immediate window. The code that access produces returns the below when run in the Immediate window:
runtime error 450: Wrong number of arguments or invalid property
assignment
The frustrating thing is, that regardless of where or how i enter the code in the control source, access changes it to its version. When the form is opened the txtCM_ID simply remains blank.
I have also tried to go around this problem by changing my reference to the following: Forms("frm_ClientModel").Controls("cmbCountry").column(0)
While this version (also tested and ok in VBE) doesn't produce an error, it too returns nothing in the text box.
What am I missing / doing wrong / overlooking?
Use just
=[cmbCountry]
To access the value of cmbCountry from other control.
I was not able to specifically determine why this happened, but ended up solving the issue by using the OnClick Event of the Dropdown and writing the needed info into the Textbox via VBA with this:
Private Sub cmbCountry_AfterUpdate()
Me.txt_CMID = Forms("frm_ClientModel").Controls("cmbCountry").Column(0)
End Sub
If anyone else finds an answer as to why form controls did not work properly I would be interested in hearing from you.

Error upon opening form

I am going crazy with the easiest piece of code that just won't work. I can't fugure out why. I have a button on a form that opens another form but when i click it I get the following error and don't really know what to change:
my code click code is:
Private Sub HSbrowse_Click()
DoCmd.OpenForm "frmSearchPCR"
End Sub
Any Ideas?
I've had this issue several times. I believe that it is caused by the module of your form becoming corrupt. To fix it, set the "Has Module" property of your form to "No" and save and close your form. This will delete the module and any code it contains, so you will need to copy your code first. Open the VBA window to verify that the module was deleted, then reopen your form and set its "Has Module" property back to "Yes" and paste your code back in. This method has solved this problem for me on more than one occasion.
Does the current form has any ActiveX Control in it?
If not then your form may be corrupt,
rebuild your database using e.g. TM-RebuildDatabase
Start by removing individual controls one-at-a-time. When your error goes away you will know the offending control, if that is indeed the problem.
Does the form have underlying data? Did you remove that to see if the problem changes?

Can't change Text of TextBox in Report_Open

All I want to do is set a textbox or label's text to something dynamic when the report is opened with a click of a button in another form. I've solved everything except actually changing the text.
This code gives run-time error 2478 on SetFocus:
Me.tFilial.SetFocus
Me.tFilial.Text = filialen
Without SetFocus I get a run-time error saying the text can't be changed without switching control to the control in question.
What is allowed where is always in question in Access, it seems. How do I solve this? Can I set the value on the buttonclick in the other form with
Reports![rptPressSchema]![tFilial].text="Hello"?
I would be happy to use a label instead, if that solves it. But the bottom line is I can try to do this every which way but I thought I'd ask you for advice as to best practice, as this must be a very common task indeed.
From the Access help:
While the control has the focus, the Text property contains the text data currently in the control; the Value property contains the last saved data for the control. When you move the focus to another control, the control's data is updated, and the Value property is set to this new value. The Text property setting is then unavailable until the control gets the focus again. If you use the Save Record command on the Records menu to save the data in the control without moving the focus, the Text property and Value property settings will be the same.
Basically, the .Text property serves no purpose in a Report because individual controls cannot receive the focus. However, as #Remou stated in his comment, you can simply replace .Text with .Value and your code should work fine (no need to set focus when updating the value).
spent a lot of time for searching and trying. Finally figure the things out...
To dynamically set the TextBox content, it is convenient to use
tbTest.Value = "hello"
but the trick is if you are using this On Open, it will be in trouble...
Run-time error '2448'
You can't assign a value to this object.
So you need set the value On Load
Private Sub Report_Load()
Me.tbTest.Value = "hello"
End Sub
Private Sub Report_Open(Cancel As Integer)
'Me.tbTest.Value = "hello"
End Sub
I see nowhere for any explanation of this, my guess is for Open event, the object is still not initiated (document for explaining Load and Open event)...

Access form's Detail's event only triggers on first record

I'm designing a Form in Access 2003. My goal is that when the user double-clicks on a record (aka Detail), a second form opens. This is easy enough to do when I start from scratch.
But of course, I'm not starting from scratch. I've been working on this for a few hours now, have added some conditionally formatted textboxes, fields, etc, and don't want to start over if I can avoid it.
I don't know if I turned on/off some obscure property, but I've been working on this form for a few hours now and the Double-Click event of the Details section only triggers when I double-click on the first record or any other with conditional formatting.
To troubleshoot this, I've gone so far as to comment out all the other VBA script except this:
Private Sub Form_Load()
Me.Application.DoCmd.Maximize
End Sub
Sub GotoFrmDetails()
MsgBox "You Double Clicked Me!"
End Sub
Even with the VBA script reduced to just these lines, the DoubleClick event only triggers on the first record and no other. Can anyone tell me what wrong? I'd rather not have to rebuild this from scratch.
I find the click events of form sections to be more trouble than they are worth (click event doesn't pass through to the form if user clicks on a control or label, etc). What I use instead are transparent command buttons.
Just add a command button to your form and set its Transparent property equal to True. Then you can use the Z-order to make fine-grain changes if necessary (send to front/back, send forward/back).
They can be hard to find once you make them transparent, so be sure to give them descriptive names.
If you want the user to be able to double click anywhere in the detail section, just make the transparent command button's height/width match the section height/width.
Move your code from the Detail section DoubleClick event to the transparent command button's DoubleClick event and you should be good to go.
I'm wondering if the rows are getting their Locked property set to Yes, or their Enabled property set to No during your condition formatting.

MS Access: How do forms communicate values to each other?

I have a form (FORM-A) that requires the user to select a vehicle.
The user should click on a button on FORM-A that say's select vehicle.
A selection form (FORM-B) should open where the user can select a vehicle.
The selected value should be communicated back to FORM-A.
How would you accomplish this in MS Access 2010?
FORM-B is a continuous form that contains a picture of the vehicle and some other information.
From what I understand from your question, you want formB to open a kind of pop-up. When the pop-up is closed, its result is put somehere in the calling form.
Solution proposal:
a) open FormB using syntax docmd.openform "formB", windowmode:=acDialog.
This will prevent execution of the next lines until formB is closed or hidden.
b) in the OK button of FormB, just hide the form, do not close it.
c) when code resumes in formA, you can now
check if formB is still open. If not, it's been cancelled
read the value in hidden formB (still open), then close formB
Otherwise, you could also have formB to update a control in formA before closing. But I don't like that approach because then formB is not reusable, and it creates an unnecessary dependency between formB and formA.
I am not sure why you would need a separate form for this - just have the first textbox be a listing of all the records of vehicles in the database, where you would select one, and the rest of the vehicle information is auto-populated from the vehicle table, but not copied into your parent table. Of course, I am not sure of your table structure either, so there might be a reason for this method that isn't apparent to me.
The benefits of the method above is that if you add more vehicles, your selection box is automatically updated - and you keep the forms you have to load to a minimum (always a good performance move)
You can reference the forms in this manner forms!formName!controlName.
Once you see how this works you will be able to fool with it to get it working with your existing setup. Let’s use 3 controls a text box on Form-A, an image on Form-B and a text box on Form-B. The text box on Form-A will be named txtVehicle, the image on Form-B will be named imgVehicle and the text box on Form-B will be named txtVehicleName.
You can set the name of a control within properties. When you click on imgVehicle it will put the value from txtVehicleName into txtVehicle.
You will have to do a little coding - it's easy though if you have not done it before. Under properties for the image you will see events. If you click on the "On Click" event you will get a drop down list. One of the choices will be [Event Procedure] - choose that. A little button with 3 dots on it will also show up at the end of the row. Click it and you should be taken to a code window with some code like this in it.
Private Sub imgVehicle_Click()
End Sub
Here is where you put your code. Something like this should work. This is it in its most simplistic form.
Private Sub imgVehicle_Click()
Forms!Form-A!txtVehicle=forms!Form-B!txtVehicleName
End Sub
Now although that will work, there are a few things that we should be doing in this method that we are not. We should reference Form-B directly since we are in it, we should verify that Form-A is in fact open.
Private Sub imgVehicle_Click()
If currentproject.allforms(“Form-A”).isloaded then
Forms!Form-A!txtVehicle=me!txtVehicleName
End if
End Sub
Hope that helps
You can create an instance of formB within formA and control it. Below is the VBA code for formA. When clicking on a button on formA, you create a new instance of formB and give it focus. At the same time, you can set properties for its controls. You can use this approach to set the right picture in your control on form B. Hope this helps.
Example:
Option Compare Database
Dim fB As Form_FormB
Private Sub btnA_Click()
Set fB = New Form_FormB
fB.SetFocus
fB.tbxB.Text = "Some text sent from A to B!"
End Sub
If you want both forms to be visible all the time, I suggest using a subform with the list of all the vehicles or just details for the one that the user selected.