Call previous value input into the textbox in Ms Access Form - ms-access

I have a table(table Income) with 8 fields for recording Income and Expense.
No: AutoNumber
PL: Income/Expense
Date
Staff_ID
Customer_ID
Payment_Type: Cash, Credit,...
Service
Amount
I create a form with a subform for data input.
cmb_pl, cmb_date, cmb_staffid, cmb_custid, cmb_type, cmb_service, txt_amount
Income 03-Dec-20 1 2 Credit hair_cut $15
After click button Add then this record will run into subform.
Next I input second record which few differences from first record.
cmb_pl, cmb_date, cmb_staffid, cmb_custid, cmb_type, cmb_service, txt_amount
Income 03-Dec-20 1 2 Credit nail_fix $18
In this case, if I lazy to reinput the data then I just only double click on those fields then data from previous record (first record) will come itself. And for those which not double click then I can insert the value from the combo or textbox.
So, what is your direction for making this works?
Million thanks for your wonderful support!

Use VBA in each control's BeforeUpdate or AfterUpdate event to set its DefaultValue property. Value will carry forward to each new record until user inputs a different value. No double click required.
Me.YourControlName.DefaultValue = """" & Me.YourControlName.Value & """"
This syntax is valid for Text, Number, DateTime and Boolean Datatypes.

Related

Calculated Textbox based on values from two fields in a form

I am having trouble trying to create an expression that will give me a total based on two values in two different text boxes.
I created a form that has a text box called Niin, textbox called variance, textbox called price and textbox called Total.
I also have a table called NIIN_PRICE that has a NIIN column and a price column next to the associated Niin.
The result i am seeking is:
Have the value entered in textbox Niin lookup the corresponding value in Table NIIN_PRICE WHICH will then output the price into the Price textbox.
I will then want the total textbox to be an expression by multiplying the value in variance textbox with the value in price textbox and get the total of both numbers.
Example:
I type 1234 in NIIN textbox in my form,
1234 is searched in the NIIN_PRICE table and returns the value of .25. .25 is added to the price textbox as an output.
i then type 4 in the variance textbox which will update my value in the total textbox to 1 (.25 from the price pull Multiplied by 4 from my variance input).
Is this request possible? Thank you for the help.
theForm might look like this>>
tbxNIIN tbxPrice tbxVariance tbxTotal
on tbxNIIN event tab add AfterUpdate sub
that reads the NIIN_PRICE table and copies the value to tbxPrice
and then clears tbxVariance and tbxTotal
and sets cursor to tbxVariance.
On tbxVariance event tab add AfterUpdate sub
that multiplies the two and puts the result in tbxTotal

Refreshing an Access form textbox if user deletes a record from table

