How to set a default value in an Access 2000 combobox - ms-access

I have an Access 2000 form that contains a combobox. The combobox is bound to a field in a table. When the value in the table is null, I want to set a default value on combobox without making the record dirty. Setting defaultValue does not work unless it is a new record. When I try to set the value, I get an error "You cannot assign a value to this object".
Any thoughts?
Me.cboName.Value = Me!cboName.Value ' This causes the error mentioned above
Me.cboName.DefaultValue = Me!cboName.Value 'This does nothing on an existing record.

The DefaultValue is entered when a NEW RECORD is created. To have the value display for an existing record... the easiest way I can think to accomplish this is to usean unbound control. For example, if the field you are using is theName in the Current event you'd use code like this:
Private Sub Form_Current()
me.cboName.value = Nz(me.theName.value,defaultValue)
End Sub
Where defaultValue is your previously determined default value. This will effectively require you to have two controls for the name... One with the bound value and one with the displayed value. If you do this, you'll have to also add code to update theName, when you change cboName.
As Remou suggested, you should ask yourself if this is really what you want to do, as it is definitely at least a little messy.

Related

How to programmatically select a value in combo-box?

Good Day all. I have a quick question. I have an openargs value that I'm am trying to use to show up in my combobox (cmbMemberName) on return from another form. The combobox populate the underlining subform. I just can't seem to get the right method. I can't use recordsource cause that would filter out the rest of the records. Rem: I just want the focus on the updated record and loaded into the combobox upon return. Here's the last method I tried.
If Nz(Me.OpenArgs) <> 0 Then
Me.cmbMemberName.SetFocus
DoCmd.FindRecord Me.OpenArgs
MsgBox (Me.OpenArgs)
Me!cmbMemberName.Dropdown
Else
....
The error occurs on the DoCmd. Any suggestions. Thanks. I could load the entire sequence, but don't think that would be necessary.
First you have to set the value of the combo box. Assuming your openargs matches the bound column of the combo box that should simply be
Me.cmbMemberName = Me.OpenArgs
After that you need to get you subform to populate based on the combo box value. Assuming you have set the subform to properly read the value you just need to requery it
Me.MySubForm.requery

Access SubForm recordsource revert to original

I have an Access form with two subforms, both in continuous mode. Since I cannot have a subform inside a continunous form, I had to do it that way (Datasheet is not an option either)
Either way, when I click on my first subform, I change the other subform record source using some rather simple code :
Public Sub MAJFiltre(intIdMembership As Integer)
IdMembershipFiltre = intIdMembership
Me.RecordSource = "SELECT * FROM T_PeriodeMembershipPartipant WHERE IdPeriodeMembreship=" & IdMembershipFiltre
Me.Requery
End Sub
This function is called from the first subform. I did this for another form and it was working fine. For this one, if I use a breakpoint, I can see the recordsource is changed, but nothing happen in the UI. However, if I put a breakpoint in a BeforeInsert event, I can clearly see the recordssource reverting to the original one (with no WHERE clause)
I also notice something unusual : If I save the form code while debugging, all of a sudden, it works. However, as soon as I close the form, it revert back to its "buggy" version.
Does anyway have an idea of what is going on and how I can correct /prevent it ?
Thanks
It sounds similar to a problem we suffer periodically, which relates to subforms linked to a parent form with link master/child ids: If you've (ie Access has done it for you) unintentionally saved a filter value as a property in one of the subforms, (filter by or in a record source), when your code reassigns the record source, this saved value prevents the new record source from being assigned correctly, and ms access then does what it thinks is best - in this case, reverting to the valid (prior) record source.
Check your subforms for unwanted saved values of data-tab properties.
Could the problem be that your field name is misspelled in your query criterion?
IdPeriodeMembreship
If the field in your T_PeriodeMembershipPartipant table is IdPeriodeMembERship instead of IdPeriodeMembREship your query would likely prompt you to enter the parameter value manually when it is run.
If that field does exist in your table but the value you're specifying in your criterion isn't found in that field, it will return no results and the second subform's recordsource will be set to an empty recordset.

Access VBA restore listbox selection after requery based on a non-bound column

So, I inherited MS Access 2010 (and 2013 now) code that has a listbox bound to a column that is not unique. However, that bound value is used in code, so, I cannot change which column is bound. But, I have a subform that needs to requery the listbox on the parent form. What I am struggling with is how to restore the selection in the parent's listbox to what item was selected before the listbox was requeried.
I have tried storing the Parent.List0 value before the requery and then using Parent.List0.Selected and Parent.List0.ItemData, but these do not seem to work because, presumably, the listbox is bound to a column that is not unique.
There is a column in the listbox that is unique. The 4th column (column(3)) is unique, but I'm not finding a way to select a listbox item based on a column other than the bound column.
I am confident someone has some ideas that will sort this out, but I've not been able to locate any solutions in my search of the web.
I am in version 2010 and while testing, I was unable to replicate a situation where the list box loses its value. Neither on a bound form or an unbound form.
In any case however, it seems that column 3 has a unique key - so you can use that.
' Assuming this code runs from within the sub-form
KeyID = Me.Parent.Controls("MyListBox").Column(3)
With Me.Parent.Controls("MyListBox")
.Requery ' Or do whatever you do that causes it to requery
' Find the previously selected item and select
For i = 0 To (.ListCount - 1)
If .Column(3, i) = KeyID Then
.Selected(i) = True
Exit For
End If
Next
End With
That should do the trick.

ComboBox empty/null throws error. How to "reset" field?

I have a combobox in my form with a table-filled list as rowsource. When the user enters the field, changes something and delete it so that the combobox is empty I get the error "You tried to assign a null value to a variable that is not a variant data type".
The problem is that my combobox does not allow an empty string but i also cannot find an event in the combobox that fires before the error. So I cannot use Len or IsNull to catch the error. Currently I use this "work around" but it is non-specific as it uses the error event of the form not of the combobox:
Private Sub Form_Error(DataErr As Integer, Response As Integer)
Me.myField.Undo
Response = acDataErrContinue
End Sub
Is there a better way to handle this issue?
I found this. But that did not solve the problem.
As Remou indicated in the comments, the error is in response to what you are doing after a new value is entered into the combobox. Check the combobox events. If you don't mind about the type and want to use null values, you can change your variable to a Variant type. If you are using an actual data type such as string or integer then use the NZ() function to screen out nulls.
For example, if you want to assign an empty string to a string variable if the combobox is null then you can do:
dim s as string
s = nz(me.combobox,"")
As for firing before the error - somewhere in your code you already have the offending code that is causing the error. You need to locate that. More information will be required from you in order to provide a more detailed and specific answer.
It's an undocumented Microsoft "Feature" designed to drive you crazy. And the people who tell you "look in your code", "somewhere in your code" don't know what they are talking about.
Two workarounds. Change the underlying field in the table to allow nulls, OR, and I think this is the best way, change your combo to an unbound control and set the underlying field in the AfterUpdate event.

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.