Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ms access me.openargs in form record source

Posted on 2015-02-01
1
Medium Priority
?
784 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 48

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

721 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