Access - Update a field based on other fields - sql

Is there a way for Access to update a field based on the presence of answers in other fields of the same table?
For example, if fields A,B,C,E,F all have information in them (either "yes" or "no"), then column D should be populated with an "x" ; if only A,B,E have information then column G should be populated with an "x"... etc.
Thank you!

Run an Update query. It can be triggered in VBA code based on an OnChange event of a field, or an OnDirty event of the form.
Or, you can just set the control equal to your value based on the same events. You can put code in the OnChange or AfterUpdate event of every control that automatically does that calculation, or have a button that the user would have to press to do the calculation.
It's basically:
If Nz(Len(Me.A)) > 1 and Nz(Len(Me.B)) >1 Then
Me.D = "X"
Me.G = ""
Else
Me.D = ""
Me.G = "X"
EndIf
You obviously have to add more fields, but you get the idea. Make sure you use the Nz function or it will get tripped up if the field has a NULL value in it.

Related

How to change visibility of a combo box based on another combo box value in VBA

I am using Microsoft Access at the moment.
I am trying to populate a number of combo boxes based on another combo box value. For example the first combo box is called cmb_BoxValue where it has a number of 1-20 and the remaining combo box will have the name cmb_Box1 until cmb_Box20. Once the user has selected the number of combo box required, using an AfterUpdate sub it will allow visibility = True based on the value. If the selected value is 3, cmb_Box1, cmb_Box2 and cmb_Box3 Visible = True. I managed to do it using a select case like below:
Private Sub cmb_BoxValue_AfterUpdate()
Dim Size1 As Integer
Size1 = Me.cmb_BoxValue.Value
Select Case Me.cmb_BoxValue
Case 1
Me.cmb_boxBox1 = True
etc...
But I feel like this is very repetitive and not the most ideal solution. I feel like a for loop is the ideal solution but I am not sure how to approach it. Thank you
Since comboboxes have similar names with number suffix, could dynamically build combobox name in an incrementing index loop, with a test if index is <= cmb_BoxValue input to set Visible property:
For x = 1 to 20
Me.Controls("cmb_Box" & x).Visible = x <= Me.cmb_BoxValue
Next

(Microsoft Access) How to Input multiple new records with 2 out of 5 fields from being reset

I am trying to create an input form to input new entry into my table.
Basically, I have a simple table with 5 field. DATE, PERSON, Value x, Value y, Value z.
Every time I finished input a new record, the form reset to blank. I really need the Date and Person Field to keep the previous record value so I dont have to input them again and again. Only reset Value X,Y,Z.
Please help
For the date field, use the AfterUpdate event of the form to set the DefaultValue property as a text expression of the date value:
Me![Date].DefaultValue = "#" & Format(Me![Date].Value, "yyyy\/mm\/dd hh\:nn\:ss") & "#"
Similar for the other fields.

Filter as you type combo box me.recordsource

I can upload a file if someone tells me how.
Need help replicating a filter as you type on a combo box that is being used at the record level. Example: Instead of having an open text box for the prefix (e.g. Mr. Mrs. Ms. Dr.), I'm using a combo box that looks up from a reference table. I want to be able to type the letter "r" in the combo box and have it filter out Ms. and showing the remaining values. Once I make a selection store the selected value in the Name table.
Issue: When I add a new value in Combo4 the other rows above clear out if they don't match the value I just typed into the cell. Something likely with the RowSource in the below formula. Do I have something out of sequence or a flawed formula?
What I think I'm trying to do:
1) If Prefix value populated w/ value in t_Name THEN show the matching value in t_ref_Prefix
2) If Combo4 is Blank / Null THEN then open Combo4 and show all values in t_ref_Prefix so a value can be selected.
3) If user is typing text into Combo4 THEN filter on change using * on both sides of the typed value.
Option Compare Database
Option Explicit
Private Sub Combo4_Change()
'https://stackoverflow.com/questions/48133260/display-records-in-access-db-combobox-on-any-text-typed-by-user
'test number of characters entered - if greater then 0 then assign rowsource
If Len(Me.Combo4.Text) > 0 Then
'set the rowsource to match user search criteria
Me.Combo4.RowSource = "SELECT * FROM t_ref_Prefix WHERE Prefix LIKE '*" & Me.Combo4.Text & "*'"
'show the search in real-time
Me.Combo4.Dropdown
Else
'set to no
Me.Combo4.RowSource = "SELECT t_ref_Prefix.auto, t_ref_Prefix.prefix, t_ref_Prefix.sort FROM _
t_ref_Prefix ORDER BY t_ref_Prefix.sort, t_ref_Prefix.prefix"
End If
End Sub
You need to set
Combo4.AutoExpand = False
This will do it.

Yes/No data type always displaying as True/False - Boolean formatting issues

I have several fields on a form that absolutely will not display a boolean value which is set to display as Yes/No, the dropdown menu is set to only offer Yes/No as options, but no matter what I do, the values displayed on the form always show True/False instead. Pictures below:
At this point I'm not entirely sure what it is that's causing this, but it's kind of annoying.
If you want to use a ComboBox, set the ComboBox properties as follows
In the data section:
Control Source = <name of your table or query column>
Row Source Type = Value List
Row Source = -1;Yes;0;No ' Alternating values and texts.
Bound Column = 1
Limit To List = Yes
In the format section
Column Count = 2 ' Because we have a value and a text.
Column Widths = 0cm ' This makes the first column invisible.

VBA - Based on User Input - look up value in table

I have created a form that asks for two user inputs, site location and sku. Site location is a drop down and SKU is a text box. Below it there is a textbox which I want to populate based on user input after they hit the "whats my price?" button.user form
I have a matrix of prices with the SKU in column B and the sites across the top in row 1 with their respective prices in the matrix(columns D-H). I have attached a sample of the table. Please note that the "SKU" and "Site" titles will not be in my actual matrix.
pricing table
I need assistance coding the "What's my price?" button in the user form.
I feel as though I would need an if statement using some sort of look up but i'm a little lost as to how to start the code.
Here is what you have to do:
Read the value from the site field;
Read the value from the SKU field;
Display the matching in the Your Price is field, using the following formula:
WorksheetFunction.Index(Range,site field, sku field)
More about WorksheetFunction.Index here:
https://msdn.microsoft.com/en-us/library/office/ff197581.aspx
You need to use Application.Match on the row and the column independently, then get the corresponding cell. Try the following code, but replace the control's names (txtSku, cmbSite) and the sheet's code name (mySheet) with yours.
Sub WhatsMyPrice_Click()
Dim rowNum, colNum
rowNum = Application.Match(txtSku.Value, MySheet.Range("B:B"), 0)
If IsError(rowNum) Then MsgBox "SKU not found": Exit Sub
colNum = Application.Match(cmbSite.Value, MySheet.Rows(2), 0)
If IsError(rowNum) Then MsgBox "Site not found": Exit Sub
txtPrice.Value = MySheet.Cells(rowNum, colNum).Value2
End Sub