Access assigning Null / Default value - vba

I have a form in Access with several comboboxes.
If the user selects X in combobox A a value is assigned to another field (Field A). This is working fine now (through VBA code in the on change event).
However, if the user selects Y in combobox A I would like the value in Field A to return its default value (empty/null).
Currently I have it returning to 0, but that is not ideal. What can I do to make it return to default/null/empty?
I tried:
Field A = Null
But then I get the following message:
You tried to assign the Null value to a variable that is not a Variant data type. (Error 3162)
When I make it Field A = 0 then it is working, but as I said before that is not ideal for my situation.
Thanks in advance.

Related

Select combobox intem after bounded with SQL Query in Access

I have the following pair of combobox that are used for two inserts in two different access tables from a single form.
The problem I have is that I am not able to make anything else load the form is selected both in Name_OT and in Year the first value that contains corresponding combobox.
I think the solution is with:
Combobox1.Selected (0) = True 'First value
But the combobox goes blank, no text or anything appears.
Solved with this
Cuadro_combinado79 = Cuadro_combinado79.ItemData(0)
Cuadro_combinado85 = Cuadro_combinado85.ItemData(0)

How can I assign a value to an Access Combobox?

I have a form (FrmAddBookReviews) that is opened when certain events in another form (FrmAddArticles) occur.
I want to initialize the FrmAddBookReviews with some values from FrmAddArticles. It works fine except for the Author of the book review who I have in 2 comboboxes, where I want to write the first name in one and the last name in the other combobox. I do not want to use an unbound textbox, because I want the value of the primary key of the reviewer to appear in the table belonging to FrmAddBookReviews.
If I do (rstauth.Fields(0) is the primary key to TabAuth)
Me!cmbFnam.SetFocus
Me!cmbFnam.Selected(1) = True
Me.cmbFnam.Column(1) = rstauth.Fields(1).Value
I get run time error 424: Object required
I have tried a DLookup to get the name followed by
Me.cmbFnam.Column(1) = strFnam
gives "the value does not match this field", in spite of the fact that column 1 of the combobox is text (but the control-source is the id of the reviewer). the same happens when I do
Me.cmbFnam.Column(1) = rstauth.Fields(1).Value
I have also tried to give the row-number in the Columns-clause. This gives me the "object required" error.
Can I get any help?
Biorn Veiro

How to use the value not displayed by a dropdown when connected to access database

My program uses a database in access which consists of BreedID and BreedName. I have a dropdown box in vb.net which shows the name for the user to select, however in my code I would like to store the ID related to that name so that I can send it over to the access database using oledb. Currently this is what it looks like:
Dim BrVar As String = Breed.SelectedItem.Text
But that isn't working! Please help!
You can add hidden columns to your dropdown box, it may already exist. The first column in a dropdown box is column(0) and you can set the width to 0cm. This can be for the ID value. Leaving column(1) for the Name value.
Then you can use Breed.SelectedItem.column(0)
The first thing to do is on the Data tab set up your rowsource to SELECT both the BreedID and BreedName fields (in that order). Then make sure bound column is set to 1.
Then on the Format tab set Column Count to 2 and Column Widths to 0;1.
That will have the result of displaying the BreedName field but using the BreedID field as the value of the combo box.
Use Dim BrVar As Long = Breed.SelectedItem to get the value of BreedID.

Access Textboxes Adding Values

I have an access database, with a form, that contains numerous textboxes. The textboxes are filled with currency data based on a selection made by the user. Each record may have different values some may have no values. I need to add 3 of the txtboxes together but the value always shows as Null.
me.txt1 = Nz(me.txt2.value + me.txt3.value + me.txt3.value)
txt2 = 23.04
txt3 = Null(empty)
txt4 = 15.64
The value of txt1 should be 38.68 instead its coming out Null. The textboxes are populated by a subroutine that is called from the AfterUpdate event of a dropdown.
Where do you use that formula ? Is it in VBA ? You don't need it.
If it's from the Control Source of txt1, then Meshould be replaced by Form (or by nothing at all)
In the Control Source property of txt1, type = nz(txt2)+nz(txt3)+nz(txt4)
That should be all you need. No VBA.
Another solution, IF the source of the form is a Query, is to add a calculated field in the query.
In both cases, of course you will NOT store that data (which would violate 3rd Normal form)

Conditional default values in MS Access

I'm using MS Access 2007. I have a form with several phone number fields (cell, home, work, primary*). The data source for all of these fields comes from a Customers table.
Basically, what I am trying to do is establish a set of default values for the primary* field. I would like to accomplish something like this:
If [cell] <> Null:
primary* = [cell]
elif [cell] Is Null and [home] <> Null:
primary* = [home]
elif [cell] Is Null and [home] is Null and [work] <> Null:
primary* = [work]
I know this isn't very logical code, but I hope you can see what I'm trying to do here. I just want the primary* field to default to the cell number if there is one, then the home number if there is no cell, and finally the work number if it's the only one available.
Is there a way to do this with the Properties panel in Access?
The default value property for a data control is evaluated/applied at the instant a new record is started. It's probably obvious that happens before the new record has been INSERTed into the table. Perhaps less obviously, it happens before the user has entered any data into the new record. So even if it were possible to have the default value based on another field value ... the other fields would be Null at that point, unless they also had a default value assigned.
In a different situation you might consider the form's before insert event to assign whichever control value you want to the target control. However, that's not such a great fit if you want the user to change the default primary value before the record is saved.
Think I might fall back to using the after update events on the individual phone number controls. So for [cell] after update, you could use:
If IsNull(Me.primary) Then
Me.primary = Me.cell
End If
And similar for the others.
After the user or the code has assigned a value to Me.primary, it will be left unchanged unless the user changes it.
If you don't mind your control being read only, you could simply use a expression as your source control. Something like this :
=iif(IsNull(Cell);iif(IsNull(Home);Work;Home);Cell)
If you want the control to be editable, you'll need to dynamically change the control source in the code behind on the onActivate event I guess (I haven't tested that).