Access ODBC Subform field size too small to edit - ms-access

I have a form with a combo box that allows a user to filter data in a subform using
DoCmd.SearchForRecord , "", acFirst, "[Division] = " & "'" & Screen.ActiveControl & "'"
The user should then add data to the subform below.
I've two problems:
When the user tries to edit this data, they get "The field is too
small to accept the amount of data you attempted to add. Try
inserting or pasting less data". It doesn't matter which of the
fields the user tries to enter data into, they all suffer this
problem.
The "Division" field that has been filtered, shows up as #Error for
the new record line. I've set a temp variable for this (see below), so it should
be showing the value used for the filter, but does so with or without the default set.
TempVars.add "Div", [Combo5].Value
I've looked in my ODBC forms and all the data types/sizes etc look to be the same.
There's data already in this form, which can be edited, even though it's the same size, but no new records can be saved. As well as this I can edit data in the supporting table.

Answer
Thanks to #AVG and #WayneG.Dunn for help. I ended up using the following code and using the filter as suggested. I couldn't get it to work very well on the subform, so I hid the field and used that as a Master Field for the subform. Still getting and error message "field too small" and "#ERROR" in the new record label, but the form works, so I'm just going to use it as is. Code for reference:
DoCmd.SetFilter "Division", "Division = " & "'" & [Combo5].Value & "'"

Related

Microsoft Access vba update date of record on button click

I have a form with combobox cmbProjectName where a project name can be selected from tblProjects. The form also has a button btnComplete.
When the button is clicked, I would like to update the record for the selected project so that field Date Modified is filled with today's date. I'm having trouble coding up the VBA to find the correct record to update.
The field I am looking to update is tblProjects.[Last Modified], and I would like to find the record by referencing the combobox cmbProjectName by using the column tblProjects.projName.
This could be handled several different ways, however, it looks like you might be trying to find a VBA SQL solution. As such, try this:
Currentdb.Execute "UPDATE tblProjects " & _
"SET tblProjects.[Last Modified] = DATE()
WHERE tblProjects.projName = " & cmbProjectName.Value & "", dbfailonerror
This would of course require that the tblProjects.projName be a unique value for the entire table. Otherwise, you will need some other sort of solution.

Access Main Form 'Enter Parameter Value'

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?

ms access openform where no data

I have a frmA, based on qryA.
Button btnOpenfrmB on frmA opens another frmB, based on qryB.
But this frmB includes some unbound textboxes, with data from qryC, i.e.
=DLookUp("Field";"[qryC]";"[ValueC] = " & [ValueB]). If qryB has no data (records), Dlookup fields return errors and opening frmB, which includes these textboxes, ends with an error. I understand this behaviour of frmB (there is no value B, that's why an error), but HOW CAN I AVOID IT and correctly open frmB for entering the first record ? When qryB has at least one record, everything works OK.
Thanx in advance
Duski.
Just use Nz function if Field don't have, for instance 0 values:
=DLookUp("Field";"[qryC]";"[ValueC] = " & Nz([ValueB],0))
or iif function if Field can contain any value:
=iif(IsNull([ValueB]),"",DLookUp("Field";"[qryC]";"[ValueC] = " & [ValueB]))

Blank subform: Unable to find a record, based off multiple fields

I've looked around for an answer to this issue, but have fond none. Thank you in advance to anyone who's able to help. I'm trying to look up records and alter them, based off multiple fields. However, my form shows up blank.
I have a database with one to many links for the following tables:
Sample->Set->Catch->Length->Diet (Key fields: SampleID, SetID, etc.)
Preliminary data is entered. I have additional data for some individuals to be entered into the Length and Diet tables. So, I created a form with combo boxes that allow the user to navigate to the correct fish by selecting: Date, Station, Set, Species, and Length. So, when I select a date, I'm restricted to stations sampled on that day and so on. I have a query string set up to restrict results to those matching the criteria entered into the combo boxes. My subform is based off the final query in this string (Query 5). It's linked on the primary key field for the length table (LengthID). All fine so far.
The issue: When I open my form and select values for each combobox, the subform remains blank. However, I can run Query 5 from the sidebar at this point and it runs successfully. I could just enter data directly into the query, but it would be less streamlined and vulnerable to human error.
I've also tried opening my subform directly from the sidebar. When I do this, Access prompts me for the Date, Station, Set, Species, and Length. Twice. The form then shows up and all are fields blank including the LengthID field, which should be filled in (since I'm looking up an existing record). I don't know why it prompts me twice, but I think that the subform isn't showing up in regular form view because the database sees the LengthID field as blank.
My combo boxes appear to navigate correctly to a given record. The query string my combo boxes and subform are based on all work when run directly. But I can't enter data into my subform, presumably because the subform can't find the correct record even though the query it's based off of can find it just fine. I've run out of troubleshooting ideas, any advice is greatly appreciated. Thanks!
If I understand correctly, you're trying to achieve a query WHERE clause utilizing combo-boxes. There are many SO questions out there on this but this should get you going. Let me know if you run into trouble and I'll help you out.
I assume you have a linked parent/subform combo. This code would be on the parent form search button:
Dim strSQL As String
strSQL = " 1=1 "
If Not IsNull(cmbComboBox1) Then
strSQL = strSQL & " AND Field1 = " & cmbComboBox1
End If
If Not IsNull(cmbComboBox2) Then
strSQL = strSQL & " AND Field2 = " & cmbComboBox2
End If
If Not IsNull(cmbComboBox3) Then
strSQL = strSQL & " AND Field3 = " & cmbComboBox3
End If
Me.Filter = strSQL
Me.FilterOn = True

