MS Access Form - unbound textbox causing new record - ms-access

So I have a bound form.
User updates an unbound combo box, then vba {after_update} populates the value of a bound textbox (1).
I also have an unbound textbox (2).
I can enter text in the unbound text and manually change the value as many times as I want.
However, if (1) is already populated, then any change to the unbound text (2), causes a new record in the underlying table.
Can anyone explain why I'm getting a new record in this context ?
It is 'un'bound, so not sure why it has any effect of the underlying record ?
I just want the user to enter text without any update/commit to the underlying record.
Any guidance appreciated.
Thanks

Copy the code into the form's current event, i.e
Private Sub Form_Current()
#place a copy of all the codes in the after update event in here#
End Sub

Related

Force Combo Box List Update with VBA

I am working on an MS Access Form. I am trying to have a Combo Box on the form who's list is narrowed based on information earlier in the form. I have a Query saved in the database that gets the list properly.
The problem I am having is that the query is not called after each update of the form so the Combo Box's list is always what it was when the form was opened.
I have tried to force an update using Me.MyComboBox.Requery in the AfterUpdate event of the previous control but this only requeries the record source of theform, not the row source of the Combo Box.
I have also tried changing the row source property to force a requery but that did not work eithier:
Private Sub PreviusControl_AfterUpdate()
Me.MyComboBox.RowSourceType = "Table/Query"
Me.MyComboBox.RowSource = "qryDynamicComboSource"
End Sub
Thanks in advance!
It looks like everything you're doing is correct. Check your query design if it's actually changing based on your parameters.

How to make Dlookup textbox writeable

I am working on forms and made a single form for data entry and search ( data extraction) but the problem is that i used Dlookup formula on some textboxes for ease in data entry but when i attemp to search access doesn't show data on that textbox and shows the error that the object is read only.
How can i get the textbox show data as well as have Dlookup formula?
Kindly help.
Many thanx
You can set the textbox value by code instead of putting the DLookup into the data source of the textbox.
Putting it into the data source means that you can't edit the textbox at runtime, as you experienced.
But you can set the value once in the Form_Open event, for example:
Private Sub Form_Open(Cancel As Integer)
Me.TheTextBox = DLookup(...)
End Sub
This will cause the textbox to be filled automatically when the form opens, but the textbox is editable and you can overwrite the value.

Refresh query-driven combo box values when field value changes?

I have a combo box on an form where the values are populated based on the value in a separate field.
To do this, I have created a combo box and set the "Row Source" to run a SQL statement.
The problem I am having is that if the data in the field changes, the combo box values do not update.
How do I get access to re-run the query?
See whether this description is reasonably close to your situation.
My form has a text box, txtFoo, and a combo box, cboBar.
The row source property for cboBar is a query which references txtFoo. And I want the combo's contents updated in response to changes in txtFoo. The solution is to requery cboBar from txtFoo's after update event.
Private Sub txtFoo_AfterUpdate()
Me.cboBar.Requery
End Sub
I have had issues in the past with Requery not working or even hanging.
It's not pretty but you might want to try this:
Me.cboBar.RowSource = ""
Me.cboDemoUnit.RowSource = "your SQL statement"
Here is an example of what I have done recently to do the same exact thing, this should help! Its in an afterupdate sub.Example of dynamic combobox

How to set the value of an unbound textbox with a default value to an empty string with Access VBA

I have a database with information about visitors to our department. Here is my setup: A user selects a visitor name from a listbox. Access assigns the values from that visitor's record to temporary variables and opens an unbound form where those variables are the default values in the text field. When a user changes a value in a textbox, an After_Update event uses SQL to update the corresponding field in that visitor's record and assigns the new value to the temporary variable.
After_Update example:
Dim strRUN As String
strRUN = updateVisitorOnDirty(Me.txtVisitorTravelMethod, "VisitorTravelMethod", 1)
HideWarnings (strRUN)
TempVars!strVisitorTravelMethod = Nz(Me.txtVisitorTravelMethod.Value, "")
"updateVisitorOnDirty" is a function that returns SQL to update a field (specified in second argument) in the table with visitor information with the first argument. (The number at the end is says it's a string.) "HideWarnings" turns warnings off, executes the SQL, and then turns warnings back on.
My problem:
When a user clears a textbox, the After_Update event makes the textbox revert to it's previous value.
I thought at first that maybe the unbound textbox was reverting to its default value when it was cleared, but I put a breakpoint in the sub that fires during After_Update:
If I change a value in mytextbox from a to b, debug.print shows the value of mytextbox as b. If I change a value in mytextbox from a to (blank, or empty string), debug.print shows the value of mytextbox to still equal a.
My workaround has been to include a little "x" near the textbox that the user can click to 1)clear the textbox, 2)update the temporary variable, and 3)update the table. It works fine, but it seems like there should be a better way. Help?
Please let me know if you need any other information or if I should otherwise re-word this. I come here all the time for answers, but this is the first time I've actually submitted my own question. Thank you!
(I edited this to make the code show up correctly...didn't leave a line before.)
I figured out that the unbound textboxes were reverting to their default value when a user tried to make them blank. To stop them from doing this, I put this in the AfterUpdate of the textboxes:
Call ResetDefault(Me.textbox1.Text, Me.textbox1)
And defined the function:
Public Sub ResetDefault(ByVal strChange As String, ByRef txtCurrent As TextBox)
If Nz(strChange, "") = "" Then txtCurrent.DefaultValue = ""
End Sub
That is, if a user cleared a textbox, the default value of that textbox was set to "", allowing the textbox to actually go blank.
I think the best approach is to scrap the after_update event on your text boxes and instead either put a save button on your unbound form so that all updates are written in one pass, or capture the forms closing event and update your table then.
This is the advantage of using unbound forms, you do not have to comit data until you are absolutely ready to, but with using an after_update event you are almost mimic-ing the behaviour of a bound form.

Forms and Subforms in Access 2003

I have only one table and I would like to create a form for the users to populate the table easily. There are at least 5 fields in the table that only needs to be completed if a certain type of inspection (Fire) is selected from a drop down list and is left blank otherwise.
I would like to create a subform that will only pop up when inspection type "Fire" is selected from the drop down list in the main form. How can I do this?
I used the wizard to create the form and I am stuck because I really don't know VBA. So far, I went to the inspection type field on the form, clicked on "Properties", then clicked on "After Update", then selected the Macro that I created to open the subform when the inspection type = "Fire", but it isn't working.
What happens is the subform gets opened up no matter what type of inspection I select, then the ID number on the subform doesn't correspond to the main form (the subform ID will remain on id#1). Also, when I do input data in the subform, the information ends in the next record.
I was wondering if that is happening because I am using both the form and the subform to enter data into the same table. I hope this is a clear explanation of what I want to do.
Just to try to improve a bit on Kovags excellent proposition:
Sub InspectionType_AfterUpdate
Subform1.Visible=(InspectionType.Text="Fire")
End Sub
Sub Form_Current
InspectionType_AfterUpdate
End Sub
Just change this code to adapt your needs and add it to the Change Event of the Inspection Type textbox:
Sub InspectionType_Change
if InspectionType.Text="Fire" Then
Subform1.Visible=True
else
Subform1.Visible=False
endif
End Sub
If the fields are on the same table then I'd suggest placing the fields on the subform onto the main form. Then making them visible or not depending on the state of the combo box.
Using a subform gets a bit more complex as, among other things, you will need to save the record in the main form before opening the subform. Otherwise the subform won't be able to update the record and will give you a locking message.