Conditional default values in MS Access - sql

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).

Related

access a specific field within a query that is part of a subform

I am trying to get an understanding of what the syntax would be to access a specific field within a query that makes up a subform within another form.
Form > SubForm/Query > Field
The field I am trying to get access to a field called Berthed that operates as a Boolean field. Either the vessel is present or it is absent.
This is what I was trying and it does not work.
Me.Boat_Move_Prior_Week.SourceObject.berthed
In this case here is the breakdown
Me = current form
Boat_Move_Prior_Week = query
Berthed = field I am trying to access through the VBA
My final goal will be to create an If statement, that upon requery of the Form, will look at the Berthed field, and if the field is checked (vessel is present) do nothing, but if the field is unchecked (vessel is not present) then remove the vessel from the query.
To reach the field (on the subform) bound to the field berthed, this is the syntax (assuming the textbox on the subform also is named berthed):
IsBerthed = Me!NameOfSubformControl.Form!berthed.Value
Here an amazing guide to know how to deal with controls in main form and subforms:
Link
Aside from my syntax issues I should have been using False for the value. Thanks to the two posters above for getting me in the right direction.
If Me.Boat_Move_Prior_Week.Form!BERTHED.value = False Then
Me.Boat_Move_Prior_Week.SourceObject = ""
End If

PowerApps formula for matching records from a drop down

I'm trying to build a login form that performs a simple check against the user's PIN. The pin in stored in the database as a field in the Person entity (table).
I have a Team entity that allows the Person to select the Team they are on from a dropdown list of all Teams. The Person entity dropdown then populates based on the Person's TeamID. The User selects their Name and enters a PIN number in the txtPinNumber textbox.
The Login button is hidden by default and should only have visible = true when the pin entered into txtPinNumber is the same as the PIN field in the database.
I have the visible property of the btnLogin button set to
If(txtPinNumber.Text=Filter(Personnel,ddResponder.Selected.PIN),true,false)
However, I'm getting an error of unspecified type. Is it because I'm trying to check the entered PIN against a filtered datatype?
Any ideas on how to fix this error?
It looks like you're having difficulty because of your usage of the Filter() function. Filter() returns a table with all of the rows that meet a given criteria. However, it seems that ddResponder.Selected.Pin is the PIN you are seeking to match. If this is the case, you can simply have the visible property set to:
txtPinNumber.Text = ddResponder.Selected.Pin
If, however you need to do a search in your table to find the relevant PIN, you will instead want to use the Lookup() function.
You can see the documentation for these functions here:
https://powerapps.microsoft.com/en-us/tutorials/function-filter-lookup/
The Lookup() function will return the first matching row based on the criteria you provide, otherwise it will return a blank. You can use this with the IsBlank() function to determine whether or not to display the button.

Access and VBA: change report text from report code

in my Access application I have a form and a report. In the form I have my textBoxForm, in the report I have my textBoxReport. Normally, in the textBoxReport I see the textBoxForm value. How can I set dynamically from the report VBA-code the value of the textReport? For example, I want that, if the value in the textForm is 1, the value in the textReport is "ok".
I have already searched a solution in internet, but in all cases I see always an error similar to 'impossible set this value for this element'.
Thank you!
Does your report use a query? If so, you would just place an 'IIF' in the query that tests for the value you want to change, then either changes it to something else, or retains the original value. The below will test field 'Nbr1' for the presence of a 1, and if found, change it to 'OK', otherwise it stays the same.
Note! You will need to change the control source in the report to reflect the name you provide (i.e. 'MyChange') because you can't keep the original name.
SELECT Table1.ID, Table1.EMPID, Table1.TestResult,
IIf([Nbr1]=1,"OK",[Nbr1]) AS MyChange, Table1.Nbr2
FROM Table1;

Get value in textbox depending on combobox

I have searched high and low for an answer to this. Lots of places come close to what I need but try as I might I can't find exactly what I need. So, here goes. I have a combo box on a user form that reads values from a table. when a user makes a selection, depending on how far down the table the selection falls, I would like a textbox to display one of two strings. I'm currently using a toggle button which seems a bit 'clunky. I'm fairly new to vba so be gentle with me.
Me.textbox.Value = Me.combobox.Value
will give you the value of the bounded column of the selected row, usually the first one (e.g. if your combobox shows data like ID;LastName;PreName, it shows the ID)
If you want to show other colums than the bounded one, use
Me.textbox.Value = Me.combobox.Column(n)
(n stands for the column, beginning at 0 for the first, 1 for the second, ...)
So if you want to show the LastName of the previous example, go with
Me.textbox.Value = Me.combobox.Column(1)
If you just want to show the string when it doesn't fit in the combobox column, I'd make it like
If Len(Nz(Me.combobox.Column(1))) > n Then
Me.textbox.Value = Me.combobox.Column(1)
Else
Me.textbox.Value = Null
End If
There might be another solution, but this would be an easy one

Getting value of previous selected item (dropDown)

When a user changes the selected item on a dropdown I need to get the PREVIOUS item selected,
EX:
dropdown items:
1) Questions
2) Jobs
3) Tags
4) Badges
User has #2 Selected and then changes to #4 -- How can I get the value of #2 when they change the selection?
Declare an instance variable in your form (WinForms) or window (WPF).
When a user selects an item:
Do what you want to do.
Save the current item index in the instance variable.
In step 1, you can now access the instance variable to get the previously selected item.
Declare a global variable that will contain the previous value.
When the user changes the selection in the combobox, set the variable to the currently selected value. Allow the selection to be changed. You now will have the previous value.
If you need to have the history of changes, then the global variable would be a collection. Then on changed event, add the current selection to the collection.
If your control is bound to data, there is no need to "Squirrel" the old value away, your data provider usually does this for you.
For example, if you are bound to a DataRow, this code will get the previous value.
? = [Your DataRow].item("[Your column name]",OrigialVersion)
This varies based on your data but ultimately, you could always re-query the database to get the original value as well.
Regardless of what you are bound too, if you ask the datasource for it's value during the Validating event of the control, it will have not changed yet so it will give you the old value, which you can then compare against the current selection.
Lastly, if you are not bound to data, I typically store the old value in the TAG property on the GotFocus event of the control. Then you can compare against that.
Hope this provides some other options that might help you, depending on your case.