Solved

ms access me.openargs in form record source

Posted on 2015-02-01
1
631 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 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 500 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

705 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now