Reference a field from Subform as a Variable in VBA - vba

I have a form [form1] with a subform [subForm1].
[subForm1] contains a data table with fields "day", "month", and "year".
In [subForm1], I have 3 textbox "dayText", "monthText", and "yearText" that each have a control source from the corresponding fields from the data table.
I am trying to write a macro that uses the "year" field value from the row that is currently selected in the subform. So if I select the first row and run the macro, I want the macro to use the value "2019".
day month year
1 1 2019
2 3 2017
This is what I have tried so far and this is the error I get. I appreciate any inputs. Thanks
Declare yearStr as String
yearStr = Forms![form1].[subForm1].Form!yearText
Error - Cant't find the field '|1' referred to in your expression.
Edit: I found that Me.CurrentRecord will give me the index of the row, but I'm not sure how to use that to get the value of a specific column.

Me![year] ended up working. Was a lot easier than I expected.

Related

Calculating the number of like records in an unbound text box on a form. in MS Access 2016

I have an unbound text box [txt_AmendmentOF] on a form to count the total number of Amendments a specific record has in the database. So the user knows which amendment they are on. Just a simple you are on Amendment 3 out of 8. i have this calculation i am using in the Control Source field of the text box [txt_AmendmentOF]:
=IIf(IsNull([txt_Amendment]),0,Count([MIPR_Number]))
there is a Field on my table called [Amendment] its text box on the form is called [txt_Amendment]. I have the calculation return a zero in [txt_AmendmentOF] if [txt_Amendment] is null. I need it to count the record number [MIPR_Number] that are the same and return the total number of Amendments count in the [txt_AmendmentOF] text box. on my table i have private key field called [ID] it is an autonumber format. The problem with the code above is it is counting all the fields that have a [MIPR_Number] and returning the total row count of that. i am not an expert here so any help would be greatly appreciated.
Update to post..
I have also tried this and got an error in the text box
=IIf(IsNull([txt_Amendment]),0,Count([MIPR_Number]=[txt_MIPR_Number]))
Counting by a condition is one of the basic dataabase tasks. To get answers from a db, you have to query the database (and that is done by createing a query).
To count same[MIPR_Number]you have to group the data (a group contains same numbers), then count.
SELECT COUNT(id) as CountOfSameNr, [MIPR_Number] FROM Table GROUP BY [MIPR_Number]
Store this query as e.g CountMIPRNumber.
Now you have two options:
Use a Dlookup to get one value
=IIf(IsNull([txt_Amendment]),0,Dlookup("CountOfSameNr","CountMIPRNumber","[MIPR_Number] = " & [MIPR_Number]))
or add the query to form recordsource (join on [MIPR_Number], "SELECT * FROM TABLE" is forms query)
SELECT CountMIPRNumber.CountOfSameNr, TABLE.* FROM TABLE LEFT JOIN CountMIPRNumber ON TABLE.[MIPR_Number] = CountMIPRNumber.[MIPR_Number]
and reference the count field
=IIf(IsNull([txt_Amendment]),0,[CountOfSameNr])

DSum Works in Field Control but Overflows error 6 in VBA

I have a combobox with about 11 user names for options.
On Update a tempTable is created from a make table query.
(The make table query uses data from a larger static table that originated from excel.) There are no blanks, no nulls, no decimals, just integers
Within the tempTable is a column for "NetWorkDays" <-Calculated in excel first.
My Static Table and tempTable were originally set to integer.
Now my tables is set to long integer.
Still I get run-time error 6 overflow.
The kicker is ....it only overflows for one of the combo box options.
Even more confusing to me is that I can enter the same =DSum into a txtBox control on the form and it gets the sum from the tempTable data just fine!?
the tempTable has only 600 rows and the NetWorkDays colunn total is actually about 44600.
From another formun I read that the Biggest Integer in VBA is 32767, which is why I switched to long....which did not help.
In Short
Either of the options below:
TOTALDAYS = Int(DSum("[NetWrkDays]", "tempTable"))
TOTALDAYS = DSum("[NetWrkDays]", "tempTable")
with:
Tables set to Integer or Long
Result:
In VBA error 6 , for one comboBox option. but the desired answer can be found from txtBox control on the form using same temptable and DSum formula?
Not sure what I am missing here. My guess is the large sum value, but thought long would handle that....also why does it work in a control and not in vba?
Thanks for any insight on why a situation would only fail in VBA?
Dim TOTALDAYS as Long should solve this issue?

