Results 1 to 6 of 6
  1. #1
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 7 64bit Access 2003
    Join Date
    Feb 2011
    Posts
    1,919

    Setting variables in a form from a subordinate form (NOT a subform)

    I can't seem to find the correct syntax where I have a subordinate form wanting to set a variable Dim'd in the form that opened it, i.e., DoCmd.Openform MyForm.

    Forms!frmRecipesMain!Form.strRecipientName = Me.ContributorName

    strRecipientName is the variable Dim'd in the form frmRecipesMain and it is from that form that the subordinate form is opened. A2003 reports that it can't find the form frmRecipesMain.

    What have I missed here?

    Thanks,


    Bill

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    strRecipientName is the variable Dim'd in the
    This tells me the variable is in VBA code. It is not a control on a form so you cannot reference it using "Forms!frmRecipesMain!Form.strRecipientName".
    Is the variable declared in a procedure that is declared PRIVATE?

    You could use the "open arguments" argument to pass the data to the subordinate form.



    DoCmd.OpenForm "MyForm",,,,,,Me.ContributorName (see help)

    Syntax:
    DoCmd.OpenForm formname[, view][, filtername][, wherecondition][, datamode][, windowmode][, openargs]



    Then run code in the subordinate form to do what you want with the data in the openargs argument.

  3. #3
    ketbdnetbp is offline Competent Performer
    Windows 7 32bit Access 2003
    Join Date
    Mar 2011
    Location
    Midwest
    Posts
    254
    Bill - A few questions...

    Form1 = frmRecipesMain
    Form2 = What is it's name?

    Is Me.ContributorName on form1 or form2?

    When you say strRecipientName was dimmed on form1, was it something like...

    dim strRecipientName as string
    strRecipientName = ???What???? Is the value in a control? If so, what is the name of that control?

    Also, are both forms open when you get the error?

    Lastly, which form is the destination, i.e. where you want to set the value?

    Jim

  4. #4
    ketbdnetbp is offline Competent Performer
    Windows 7 32bit Access 2003
    Join Date
    Mar 2011
    Location
    Midwest
    Posts
    254
    Sorry Steve, didn't see your post before replying. Jim

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,959
    One form cannot set a variable declared in another form. Only the procedure that declares the variable can set the variable, unless variable has been globally declared in a general module.

    A form can set value in a control on another form.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    stmoong is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Sep 2011
    Location
    Singapore
    Posts
    108
    If you need to set the variable "strRecipientName" before the "frmRecipesMain" is loaded, then you have the following options:
    1) Use DoCmd.OpenForm as stated by Steve (IMHO, this approach is the cleanest)
    2) Use a module to store as global variable as stated by June7
    3) Use a hidden form to store the name in a text box (ugly approach)

    For the purpose of form A setting a variable in form B ("frmRecipesMain") after form B is loaded, you can either use a module and store as global variable Or using the OO-approach and extend the form. Not exactly better, because you can still set it through the DoCmd.OpenForm approach, but for the purpose of exploring, here it is

    In the code behind for form B:
    Code:
    Dim mStrRecipientName as String
    .....
    (some additional code)
    .....
    
    Public Sub SetRecipientName(ByVal pVal as String)
        'Here you can even do some validation prior to setting the value
        mStrRecipientName = pVal
    End Sub
    Then, in your code behind for the other form, you can do this. Just ensure that the frmRecipesMain is loaded before you call the subroutine.
    Code:
    .....
    Forms!frmRecipesMain.SetRecipientName "some names"
    .....

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 12-22-2012, 01:36 PM
  2. Replies: 3
    Last Post: 04-23-2012, 12:06 AM
  3. Setting global variables
    By Remster in forum Programming
    Replies: 1
    Last Post: 08-24-2011, 08:47 AM
  4. Replies: 1
    Last Post: 05-22-2010, 10:39 AM
  5. Replies: 3
    Last Post: 09-19-2008, 02:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums