Subform doesn't recognize textbox source after reopening file - ms-access

I have one main form with a subform and two date text boxes. The subform has been connected to the query with a date value column. I put criteria in query Between [Forms]![frm_mainform]![txt_from] And [Forms]![frm_mainform]![txt_to]. Also, both text boxes have Default Value set as =DateAdd("d";-30;Date()) and =DateAdd("d";30;Date()).
So, it works fine until I close the document and reopen it. After that subform doesn't recognize text boxes and lanch me entry boxes for both values.
Data entry form pic
After I change just one letter in Name it works again, and when I close the document again it lost it again.
Is anyone familiar with this behavior?

I have put Me.Refresh on Form_Load() event and it fixed it.
Still, I don't know what causes such behavior.

Related

Closing form in access automatically updates first row in table

I am working on a simple database in MS Access for small manufacturer. Its very simple and small.
Basically I have a form where workers can log what they did during the day. I have 4 Combo boxes and 3 text boxes.
Every think works like a charm until I close the form. When I close it, it updates first row, but it only changes what was in text boxes, data form combo boxes is the same. E.g. worker enters some data, clicks insert and table is updated with this new information. Then when he/she clicks close this information is inserted in first row in this table, but changes only what was in text boxes.
Same think happens no matter what, if there is data in those boxes or not.
This is how I close my form:
Public Sub Close (formMe As Form)
DoCmd.Close acForm, formMe.Name
DoCmd.OpenForm "WorkerLogin"
End Sub
But I noticed that same think occurs if I ran the form as pop up, and then closing it with X.
I appreciate any suggestions.
Thank you
I manage to fixed it with
Me.Unload
before closing it.

Obtaining textbox value in change event handler

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.

Expression in control source and default value

I am pulling my hair here.
I want to link 2 subform fields to a field in the main form.
I used Parent.MyFieldName.Column(3) for the control source of the first subform field and it is displayed fine. The Row source of my second subform field is using the first subform field as a criteria for its query.
The problem is that this combobox remains empty when I use the above code but is populated if I give the first field a constant value.
Feels like the combobox is getting filled before the retrieval of data from the first subform field.
I tried moving the above code form control source to default value but then both fields remain empty.
What am I doing wrong?
I have found the main reason why things weren't working - Subform controls load before the rest of the main form. This means that at the time my subform dropdown menu seeks the data in the other form fields they still haven't appeared and thus I am left with an empty dropdown menu.
I solved this by removing the sourceobject of the subform control and adding
Me!Subform1.SourceObject = "SubformOtpuskaneIzhMat"
In some events in the main form.

Access 2007 Split form VBA: acNewRec on open prevents tabbing through form - acts like the first field is not 'selected'

I hope someone can help me out, or at least help figure out a workaround.
I'm using Access 2007's split form feature, and have the code below run on the Form_Open event, as well as after two button_click events. The code works fine when run after the button_click events, but when it runs on the form_open event, it causes problems.
If form is opened and user enters text in the first field, he/she cannot use Tab or mouse to select the next form field. The user is stuck on the first form field until pressing Esc to cancel the data entry. In order to successfully enter data in the first form field when the form is opened, a user must first select another form field, then re-select the first form field then enter text in first form field. After this nonsense, the user CAN select next form field with Tab or mouse. This must be performed once every time the form is launched. The same VBA code on the button_click events works fine.
Noteworthy: When the form is first opened, NONE of the form fields in the datasheet section of the form appear 'Selected'. When a user begins to enter data in the first form field, the 'new record' marker (*) moves to the second row as it should, but the first row does not show the data being input. This behavior is odd.
After performing the clear field, click another field, click back to first field workaround described above, the datasheet shows properly selected fields and Data as it is input.
Any ideas? Is this a bug? Is there an easy workaround, such as performing the field-select workaround via VBA at form open?
Any help is MUCH appreciated.
Code:
DoCmd.ApplyFilter , "([Contractor].[CheckOutStamp] Is Null)"
DoCmd.GoToRecord , "", acNewRec
Link to mdb:
https://docs.google.com/leaf?id=0B-jx09cwIQDsYWM2MzMzMDQtYjUzNi00N2E5LWFjYTktNzFiYWYzMDZiYWU1&hl=en&authkey=CPPmoMEF
Some thoughts:
Try moving it from OnOpen to OnLoad. The events in OnOpen can happen before the data is actually loaded, where as OnLoad happens after that is already done.
Also, you might want to just set the form's Filter property to [Contractor].[CheckOutStamp] Is Null and set the FilterOn to Yes, and set the form to DataEntry, which means it defaults to a new record upon open, and doesn't load any of the older records. Once it's open, you can change the form's edit/add mode to whatever you like.

Access: Dropdown doesn't display value in first record of continuous subform

I have a continuous subform in an Access 2003 form. The first control on the subform is an unbound dropdown box. When the form (and subform) are opened the dropdown on the first record receives focus, but the text is blank (just a blinking cursor). If I click on any other record, the first record's dropdown text then display the correct value (and the record I clicked on does not go blank).
I tried the following line
Call DoCmd.GoToRecord(acDataForm, "DetailsSubform", acNewRec)
in the both the main form's and subform's Load, Open and Activate events with no luck. Either I get an error that the subform isn't open, or there's a conflict with the routine that populates the dropdown.
I'm looking for a way for either the dropdown to display its value, or simply jump to a new record (my client would prefer the latter but can live with the former). More information can be provided if needed, I just wanted to start with an overview. Thanks.
You probably need the current event. Note that an unbound control in a continuous subform will display the same value for all records.