LinkChildFields and LinkMasterFields being automatically set when changing recordsource

In Access Properties for a subform, I have "LinkChildFields = '' " and ""LinkMasterFields = '' " (i.e. blank for both, this is what I want: no link)
When I change the Recordsource of the subform, and requery the subform, (in VBA) they are both automatically being set to a field, in my case "CaseID" (resulting in no records being displayed)
This is my recordsource:
stSQL = "SELECT qryCasesAndCards1.CaseID, qryCasesAndCards1.CaseStatusID, qryCasesAndCards1.Status, qryCasesAndCards1.CompanyName, qryCasesAndCards1.NumberOfCards, qryCasesAndCards1.FullName, qryCasesAndCards1.CaseCreatedDate, qryCasesAndCards1.CaseClosedDate, qryCasesAndCards1.CreatedBy" & _
" FROM qryCasesAndCards1 where not caseID = " & Me.CaseID & " and cardnumber in (select qryCasesAndCards1.cardnumber from qrycasesandcards1 where qryCasesAndCards1.caseID = " & Me.CaseID & ")"
I've tried substituting a simpler query "select * from qryCases1" and the problem still occurs.
In VBA, right after the requery, I am debug.print'ing ".linkchildfields" and ".linkmasterfields" and this is how I can see they are both being automatically set to "CaseID"
I have tried changing both values in Access form properties to nonsense value, resaving, changing again, resaving etc, and still no joy.
I can workaround problem by setting both those values right after the recordsource runs, but there is an unacceptable delay when doing this (about 5 seconds for each value)
One thing I'm just wondering now, is wether my form filter is being propagated to the subform? (am opening it via "docmd.open ... caseid = x" )
CaseID might have at ONE stage some long time ago been entered in to those link fields... but it's definitely not now. It's like they're locked in an Access vault somewhere and it's thinking "golly gee i'm pretty sure he wants CaseID in there gee whiz I'll get right on that!"
I've found MSAccess: Change in subform recordsource causes loss of LinkChildFields binding and I've found Linking SubReports Without LinkChild/LinkMaster but I can't get any help from them.
Thanks for any help offered: I'm tearing my hair out :)
To keep Access from automatically updating the LinkChildFields and LinkMasterFields on a subform...
Ensure that the RecordSource queries do not have matching key fieldnames.
Ensure that the Link Child Fields and Link Master Fields are blank on the SubForm object's Property Sheet.
DO NOT set the LinkChildFields and LinkMasterFields properties in code (i.e. remove any previous code like MySubForm.LinkChildFields = ""), otherwise the negative side effects may not be resolved.
If the form and/or subform are bound directly to tables, simply update one of them to query the table and add an alias for one of the key fields. For existing queries, it is possible to add an alias for one of the key field names without negative consequences in probably most cases. In my case, I only had to add "AS Acct" to a single field in the SELECT part of the subform's RecordSource SQL--no other change was necessary in the FROM, WHERE or ORDER BY clauses even though the original [Account] fieldname is referenced in each of those clauses.
SELECT Actions.[Account] As Acct, ...
Of course, this required an update to the ControlSource property for the relevant control on the form.
These changes eliminated the extra few-second lag when changing both the parent record and when switching records on the subform! Likewise, it eliminated redundant events during record navigation!
I found no other setting that keeps Access from automatically re-binding the subform via the LinkChildFields and LinkMasterFields properties. As already pointed out in the comments, it also doesn't help by resetting the link fields (e.g. LinkChildFields = "") after setting RecordSource, primarily because Access takes time to reset the fields, much of that time spent in events fired both upon the automatic assignment of the link fields and again when they are reset to blank. (e.g., Form_Current is called at least twice for each new record navigation.)
Years passed... as even on Microsoft Access 2019, changing subform .RecordSource always alters original .LinkChildFields and .LinkMasterFields, reassigning good fields will cost us 5s time for each property, we have no way to avoid this.
As a workarround, we can use always the same query, for example, qrySelTmp, as .RecordSource for the subform, at the design time
Mainform.subform.Form.RecordSource = "qrySelTmp"
When we must change .RecordSource content, we change only the definition of the Query, like this
CurrentDb.QueryDefs("qrySelTmp").SQL = "SELECT qryCasesAndCards1.CaseID, qryCasesAndCards1.CaseStatusID, qryCasesAndCards1.Status, qryCasesAndCards1.CompanyName, qryCasesAndCards1.NumberOfCards, qryCasesAndCards1.FullName, qryCasesAndCards1.CaseCreatedDate, qryCasesAndCards1.CaseClosedDate, qryCasesAndCards1.CreatedBy" & _
" FROM qryCasesAndCards1 where not caseID = " & Me.CaseID & " and cardnumber in (select qryCasesAndCards1.cardnumber from qrycasesandcards1 where qryCasesAndCards1.caseID = " & Me.CaseID & ")"
If(Mainform.subform.Form.RecordSource = "qrySelTmp") then
Mainform.subform.Form.Requery
else
Mainform.subform.Form.RecordSource = "qrySelTmp"
End if
As shown in the code, we do only a subform .Requery instead of assigning a new .RecordSource.
As Subform.Form.Requery does not alter subform properties .LinkChildFields and .LinkMasterFields, we gain about 8s for each real change of .RecordSource.
In my case with 1 Customer - Many Orders diagram, I've a gain of time at the proportion of 2s over 20s.
What I done in the past is simply REMOVE the link child/master settings.
Then in code I SET the child critera. So in the following, the link master/child SHOULD filter by tour_id, but I simply INCLUDED the criteria in the SQL and then HAMMER OUT the settings.
Dim strSql As String
' load sub-form to this tour....
strSql = "select * from qryGuidesForTour2 where Tour_id = " & Me!ID
Me.subGuides.Form.RecordSource = strSql
Me.subGuides.LinkChildFields = ""
Me.subGuides.LinkMasterFields = ""
The above was a fix and fixed any performance issues. I suppose it really depends on "how" the main form record is set, but if you are setting up the "main" form record via code, then as above simply stuff in the child forms recordset directly with the critera you need. And while in above I did blank out the master/child settings, if you ARE going to allow adding of reocrds in the sub form, then likely in above you should set the master/child fields as opposed blanking them out.