Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ms access me.openargs in form record source

Posted on 2015-02-01
1
Medium Priority
?
817 Views
Last Modified: 2016-02-10
Hi,

I'm have created an application using ms access 2013 and having a little trouble passing a value from one form which is to be used in the query for another form. I can capture the value and by using me.openargs on the destination form populate a text box. Me.text41 = me.openargs. this works fine but I'm not sure how I can use the value in the record source query for the form. Do I need to set up a parameter in the record source query and somehow update that with the me.openargs value using VBA or can I reference the me.openargs value directly in a form record source query or is there a better way. The query for the form is quite long but basically I just need to pass a number which will be used ad the ID in the query

Thanks
0
Comment
Question by:victoriaharry
1 Comment
 
LVL 49

Accepted Solution

by:
Dale Fye earned 2000 total points
ID: 40582461
Generally, when I want to open a form to a particular record, I use the WhereCondition argument of the OpenForm method.  This will take me to a record in another form, which I know exists.

docmd.OpenForm "formName", acNormal, [FilterName], [WhereCondition]

I generally build my WhereCondition as a separate string:

Dim strCriteria as string
strCriteria = "[ID] = " & me.txt_ID
docmd.OpenForm "formName", , , strCriteria, , acDialog

You can use the OpenArgs argument, but you will probably need more code than what you provide.  As an example, you might do something like:
Private Sub Form_Open(Cancel as Integer)

    Dim strCriteria as string

    strCriteria = "[ID] = " & me.OpenArgs

    With me.recordsetclone
        .FindFirst strCriteria
        if .nomatch then
            if msgbox("Not found, add record", vbOkCancel) = vbCancel Then
                 Cancel = true
                 Exit Sub
            else
                 docmd.GoToRecord , , acNewRec
                 me.Text41 = me.OpenArgs
            endif
       Else
            me.bookmark = .bookmark
       end if
   end with

End Sub

Open in new window

This subroutine, or something similar would open the form, attempt to locate the record where the [ID] field value is equal to the ID you passed to it.  If found, it would set the focus to that record.  If not found, it would prompt the user asking whether to add a new record.  If the user selects OK, it would go to a new record and assign the text41 control the value of the OpenArgs.  Otherwise, it would Cancel the form open event, returning control to the
docmd.openform line from the first form.
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question