DoCmd acCmdSelectRecord gives me run-time error 3125 - ms-access

I'm building a relatively simple datasheet-style form for selecting a record (which will be used to populate another form)
The form pulls from a query with multiple columns. Since it's counter-intuitive to have a highlighted field and data entry cursor when all the user wants to do is select a record, I added the following code to each field on the form:
Private Sub Model_Enter()
RunCommand acCmdSelectRecord
End Sub
This works fine for the "Model" field, but for every other field it gives me the following error:
Run-time error 3125
" is not a valid name. Make sure that it does not include invalid characters or punctuation and that it is not too long.
There's literally nothing else going on in this form, so I have no idea what the issue is, or why the code works for Model and breaks for everything else. Anyone else run into this issue?

I suggest you use the On Current event, rather than write code for each control. It is also possible to use conditional formatting and a little code to highlight a record.

Related

Trouble with Access 2010 .SetFocus command syntax

Extremely puzzling:
Upon opening a simple form from another form by vba, the cursor moves to a particular field.
However, when this field is Null there is each second time Error 2110. The syntax to be used changes every time as shown below.
Even more puzzling:
Upon clicking "Debug", the error proves to be imaginary: on the corresponding code line, one can simply continue with F5 or F8 and the procedure ends correctly with the focus where desired.
I found a provisory workaround which does not generate the error message but would like if possible to avoid such limping coding:
'…
Debug.Print Me![MyTextField].Enabled ' always True
Debug.Print Me.Name ' always correct form
Me.Repaint
On Error Resume Next
[MyTextField].SetFocus ' without Me!
Me![MyTextField].SetFocus
' Forms![MyForm]![MytextField] : same result as with Me!]
' one time error with Me! but not without Me!,
' next time vice versa, and so forth…
On Error GoTo 0
'…
When [MyTextField] is not Null, both syntaxes work fine without generating an error.
What is wrong with this .SetFocus command ? "Repairing" the database didn't help.
You can't set focus to the control that has focus. It would give you a very easy way to set up a infinite loop.
You would have to set focus to another control first.
Minty's right. An easy workaround is doing it in an if statement or create a bool to see if IsNull(fieldhere) = true then exit sub or your action here. Or more simply maybe try:
if MyTextField.gotfocus = true then
do something
else
MyTextField.setfocus
I recently ran into this issue in something similar and had to create a public bool function and test it on load events. Please note that I'm a beginner in access so there's probably many better ways to complete this :)

MS-Access VBA: Passing variable from one form to another giving a run-time error

