Good morning, I am developing an access database for use in Nepali schools.
I have a form (the main form) with a subform (frmStudsTakingTTsv2) and 12 textboxes on the subform txtSubA_(i).
I am unable to make a textbox perform a Refresh in VBa . This follows a series of action queries that are working well, these are in an AfterUpdate event on a combo box in the main form.
The text boxes are filled from expression builder and so need to be requeryed. The code in there is identical but textboxes on the main form change their contents – so each text box holds a different result.
What I have tried:
A -
frmStudsTakingTTsv2. & "txtSubA_trim(str(i)).Requery
the compiler does not like the &
B – using a string
Where = "Form.frmStudsTakingTTsv2.txtSubA_" & Trim(Str(i))
Me(Where).Requery
Produces run time error 2465 Can not find the field referred to in the expression
C – Then tried this
Dim crtlWhere As Control
Set crtlWhere = frmStudsTakingTTsv2.txtsubA_ & (i)
Me(crtlWhere).Requery
On Load event – method or member not found
D – Next
Set crtlWhere = frmStudsTakingTTsv2
Location = ".txtSubA_" & Trim(Str(i)) – this is fine
Me(crtlWhere & Location).Requery
Error 450 wrong number of arguments
E – Next
Me.Controls(crtlWhere & Location).Requery
Error 450 wrong number of arguments
Just to add this site has been v useful, offering solutions over the years.
Try with:
Me!frmStudsTakingTTsv2.Form.Controls("txtsubA_" & Cstr(i)).Requery
where frmStudsTakingTTsv2 is the name of the subform control.
Related
I have been trying to incorporate a built in macro action (SearchForRecord) in MS Access, however cannot get it to work for the life of me. There is minimal resources available online for this operation, and I've noticed that other people have struggled with the same issue.
I made a test database just to see if I could get it to work in the most basic form. I made a table with 3 columns (ID, Name, Colour) - I turned the table into a tabular form using the Form Wizard. I created a text box with a search button.
I then made a macro operation:
SearchForRecord
Object Type: Form
Object Name (Name of the Form) "frmNewSearch"
Record: First
Where Condition: ="txtIDSearch = '" & [Forms]![frmNewSearch]![txtSearchBox] & "'"
I took the where condition syntax directly from https://learn.microsoft.com/en-us/office/client-developer/access/desktop-database-reference/searchforrecord-macro-action
I set the button click event to the Macro that I made.
In theory, I enter the ID into the txtSearchBox and it should bring up the appropriate record within the same frmNewSearch form.
When I try this, nothing happens and it just sits on the first record. I am using MS Access 2016 - is the macro action maybe just not supported in this version?
If there is another way at approaching this it would be much appreciated!
Cheers
Referenced article states:
Note the equal sign (=) at the beginning of the expression, and the
use of single quotation marks (') on either side of the text box
reference:
="Description = '" & Forms![frmCategories]![txtDescription] & "'"
Have to actually type = sign into argument. (Yes, I hear your ranting and cursing, but that's life.)
I presume txtIDSearch is name of textbox. The criteria must use name of field, which you say is ID. If ID is number type, don't use apostrophe delimiters (apostrophe delimiters are used for text fields, # delimiter for date/time, nothing for number type). So result will show like:
Where Condition: = ="ID = " & [Forms]![frmNewSearch]![txtSearchBox]
or since code and controls are on same form, simply:
Where Condition: = ="ID = " & [txtSearchBox]
However, both fail if form is a subform. This is because form is not open independently in Forms collection. A reference incorporating parent form name fails as well. Use VBA code methods.
Sorry, I’m pretty new at Access so I might not be using some terms correctly (or might not know some terms at all).
I’m encountering an ‘Enter Parameter Value’ error when I put my subform1 into my mainform. On subform1 I have a button that runs query1, query2, query3. These 3 queries query tables and also calculated fields that are located on subform1. Subform1’s data source is query4. When I press the button (with 3 queries), everything works.
Once I place that subform1 onto my mainform (so that my user can press the button to run the queries without entering the subform1) I receive an ‘Enter Parameter Value’ error. Query1, query2, query3 are unable to find those calculated fields located on subform1. For example the ‘enter parameter value’ error is as follows: Forms!subform1!calculatedfield1. I’ve tried changing the ‘location’ to things like: Me.subform1!calculatedfield or Form!mainform1!subform1!calculatedfield but I still receive that same parameter error. I could move the calculated fields to mainform1, which makes the queries work fine. But I would like to keep all of the calculated fields on the subform. Does anyone have any suggestions?
I always name subform container control different from the object it holds, such as ctrDetails. Then this works for me:
Forms!Main!ctrDetails!fieldname
However, my preference would be to run SQL statement in VBA. So code behind button could be like:
CurrentDb.Execute "UPDATE table1 SET table1field = " & Me.subform1calculatedfield & _
" WHERE table1field Between " & Me.subform1calculatedfield & " And " & me.subform1calculatedfield2
Why do you need to save calculated data?
I am trying to get the Dlookup function to work in Access 2013, but i just cannot get it to work, heres what i have so far :-
I have a query called qry_VehicleOverview in this query there are Two fields called VehicleNumber and DateLastExam
I have a form, there are a number of fields, two of them called Vehicle1 and DateLastExamV1, in DateLastExam1 ! am trying to reference the relevant exam based on Vehicle1 from qry_VehicleOverview field, so when a Vehicle Number is added to Vehicle1 it displays the correct Exam in DateLastExam1
first of all I create a combo box in the form, called Vehicle1 and referenced it to VehicleNumber from qry_VehicleOverview
then i created a text box in the form, and called it LastExamVehicle1, in the control source of this field I added the DLookup function :-
=DLookup("[DateLastExam]","qry_VehicleOverview","[VehicleNumber]=""" & [Vehicle1].[Text] & """")
Then chose After update in Event tab and Selected code Builder in here I added :-
Private Sub LastExamVehicle1_AfterUpdate()
Me.LastExamVehicle1.Requery
End Sub
but when run the form, first of all i get an error of #Type in the field, when i change the value in Vehicle1 the eror then changes to #Error
I create a combo box in the form, called Vehicle1 and referenced it to
VehicleNumber from qry_VehicleOverview
If that is so, there is no need for DLookup as you already have the value.
Set the RowSource of Vehicle1 to qry_VehicleOverview and the count of fields for the combobox to 2 and use this ControlSource for your textbox:
=[Vehicle1].[Column](1)
It will automatically update.
Don't use [Vehicle1].[Text], use [Vehicle1].[Value] instead. Or just [Vehicle1] .
(.Value is the default property)
.Text is only valid while the focus is in the control, and in AfterUpdate it isn't anymore.
.Text is mainly useful in the Change event, but that wouldn't make sense for your case.
Edit
You probably need the "full path" to the control in DLookup. For better readability I suggest using single quotes.
=DLookup("[DateLastExam]","qry_VehicleOverview","[VehicleNumber]='" & Forms!yourForm![Vehicle1] & "'")
Also please double-check all names in your form (and then in your question). E.g. from your description, your event procedure should read:
Private Sub Vehicle1_AfterUpdate()
Me.DateLastExamV1.Requery
End Sub
I am developing an Access database (using Office 2016) and have several tab controls which I want to display the number of records in the subform/subreport.
After a lot of searching etc I have it working for the subforms using a function which I call in the main forms current event (but in a seperate function so I can also call via a macro when I change the main forms record with a combo box, as it wasn't updating otherwise). The code I am using is:
Function ClientTotals()
Dim i As Integer
i = Form_sbfrm_ClientContacts.Recordset.RecordCount
Form_frm_Clients.ClientTabs.Pages("Contacts").Caption = "Contacts (" & i & ")"
End Function
This works perfectly for me and my tab name becomes "Contacts (No. of records)" but I can't get the syntax right to change this to work for a report, is it possible?
I have tried:
Function ClientTotals()
Dim i As Integer
i = Form_sbfrm_ClientContacts.Recordset.RecordCount
Form_frm_Clients.ClientTabs.Pages("Contacts").Caption = "Contacts (" & i & ")"
Dim j As Integer
j = Report_rpt_CurrentProjects.Recordset.RecordCount ' this line is highlighted with the debugger
Form_frm_Clients.ClientTabs.Pages("Current Projects").Caption = "Current Projects (" & j & ")"
End Function
As well as:
Dim j As Integer
j = rpt_CurrentProjects.Report.Recordset.RecordCount ' this line is highlighted with the debugger
Form_frm_Clients.ClientTabs.Pages("Current Projects").Caption = "Current Projects (" & j & ")"
and various others.
Another question I have is why is the syntax for the form "Form_sbfrm" etc and not using a "!". If I change to "!" it bugs out.
Thanks for your help, KAL
Thanks Delecron,
I think I will stick with the tabs for now as they are giving me exactly what I want, but remember what you have said for when I make future improvements if its a better way of doing it.
EDIT
Using what you have said I changed my VBA to a DCOUNT method:
Dim j As Integer
j = DCount("*", "qry_CurrentProjects", "FK_Project_Client_ID = Forms!Navigation!Navigationsubform.form!Client_ID")
Form_frm_Clients.ClientTabs.Pages("Current Projects").Caption = "Current Projects (" & j & ")"
This means my report tabs are now also working just how I wanted
I was getting in a muddle with the criteria/filter, hense the edit.
If Recordset is an old method I am assuming it would be best to replace my other code with the Dcount method?
Thanks again, KAL
Further EDIT
After doing this I could see that everytime the form was changed there was a slight flicker. Not bad but you could see there was a lot of calculation going on. Therefore I have changed my method to the following, and posted here for anyone looking at this in the future.
In the form footer a textbox with COUNT([Project_ID])
In my function
Dim j As Integer
j = Form_frm_Clients!rpt_CurrentProjects.Report!txt_CurrentProjectsCount.Value
Form_frm_Clients.ClientTabs.Pages("Current Projects").Caption = "Current Projects (" & j & ")"
Now I can see it is working quicker with no flicker.
Recordset if you need to return complex data, if you need one value, a total or a sum, Domain functions are the way to go. Don't overdue them though, having too many on a form or a report can start to bog down usability.
Glad I can help.
Recordset.Recordcount is a legacy feature that only worked in ADP files (Access front end's to a SQL database). Those are no longer supported.
If the report is based on 1 client only and there is no grouping then you can do this:
Click on the detail section and in Events create an event for On Paint. In there set
(Name of Page).Caption = DCount("*", "NAME OF QUERY/TABLE") or
(Name of Page).Caption = DCount("*", "NAME OF QUERY/TABLE", "Filter Expression") (Filter expression optional).
If the report is grouped where it will show a different page per client or date range or any other grouping this will not work since the Caption field is not data bound. You would have to add logic to the Dcount statement above to field by the current filter condition.
For example, say you have a database of 200 clients and you're running one big report on all of them, each page will get its own tab control per client, the syntax would be
(Name of Page).Caption = DCount("*", "ClientContacts, "ClientID = " & ClientID)
The better way to do it especially if you are grouping is get rid of the tab control and use databound controls. You could create a box around the information that would be in the tab page and put a textbox where the tab would go. Create a group header for however you are grouping the data. Create another invisible textbox in the group header and set the controlsource = Count([fieldname]) where fieldname is whatever you are grouping the data by (the inside brackets count).
Then in the textbox you created to simulate the tab, set the controlsource to the name of the invisible textbox.
Let me know if this helps.
Private Sub Form_Open(Cancel As Integer)
Me.Bathroom = Forms!frmBathrooms!ID
Me.txtBathInfo.Caption = "Bathroom Room Number: " &
DLookup("Room", "tblRooms", "ID = " &
DLookup("Room", "tblBathrooms", "ID = " & Me.Bathroom))
Me.RecordSource = "SELECT * FROM tblStalls WHERE Bathroom = " & Me.Bathroom
Me.Recordset.AddNew
End Sub
where Line 2 Me.Bathroom = Forms!frmBathrooms!ID is tripping the error.
Debugger says Me.Bathroom = 1, Forms!frmBathrooms!ID = 38. Basically I'm trying to automatically make this form's Bathroom field match that of the frmBathrooms form's ID. "Me" is a form for items in the bathroom; there can be many items.
How the hell is that throwing an error? I can't find anything dispite about an hour of searching around. I understand the message, but not how this could be throwing it?
The Bathroom field of the Item table is in the form's recordsource, etc. Ideas?
Had the same problem, but it had nought to do with VBA syntax.
I attempted to set a control (ordernumber) on a form.
This ordernumber had to be calculated, which i proceeded to do in VBA.
After calculation I would try to set the control (ordernumber) on the form. And that triggered an 2448 error at runtime.
Subsequently I discovered that in form design, i had set the control source to a calculation already. So run-time VBA would not allow me to set the control as well.
After removal of the calculation in the Control Source of the control in the form design --> Property Sheet window it worked fine.
I admit, really stupid mistake, but there you have it.
Hope this helps
--X--
For anyone else having the same problem, all I did was literally move lines 2 and 3 down to below Me.Recordset.AddNew (so that it changes source and adds a new record BEFORE changing the Me.Bathroom and caption). – user1394455
The main form has to be editable in order to change the drop down list, even in the form header area. (VBA was making it non-editable upon loading.)