ms/access vba open a query as read only

Hi
On my app, I have a subform that has a data source linked to a query. However, whenever I start the master form, it first loads the subform and, if there are no records in the query, it actually creates a record (!).
What I want is a readonly query that does not generate an (almost) empty record - its master key, linked to the main form is correct.

I have tried opening the query as "snapshot":
Set q02_exameSet = LifeLingerDB.OpenRecordset("tblExame", dbOpenSnapshot)
but it still creates a new record!
can anyone help?
João serras-pereiraAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

João serras-pereiraAuthor Commented:
Hi -
an additional note:
I found that the datasource in the form was open as "dynaset" and not as "snapshot" (all the queries in vba are "snapshot", so I changed the data source type in the subform to "snapshot".
But now I am getting the error "you can't assing a value to this object on the masterform, when I want to set up the proper query..

Please see attached...  (any ideas?)

Screen-Shot-2018-01-19-at-17.29.45.png
Dale FyeOwner, Developing Solutions LLCCommented:
change the type back to dynaset

Honestly, I tend to remove the SourceObject property of my subforms until after my main form has loaded.  Generally, in the Form_Current event, I will check to see whether the subform control contains a value in the SourceObject property, and if not, I will assign it there, something like:

Private Sub Form_Current

    if me.subFormControlName.SourceObject = "" then
        me.subformcontrolname.SourceObject = "frm_SubformName"
        me.subformcontrolname.visible = true
    end if

End sub

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
João serras-pereiraAuthor Commented:
Hi Dale -

Let me just try to find if I understood.
1. change back the data source of the subform to dynaset ; then query will be updatable but, it won't affect the result if I remove the SourceObject property of the control

2. On the main form now I ensure that ONLY after its loaded I will set up its SourceObject, by using your code

3. On the close event, I'll clean up the sourceobject setting it to Null

What I do not understand is how this prevents creating a new record in the dynaset query.

I thought that I had to really Unbound the linkage to the main form...

I'll do as you suggest and will come back  to it
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

João serras-pereiraAuthor Commented:
Hi dale
It is working (almost) perfectly now.  The only thing that is not ok is that does not refreshes the subform. How should I force it?
Dale FyeOwner, Developing Solutions LLCCommented:
in design view, add the sourceobject back to the subform control, and set the LinkMaster and LinkChild fields to the appropriate fields.  Then simply delete the SourceObject.

You might also want to consider explicitly setting the LinkMaster and LinkChild properties with syntax similar to:

    Me.sub_Datasheet.LinkChildFields = "ID"
    Me.sub_Datasheet.LinkMasterFields = "ID"

Obviously, you would need to replace "ID" with the name of the fields, in the main and subform which relate the two objects.
These lines would be inside the IF statement, and follow the line which sets the value of the SourceObject.
João serras-pereiraAuthor Commented:
Thanks!
If I can't do it right then I'll put a specific question related to this one (tomorrow as I must jump to a plane...)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.