Assigning a default value to a Microsoft Access Table Field? - ms-access

I have a database table in Microsoft Access. I want to assign the default value of one of those fields to another field that happens earlier in the same table.
I tried the value =[Form1].[AssignedByes] with no luck and the same for =[AssignedByes] but with no luck.
Can anyone help?
The field name I'm assigning the value to is [RemainingByes] and the field that assigning the value of is [AssignedByes].

To get around this problem, I create a Query that basically functions as the table. I use it instead of the table when creating other queries, forms, etc.
I'm guessing that you copy [AssignedByes] into [RemainingByes], because [RemainingByes] will start off with the value of [AssignedByes], but you want to be able to change it later.
So, you can set up the Query with a calcualted field called [xRemaningByes]. You would enter the changed values in [RemainingByes]. Then set up a formulat in [xRemainingByes] that returns the value of [RemainingByes] if there is one, and it not, it returns the value of [AssignedByes].
[xRemainingByes]: Iif([IsNull([RemainingByes],[AssignedByes], [RemainingByes].
When you use the Query instead of the Table, you will use [xRemainingByes], which is the Query's calculated field, instead of the Table's field [RemainingByes].
ANOTHER OPTION ---
Create a field in the Table such as [RemainingByesOverride], and that would be where you hard enter the changing data. Then get rid of [RemainingByes] in the table and only have it in the Query as a calculated field.
[xRemainingByes]: Iif([IsNull([RemainingByesOverride],[AssignedByes], [RemainingByesOverride]

A field's default value is applied at the instant a new record is created. So you can't use [AssignedByes] as the default value for [RemainingByes], because [AssignedByes] doesn't have a value yet.
OTOH, if you've assigned a default value property for [AssignedByes], use that same property for [RemainingByes].
If your Access version is 2010, see whether you can use a data macro as a pseudo-trigger to accomplish what you want.
You could also try binding a form to the table. In the after update event of the control bound to [AssignedByes], you can assign a value to another control bound to [RemainingByes]. This could allow you to do what you need in the form, but won't apply for changes made outside the form.

Related

Pass a Parameter in SSRS while removing Dashes

I am passing a unique ID as parameter in SSRS report. In the source table, unique id does not contain dashed. However, the user may insert Unique ID including dashes "-" and in some cases without dashes. Is there a way that we could remove dashes from the parameter.
For example, unique id 3120-20268-8 is stored in table as 3120202688. How I could retrieve if user pass multiple values with or without dashes in the SSRS Report.
When is used below query, it gives record against single value only. However, gives error when more than one values are provided.
select * from Table
where Unique_ID in (REPLACE(#Unique_ID,'-',''))
For more than 1 values, it gives errors mentioned below:
The replace function requires 3 argument(s).
Query execution failed for dataset 'ATL_List'.
Thanks
One of the simplest mechanisms for this is to create an expression based parameter to hold the sanitised input. This parameter would be hidden so the user is not aware of it, but the rest of the usage of the parameter is the same.
NOTE: You could do something similar with a query based default value, but this case is easier to do via a simple expression
Single Value Parameter
Create a new parameter:
set it to hidden
Set the default value expression:
=Str(Parameters!inputID.Value).Replace("-","")
Multi-Value Parameter
This is only slightly trickier, in the expression we can join the selected values together into a CSV string, then process that value and then split it back:
Set the parameter to multi-value, but still hidden:
Set the default value expression:
=Join(Parameters!inputID.Value,",").Replace("-","").Split(",")
Without going to detailed, if we made the sanitised parameter temporarily visible, just to demonstrate the conversion, it should look like this:
The parameter MUST be hidden!
NOTE: DO NOT make your sanitised parameter visible as in the above screenshot in your deployed report! Doing so will mean that it will not pickup changes made to the input value after it has rendered the first time.
remember that we have exploited the default value, we haven't arbitrarily defined en expression to always execute.
The output when the parameter is hidden is calculated when the report is rendered, it's just harder to visualise the behavior in this static post:
In your DataSet query you would just use the sanitised parameter:
SELECT * FROM Table WHERE Unique_ID IN (#sanitisedMultiValue)
You should be able to use the replace function in your report to format the parameter value after it has been entered, something like the below
replace(Fields!Paramater.Value,"-","")=FieldinYourTable

MS Access Expression That Includes Dynamic Field Names

I have a crosstab query which returns results based on consumer demand for a bunch of material numbers. The material numbers become my field names in the crosstab query, and later the values from those fields are displayed in a form.
In the form, I display the value in a textbox. There are a couple of these textboxes where I need to sum the total of two or more values from these fields. Not a big deal it's a simple expression. For example (in the Control Source property): =[H123457] + [H123456].
This works well UNTIL there is no demand for a particular material number. In this case, the field doesn't show up in the crosstab query and I'm left trying to sum two fields where one doesn't exist.
I've tried IIf(IsError([H123456]), 0, [H123456]), Null expressions, Nz function, etc but cannot figure out how to dynamically solve the #Name issue that ends up populating the text box.
Essentially what I want is for a 0 value for the field that doesn't exist, so I can add it to the value where the field DOES exist - is this possible?
Regards!
June7 provided the answer in the allenbrowne.com link. Essentially, you need to add all of the possible field names to the Column Headings property in the crosstab query property window. Then it's a simple matter of adding an Nz() function to handle null values.
Thanks June7!

How Can I use a ComboBox bound to a null record in a detail section?

In a form, I am displaying rows of data in the detail section.
Around 5% of the rows might actually have a float value in one column that I need to be displayed as a combobox. The column in question results from a left join, so it may have a float, or it may be null with no existing record behind it. I don't want to have 100000 rows added to a table just to show a zero in the combobox on the outside chance that a user actually displays that particular record. Yes, I can add the rows, then delete them later, but that seems stupid. For the life of me, I cannot figure out how to get the combobox to default to a zero for each row, and still be selectable. Basically for each row, show the value if there is one, zero otherwise, and let the user select a new value if they want.
The VBA side is easy. I can totally create the row if a non-zero value is selected, and delete the row if the user changes it to 0. Ideal would be to bind a combobox to NZ(myField,0), or something similar. When I tried that, the combobox was not selectable.
Right now my combobox is selectable, allows the users to change the value, but shows a blank, not zero if the column is null.
The SQL the form is bound to:
SELECT dbo.PersonClasses.ClassID, dbo.ClassDates.ClassDate, dbo.PersonClassHours.ClassHours
FROM dbo.PersonClasses
INNER JOIN dbo.ClassDates ON dbo.PersonClasses.ClassID = dbo.ClassDates.ClassID
LEFT OUTER JOIN dbo.PersonClassHours ON dbo.People.PersonID = dbo.PersonClassHours.PersonID AND dbo.ClassDates.ClassID = dbo.PersonClassHours.ClassID AND dbo.ClassDates.ClassDate = dbo.PersonClassHours.ClassDate
I am looking to represent ClassHours as a Combobox for each record on the form with valid selections being 0,.5,1,1.5,2,2.5. To keep it simple for the users, I want 0 to be preselected. This is how it looks now:
You could set the Format property of the combobox to
#;\0
This will display a zero if the field value is Null. Otherwise the actual value.
Other properties which will also need to be set are the Row Source Type to Value List and Row Source to 0;0.5;1;1.5;2;2.5
Using a function, such as Nz(), in your query to change the value of the field, actually creates a new field, which is read-only.
In your case you need the field to be updateable, so one of your options is to play around with the Format propery, or Conditional Formatting.

MS Access ComboBox.Column - use name instead of index?

i have ComboBox controls with multiple columns as a Row Source in an Access 2007 form.
i'm currently getting the selected values out of each ComboBox item this way...
value = ComboBoxName.Column(i) 'where i is the index.
i would like to use the actual names of the columns as with Recordset's...
value = ComboBoxName.Recordset.Fields("columnname")
(please note that this does not work properly on the Recordset's of the ComboBox'es: it only works AFTER the first time the ComboBox is changed)
my questions:
can i do something to "make this work" on a ComboBox's Recordset?:
value = ComboBoxName.Recordset.Fields("columnname")
is there a method that directly gets the value of the selected record using the name ?
does the ComboBox or it's Recordset have a method i can use to get a column index by specifying a column name ?
i would like to avoid writing a function and i don't feel comfortable specifying column indices which may change in the future.
There is no simple way to do this. You can create a recordset, or you can assign values to variable for each column name (FirstCol=0).
As an aside, why would the columns change? If you are using a select statement, the combo will fail if the table is changed.
PFIELD = Me.Form.Combo6.Column(0, 0)

Access 2007 Using the ID value from a list box in VBA/SQL statement

Guys...if I want to run a button click event that takes a list box and uses the ID field that is in the listbox in a SQL statement in VB, then is it
me.MyListbox.selected
or
me.MyListbox.value
to get that value? for some reason I have tried both and neither seem to be working. .value returns an empty value, and .selected generates an error stating the argument is not valid.
thanks
justin
If the ID is the bound column and the listbox is not multiselect, you can use just the name of the listbox without any other qualifier. If the ID is not the bound column, then use the column property to get the value : MyListBox.Column(n) where n is the column number starting from zero.
For multiselect listboxes, you need to iterate through the selected items to get a list to use with SQL.
If you are using the query design window or a control on a form or report, you cannot use Me, you must either use the full reference (Forms!Formname!ControlName) or for a control on the same form, just the name of the listbox.