Update Excel Pivot Table Fields

I compile morning reports every day using a workbook with a number of different sheets and pivot tables. So far, I have been able to somewhat automate the data retrieval process, and getting the pivot table data to refresh was very simple. What I would like to do now is get the pivot table field filters to change automatically to include the data entries for thirty days prior up to and including today's date. I have seen some things out there that let you select just one date, but I need to select a range. I have tried using a relatively recorded macro, but it gives me this
Sub Update()
Sheets("Doorline Month").Select
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveSheet.PivotTables(-1).PivotFields("Date").CurrentPage = "(All)"
With ActiveChart.PivotLayout.PivotTable.PivotFields("Date")
.PivotItems("1/14/2018").Visible = False
.PivotItems("2/15/2018").Visible = True
End With
End Sub
I believe that this would only deselect 1/14/2018 and make 2/15/18 visible. I want this code to work automatically every morning, so if I come in on 2/16/2018 it deselects 1/15/2018 and selects 2/16/2018. I have tried replacing the dates in the recorded macro with "ActiveCell" but to no avail. Any help is greatly appreciated. If it helps, the data for my pivot tables is in sheet 1 and the dates specifically are in column A. Unfortunately, the number of data entries per day is not consistent so I don't know if that complicates things or not.
Why dont you put a flag in the database/ data of origin that marks with 1 or 0 each line depending if the date is within the range you want or not? Then you can leave the 1 or the 0 selected in the pivot table's filter field and the relevant data would show automatically every time you refresh.

VBA macro for excel. Mark row if cells in column A are the same but different in column C

I do not have any code yet as I dont know VBA that much or at all.
I got excell sheet with 4 columns. Column A is main group and C is subgroups within groups from column A.
i need to mark somehow rows where within same JobID, WFID is the same and where WFID is different within same JobID.
JobID TaskID WFID
39822 913914 Complete
39822 913915 no complete
37941 905439 Complete
37941 905440 Complete
Would you be able to help pleasE?
You do not really need macros to identify your rows.
Simply put this formula in a new column
=COUNTIFS($A$1:$A$8,A1,$C$1:$C$8,"<>"&C1)
Wherever there are different values in col C for same value of col A it will throw a 1 else 0
If you want to highlight, I recommend using conditional formatting based on the value of your new column with (0/1) values.
Hope this answers your query.

Counting text-items per page in MS access

I'd like to count a text-string in every page on a report and print out the count of the strings in the page-footer.
Searching for a string in a text field is straight forward, counting the findings within the text-field too, but how is it possible to sum the findings in a integer variable per report-page when it has several entries?
i.e. I´ve got a report-page like this where each new line is a new record.
Here the first report-page:
aaaaaaF
aaaaaFF
ffaaaaaaaaa
FaaaaaFF
Now the page-footer:
There are 4 records. The letter "F" has been found 6 times on this report page.
Now the second report-page:
aaaFaaF
aaaaaF
fFaaaaaaaaa
FaaaFaFF
FFaaaaFa
page-footer:
There are 5 records. The letter "F" has been found 10 times on this report page.
I'd be happy if smdy has an advice for me.
Thanks!
First step is to figure out how many occurances of "f" occur in each record. Which you can do using
= Len([myField]) - Len(Replace([myField],"f",""))
Now for the total occurances in that page you use the Sum function in a text box in the report footer section.
= Sum( ... )
= Sum(Len([myField]) - Len(Replace([myField],"f",""))) ' if report based on a table
= Sum([myCalculatedField]) ' if you use the occurance count formula in the query instead
If you need to total across the page there is a link detailing how to go about it here (you'll have to scroll down a bit)
http://office.microsoft.com/en-us/access-help/summing-in-reports-HA001122444.aspx
You haven't shown any of the expressions that you are using but, essentially, in the Report Footer you would include a textbox which uses your aggregate function:
=COUNT([SomeField]) 'or
=SUM(iif(some condition, 1, 0))
where SomeField is a field in the detail section, or some condition refers to this field.
That is, you need to SUM (or COUNT) across the whole report by referring to field(s) in the details section. You do not do this by attempting to refer to the subtotals that you have in the page-footers - this won't work.