This is my troublesome Access form:
The Count textbox outlined above is the textbox in question. It's control source is a field named "Audit Count" in the table "DB Audits". The idea of the textbox is to display a running count of how many audits a specific auditor has completed that day. To do this I have the textbox set up with a default value of:
=DCount("[Loan Number]","DB Audits","[Auditor] = fOSUserName() And [Audit Date] = Date()")+1
Assuming I'm using the DCount function correctly, this is supposed to count the number of [Loan Number] records entered in the "DB Audits" table by the auditor (whose name is found using fOSUserName()) on today's date. So, for example, when opening up the form at the beginning of the day the Count textbox would read 1 and when the auditor clicked Save and New it would increase to 2.
The problem I am having is something I encountered while doing some random tests of the form. If an auditor submitted an audit (let's say the 1st of the day), the form correctly displays the next count as 2. However, if the auditor were to delete the record from the underlying "DB Audits" table while the form was still open, if they were to enter a new record, the Audit Count field would display 2 even though it should be 1 (since the 1st record had been deleted).
How can I have the Count textbox refresh whenever someone deletes a record from the table while the form is still open? I tried the OnDelete event but I couldn't get it to work.
Use the form_delete event and do a textbox.requery

How to allow dayname in input string when inputting a date?

In our continuous form in Access we want to offer a combobox which will show 7 days with the dayname as part of the dropdown.
The combobox is bound to a date field. We can easily populate the combobox in the Form Load event to show the days in ddd dd/MM/yy format and existing dates are happily shown on the form like so:
However if you try and pick from the dropdown you get the error:
The value you entered isn't valid for this field.
Solutions involving an unbound combobox won't work as this is a continuous form.
It's kind of a detail, as the dayname isn't absolutely necessary, and the combo works fine without the dayname. However this is a heavy duty data entry form so optimising for the end user is important and having the daynames will assist him/her.
I recreated the problem and the solution I came up with was to have a combo box where the row source has two columns and you hide the first column using ColumnWidths and the second column is the formatted date value. So when you pick something in the combo box, the value is an unformulated date value which gives no issue.
Populate the combo like so:
Dim i As Integer
For i = 0 To 6
Dim item As Date
item = DateAdd("d", i, weekCommencingDate)
cboTimeDate.AddItem item & ";" & Format(item, "ddd dd/MM/yy")
Next i

access 2010 expression builder

I want a text box to contain data which is a calculation based on 2 other control field values - only if it's value is null (ie the current value of the column in the database is null).
So I entered =([control1]*[Control2])/1000 in the expression builder for the default value property - however the result always shows the textbox to be empty (even tho control2 and control2 contain values).
How can I achieve this? Can such an operation only be done in code-behind ie VB??
thanks,
KS
I think you're talking about a control bound to a field in the form's record source. And when the underlying field is Null, you want the control loaded with your calculated value.
If that interpretation is correct, you can do it from the form's On Current event.
If IsNull(Me.txtYourTextBox) Then
Me.txtYourTextBox = (Nz(Me.control1) * Nz(Me.Control2)) / 1000
End If
That will load the computed value into the text box, allow the user to change its value if desired, and store the value to the bound field when the record is saved.
If the bound field is not Null, its value will be displayed in the text box without alteration by the On Current code.
Is that what you want?
To accomplish this using VBA, add a Form_Load Event. (Open the form in Design View and in Form properties click the Event tab and choose Event Procedure for "On Load" and click ...)
This example uses [TextField] to refer to the table data.
Private Sub Form_Load()
TextControl.SetFocus
If IsNull([TextField]) Then
TextControl.Text = ([Control1] * [Control2]) / 1000
End If
End Sub

How to Reference Individual Form Elements in a MS Access Continuous Form

MS Access Scenario:
I am using a form (we'll call it the select_contract form) and a subform (we'll call it the employee_allocations_by_contract subform).
The select_contract form:
Is unbound.
Contains a combobox that allows the user to select a contract id.
The employee_allocations_by_contract subform:
Is a continuous form.
Is bound to a table (which I'll call the hours_allocation table) that contains the number of hours that each employee is allocated to each contract.
Binds only one field, the employee_id field, to the hours_allocation table. The row source for this field is a query that returns the ids of employees that have hours allocated to the contract that the user has selected in the select_contract form.
Contains twelve other, unbound fields, one for each month of the year. These fields are intended to display the number of hours allocated to the employee listed in the employee_id field for each month of the year.
The Problem: In order to display the number of hours by month that are allocated to each employee listed on the employee_allocations_by_contract subform, the queries for each month field need access to the employee_id field of the row on which they appear. I haven't been able to figure out how to reference this field. Because the employee_allocations_by_contract subform is a continuous form, a reference to the the employee_id field name appears to reference each row on the form.
I have searched several forums and have found related continuous form issues, but nothing that clearly addresses this problem. Any thoughts?
Well, you could build a sub-query for each of the month columns you need that total for.
Something like:
Select id, employee_ID, bla, bla, bla,
(select sum(hours) where month = 1 and year = 2010 and
employee_id = ehours.Employee_id from tblProjectHours)
as month1,
(select sum(hours) where month = 2 and year = 2010 and
employee_id = ehours.Employee_id from tblProjectHours)
as month2,
(select sum(hours) where month = 3 and year = 2010 and
employee_id = ehours.Employee_id from tblProjectHours)
as month3
etc. for each column needed
from ehours
Note I used month and year as columns and you likely have to use month([SomeDate]) = 2 and year([SomeDate]) = 2010, but you get the idea.
Also, while you can't address each row and stuff a value into that un-bound text box, you CAN in fact bind the text box to a function that returns the value/expression you need.
txtBoxMonth1 txtboxMonth2 etc.
=MyMonth(1,[employee_id]) =MyMonth(2,[Employee_id])
And, then in the form, you have a public function called
Public Function MyMonth(intMonth as interger, lngEMPID as long) as long
' code here to get total based on employee id and month
End Funciton
So, you can bind a function to those text boxes, and the current row emp ID can thus drive what each text box displays. Just remember that this function should have all of the data pre-processed, and you don't want to have to re-run and re-load the data for each function call as that will be FAR too slow. However, I have used this approach to much success. So, each row of the form only has one real column (empID), the rest of the columns are derived from text boxes bound to the public function as per above with the month being passed along with the emp id to return the value for that column.
So, above is two possible approaches I used with success.
An unbound control on a continuous form can only refer to the record that has the focus. There fore, if data is entered into an unbound control, you will see the same data for every record in the continuous form.
You cannot reference controls "individually" ("at the line level") in a MS Access continuous form.
Your only solution here is to bound these controls to an underlying recordset\recordsource where corresponding fields hold the values to be displayed.
For example, if you want to display a time period as the difference between a "date in" and a "date out", your recordset\recorsource could be something like:
select id_person,dateIn,dateOut,dateDiff(xx,dateOut, dateIn) as timeIn from ...
Then your time calculation control has to be bound to the 'timeIn' field of the recordset.
(*) please check the dateDiff arguments. I do not have any help available here ..