I've got a form frm_New_Datasheet to enter datasheets from volunteers into a database I'm making. For the volunteer id text box txt_Vol_ID I've got two buttons to either search for an existing volunteer or add a new one, each opening a respective form. I want the Vol_ID from frm_Volunteers to get passed back to frm_New_Datasheet once I add a new volunteer.
To do this, I added the following code to my volunteer form:
Private Sub Form_AfterInsert()
If Not IsNull(Me.vol_Vol_ID) Then
Dim volVal As Double
volVal = Me.vol_Vol_ID
Forms!frm_New_Datasheet!txt_Vol_ID.Value = volVal
End If
End Sub
And it worked perfectly. Then I tried creating my 'Search for Volunteers' form, added the same code to it, but now I get the following error on both:
Run-time error -2146500594 (800f000e):
Method 'Item' of object 'Forms' failed
The debugger is highlighting the line where I set the .Value = volVal so I've looked for other ways to call that text box but no luck yet. I've tried
Forms([frm_New_Datasheet]).[txt_Vol_ID] = volVal`
but that gives me a different error:
Run-time error '2465':
Microsoft Access can't find the field '|1' referred to in your expression
I'm pretty lost on this because I think I'm calling the other form's text box properly, and when I search for the first run-time error the best advice I've found is "Sometimes VBA is buggy and try restarting" which hasn't helped. So far this is the best idea I've had to pass the value from one form to another. If there's another place I can drop this code or another way to skin this cat I'm all ears.
Thanks!
Looks like you should check form/control names more carefully and make sure that the form frm_New_Datasheet is open.
Here is the correct variant for referring using Forms collection by form name:
Forms("frm_New_Datasheet").txt_Vol_ID = volVal
I'm not sure if this actually counts as an answer but after HansUp helped me through with some trouble shooting in the comments above I just copied the form that wasn't being found (still not sure why), pasted it with a new name, and deleted the original form. After that everything is working well.

Access SubForm recordsource revert to original

I have an Access form with two subforms, both in continuous mode. Since I cannot have a subform inside a continunous form, I had to do it that way (Datasheet is not an option either)
Either way, when I click on my first subform, I change the other subform record source using some rather simple code :
Public Sub MAJFiltre(intIdMembership As Integer)
IdMembershipFiltre = intIdMembership
Me.RecordSource = "SELECT * FROM T_PeriodeMembershipPartipant WHERE IdPeriodeMembreship=" & IdMembershipFiltre
Me.Requery
End Sub
This function is called from the first subform. I did this for another form and it was working fine. For this one, if I use a breakpoint, I can see the recordsource is changed, but nothing happen in the UI. However, if I put a breakpoint in a BeforeInsert event, I can clearly see the recordssource reverting to the original one (with no WHERE clause)
I also notice something unusual : If I save the form code while debugging, all of a sudden, it works. However, as soon as I close the form, it revert back to its "buggy" version.
Does anyway have an idea of what is going on and how I can correct /prevent it ?
Thanks
It sounds similar to a problem we suffer periodically, which relates to subforms linked to a parent form with link master/child ids: If you've (ie Access has done it for you) unintentionally saved a filter value as a property in one of the subforms, (filter by or in a record source), when your code reassigns the record source, this saved value prevents the new record source from being assigned correctly, and ms access then does what it thinks is best - in this case, reverting to the valid (prior) record source.
Check your subforms for unwanted saved values of data-tab properties.
Could the problem be that your field name is misspelled in your query criterion?
IdPeriodeMembreship
If the field in your T_PeriodeMembershipPartipant table is IdPeriodeMembERship instead of IdPeriodeMembREship your query would likely prompt you to enter the parameter value manually when it is run.
If that field does exist in your table but the value you're specifying in your criterion isn't found in that field, it will return no results and the second subform's recordsource will be set to an empty recordset.

How to check if an empty record is clicked in an Access subform?

I have a subform embedded in a main form. I have the following code for the onClick event of one field in my subform.
Private Sub my_field_Click()
If Not Me!my_field.Value Is Null Then
'my code here...
End If
End Sub
I want to update a text box with a click on the field, but I got an error when I click the field before the subform is not fully initiated (by setting the field's data source). So I wrote the above code to check if there is any value now in the field. However, I got the run-time error 424: Object required when running this code. Strangely, when I inspect the value of Me!my_field.Value in the debug window at run time, it does give NULL value.
Any idea what went wrong here? What else should I do to do this checking?
I am running this in Access 2000.
Use the IsNull() function in VBA code.
If Not IsNull(Me!my_field.Value) Then

Microsoft Access macro to handle copy-paste insertion

Seems like I've been hitting quite a number of pitfalls after starting out on Microsoft Access. Anyway, the latest one is this: my macro will auto-populate a field in my (datasheet) form whenever the record is dirty, which means that for this field everything is totally behind-the-scenes. Sounds good!
I faced a problem when trying to copy-paste over several columns from Excel though. Access didn't seem to recognise that I was "dirty-ing" more than one row, and consequently help me to auto-populate the fields of all these rows. Instead, it threw me the error message You must enter a value in the "xx.xx" field. followed by a disappointing Do you want to suppress further error messages telling you why records can't be pasted?. It seems Form_Dirty() isn't sufficient for my purposes...
Is there any solution or workaround for this? Let me know your comments and ideas, I'll be more than glad to listen :)
The Dirty event only fires once for the form:
The Dirty event occurs when the contents of a form or the text portion
of a combo box changes. It also occurs when you move from one page to
another page in a tab control.
You can programmatically set the DefaultValue for each form. It needs to be set specifically to a string, though, so requires additional quotes:
Private Sub Form_Open(Cancel As Integer)
Me.txtFirstName.DefaultValue = """Dave"""
End Sub