Results 1 to 14 of 14
  1. #1
    gprabhav is offline Novice
    Windows 7 32bit Access 2000
    Join Date
    May 2013
    Posts
    6

    Pass values from FORM1 to FORM2 query parameter

    I am trying to pass a value from FORM1 OnClick button event to FORM2 query parameter.


    Somehow it is not accepting the value.

    In FORM1, I am passing mr_number (textbox value) as follow:

    Code:
    Private Sub CommandAdd_Click()
    On Error GoTo CommandAdd_Click_Err
    
        On Error Resume Next
        DoCmd.GoToRecord , "", acNewRec
        If (MacroError <> 0) Then
            Beep
            MsgBox MacroError.Description, vbOKOnly, ""
        End If
        DoCmd.OpenForm "FORM2", acNormal, "", , acEdit, acNormal, Me.mr_number
       
            
    CommandAdd_Click_Exit:
        Exit Sub
    ---------------------

    In FORM2, I am accepting the value in Form_Load as follow:

    Code:
    Private Sub Form_Load()
            If Not IsNull(Me.OpenArgs) Then
               Me.mr_number = Me.OpenArgs
            End If
    End Sub
    ----------------------

    My Query's where clause is:

    WHERE (((Pt.mr_number)=[Forms]![FORM2]![mr_number]));

    ----------------------

    I do not receive any error. Just that an empty FORM2 pops up.

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    By the time set the mr_number control, the query has already run. Try adding a Me.Requery to your code.

  3. #3
    gprabhav is offline Novice
    Windows 7 32bit Access 2000
    Join Date
    May 2013
    Posts
    6
    Still doen't work.

    The call from FORM1 is :
    DoCmd.OpenForm "FORM2", acNormal, "", , acEdit, acNormal, Me.mr_number.Value

    In FORM2:
    Code:
    Private Sub Form_Load()
            If Not IsNull(Me.OpenArgs) Then
               Me.mr_number = Me.OpenArgs
            End If
            Me.Requery
    End Sub

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Why not try using the WhereCondition argument of the OpenForm command instead?
    DoCmd.OpenForm "FORM2", , , "mr_number = " & Me.mr_number, acEdit

  5. #5
    rzw0wr is offline I will always be a newbie
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Indiana
    Posts
    479
    You have not passed anything to the form.

    The openform statement you are using is for opening a form not to pass a variable with.

    Dale

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    The OP is passing the value in the OpenArgs argument of the OpenForm command.

  7. #7
    rzw0wr is offline I will always be a newbie
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Indiana
    Posts
    479
    I hope it is OK to disagree, ruralGuy.
    My understanding of "Passing" is that the variable is not in the form to be opened and that you are trying to give the form/record a new piece of data.
    The openarg statement will pass or tell a form to open with a variable if this variable is in the underlying table/query of the form.
    In outher words it is not like passing data from a function back to a calling sub.

    Is this a correct assumption?

    Dale

  8. #8
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    If you did a MsgBox OpenArgs in the OnLoad event you would see that in fact the "value" has been passed to the next form. Are we just discussing semantics? It is of course OK to disagree with me or anyone else.

  9. #9
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    @gprabhav - Not that it will effect the results but is the "mr_number" a field it the table and is it a numeric field as opposed to a string (text) field?

  10. #10
    rzw0wr is offline I will always be a newbie
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Indiana
    Posts
    479
    Thank you RuralGuy.
    I have been duly corrected.

    Dale

  11. #11
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    I take it you have not used OpenArgs yet? I use it a lot.

  12. #12
    rzw0wr is offline I will always be a newbie
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Indiana
    Posts
    479
    RuralGuy,
    I am still very new to Access.
    I have the concept however, I lack the knowledge and experience that comes with using it.

    I usally have the right idea but the wrong approach.

    And no, I have never used the openarg statement yet.
    Since I had the wrong idea about what it did.
    I will need to check it out more closely.

    Thanks,
    Dale

  13. #13
    gprabhav is offline Novice
    Windows 7 32bit Access 2000
    Join Date
    May 2013
    Posts
    6
    RuralGuy,
    IT WORKED !!! Thanks !!!
    The Problem was mr_number (which is TEXT) was loosing its value after updating the database (SQL-SERVER) from FORM1. So null value was passed to FORM2.
    The filter in FORM2 became [mr.number] = ''

    Here is the final WORKING code.
    Code:
    Private Sub CommandAdd_Click()
    On Error GoTo CommandAdd_Click_Err
        
        Dim sWHERE As String
        sWHERE = "[mr_number] = '" & Me.mr_number & "'"
        
        On Error Resume Next
        DoCmd.GoToRecord , "", acNewRec
        If (MacroError <> 0) Then
            Beep
            MsgBox MacroError.Description, vbOKOnly, ""
        End If
        
        DoCmd.OpenForm "FORM2", acNormal, , sWHERE
        
        
                          
    CommandAdd_Click_Exit:
        Exit Sub

  14. #14
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Excellent! Thanks for posting back with your success and solution. I marked this thread as Solved for you.

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

Similar Threads

  1. Replies: 3
    Last Post: 09-19-2012, 11:40 AM
  2. Replies: 2
    Last Post: 02-10-2011, 10:44 AM
  3. Replies: 3
    Last Post: 11-26-2010, 12:38 PM
  4. Replies: 1
    Last Post: 12-01-2009, 09:00 AM
  5. Pass a Parameter From a form to a Query
    By DDillesha in forum Forms
    Replies: 1
    Last Post: 10-28-2009, 12:49 PM

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