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)...
Related
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.
This is not working.
Me.Textbox = Me.ListBox.ListCount
It say;
What code should I put into it?
When I load the Form, it should display the count of the ListBox's items on the textbox.
The TextBox and ListBox are not the names of the variables, but oh the classes. When you instantiate an object of Textbox (or ListBox), VB gives it the name TexBox1 (or ListBox1). You can change this name in the properties window. I suppose what you have now are TextBox1 and ListBox1.
Me.Textbox1 = Me.ListBox1.ListCount
There is nothing wrong with the syntax of your code. So the first thing that should be looked at is the names of your listbox and textbox. By default ms-access calls them list and text followed by a number. To find the assigned name go to their property and then the other tab and name of the control is at the top of the list.
Also make sure that you are running the code from the form's onload event. You may try the forms oncurrent event to see if it makes ant difence
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.
Working in both A2003 & A2007.
How do we ensure that a selected TextBox gets the focus when the form loads? If we put MyTextBox.SetFocus in the Form_Load then we get the error:
can't move the focus to the control
This form is designed for rapid data entry, and the form somewhat rearranges itself based on the last used settings. So there are several different textboxes any of which may need the focus depending on the user. We can't just fix it in design time by giving MyTextBox TabIndex=0.
The help says something about calling Repaint which just doesn't make any sense at all:
You can move the focus only to a
visible control or form. A form and
controls on a form aren't visible
until the form's Load event has
finished. Therefore, if you use the
SetFocus method in a form's Load event
to move the focus to that form, you
must use the Repaint method before the
SetFocus method.
The best bet in this case, is to ensure that the textbox to get focus is numbered 0 in the Tab Index property.
You cant set the focus as the controls don’t really exist yet, try putting the code in the OnActivate event instead
Or just put a DoCmd.Repaint in the OnLoad event before trying to set the focus. Both should work but I'm not near a computer to check
In my experience, I've always gotten that error when the control I was trying to set focus to was either 1)not visible or 2)not enabled. I assume you've already checked those, but it would be worth double checking at runtime when you get the error message (especially since you said you are shuffling the controls at runtime).
I use the .SetFocus method pretty regularly without trouble. I don't recall ever getting an error message when setting focus to a control that already has it as Remou stated in his answer.
I believe there is also a third case that occurs if you try to set focus to a control in the form header/footer of a bound form that has had all of its records filtered out. I know that situation causes "disappearing" contents in an unbound combo box, but I think it may also play havoc with the SetFocus method. If you are opening the form in Data Entry mode, though, that should not be an issue.
Move SetFocus to the form's On Current event. Should work then unless perhaps the form's record source contains no records and you've set the form's Allow Additions property to No. In that case your text box will not be available to SetFocus on, but in my testing it doesn't throw an error.
What's the best way to refresh a list box in MS Access? The way I have tried it doesn't seem to work :-/
This is my current method (onClick event of a button on the same form):
Me.orders.Requery
I have also tried
orders.Requery
and
Forms!currentOrders.orders.Requery
but none of them seem to refresh the list box's contents - I see no new items, even though I know that there are some. Closing off the form, and then re-opening it does show the new items, however.
You could try and requery the form first and then requery the listbox
You need to use a temporary textbox, then your listbox will refresh automatically.
The following solution worked for me. (I don't know why, but using direct value from searchbox didn't work for me.) i have used no button. For instant search:
a textbox where you actually type. its name "Finder"
a textbox where you save the searching string temporarily. its name "Search".
a listbox where you show result. its name "lstResults"
(NB: also to mention, the query, uses the textbox named "Search"; which is our temporary textbox.)
code:
Private Sub Finder_Change() ' "on change" event of "Finder" box
Me.search = Me.Finder.Text ' save the typed text in another textbox and use from there for query (otherwise it might not work)
Me.lstResults.Requery ' update listbox "lstResults" on any change
End Sub