Results 1 to 15 of 15
  1. #1
    CAhawkeye10 is offline Novice
    Windows 8 Access 2013
    Join Date
    Apr 2015
    Posts
    21

    Passing Multiple OpenArgs from One Form to the Next

    I currently have a Main Form with multiple parameters that I'm trying to pass to a Second Form using openargs, here is my code on the Main Form when the user clicks a button:



    Code:
    Private Sub SubmitAndClose_DblClick(Cancel As Integer)
    
    
    Dim ctl As Control
    For Each ctl In Me.Controls
    If (ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Or ctl.ControlType = acListBox) And ctl.Tag = "Validate" Then
        If ctl & "" = "" Then
            MsgBox "Enter data into control. Exit cancelled."
            Cancel = True
            Exit Sub
        End If
    End If
    Next
    Set ctl = Nothing
    If Cancel = False Then
    
    
    If Me.myListBox.Value = 1 Then
      DoCmd.OpenForm "New Change Estimate - RFI", , , , , , Me.CAN_RFI_Number & "|" & Me.Webcor_RFI_Number & "|" & Me.Description
      DoCmd.Close
      Else
      DoCmd.Close
    End If
    
    
    End If
    End Sub
    Here is my code on the Second Form that is opened ("New Change Estimate - RFI"):

    Code:
    Private Sub Form_Load()
    
    
    Dim strCAN_RFI_Number As String
    Dim strWebcor_RFI_Number As String
    Dim strDescription As String
    Dim strIDs() As String
    If Len(Me.OpenArgs & "") > 0 Then
        strIDs = Split(Me.OpenArgs, "|")
        strCAN_RFI_Number = strIDs(0)
        strWebcor_RFI_Number = strIDs(1)
        strDescription = strIDs(2)
        Me.Filter = "CAN_RFI_Number = " & strCAN_RFI_Number & " AND Webcor_RFI_Number = " & strWebcor_RFI_Number & " AND Description = " & strDescription
        Me.FilterOn = True
    End If
    
    
    End Sub
    Any time I try to pass these parameters using the Main Form I get the following error message:

    "Run-time error '3464': Data type mismatch in criteria expression."

    When I debug, the error comes up on this line of code:

    Code:
    Me.FilterOn = True
    I've double checked the associated tables and all of the fields have the same data types which is what I initially thought the problem was. Any help would be greatly appreciated.

    Thanks!

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Are they all text type field? Try apostrophe delimiters.

    Me.Filter = "CAN_RFI_Number = '" & strCAN_RFI_Number & "' AND Webcor_RFI_Number = '" & strWebcor_RFI_Number & "' AND Description = '" & strDescription & "'"
    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.

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Any text data type values need delimiters, like this for the last:

    Me.Filter = "CAN_RFI_Number = " & strCAN_RFI_Number & " AND Webcor_RFI_Number = " & strWebcor_RFI_Number & " AND Description = '" & strDescription & "'"
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    CAhawkeye10 is offline Novice
    Windows 8 Access 2013
    Join Date
    Apr 2015
    Posts
    21
    I changed that line of code to what you provided and I'm no longer getting any error messages but the parameters aren't being passed from the Main Form to the Second Form. The Second Form is opening but none of the fields are filled in. Do you know any reason why this would be happening?

    Thanks for the help.

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You didn't specify which you tried, or what the data types of the fields are. You can use this to test right after setting the filter:

    Debug.Print Me.Filter

    as done here, to see the finished string:

    BaldyWeb-Immediate window
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    The records not showing does not mean your open args are not being passed (I wouldn't call them parameters - to me that's something else altogether). Put a break on the second form's open or load event and when you get there, test the args by typing ?me.openargs in the immediate window and press Enter. If you get a string result, the open args are being passed and you can review the result in the immediate window to see if it is structured properly. If it is, the lack of records could be due to an incorrect form design property setting such as opening the form for data input only. Having said that, it looks like you could simplify your code anyway:
    If Me.myListBox.Value = 1 Then
    DoCmd.OpenForm "New Change Estimate - RFI", , , , , , Me.CAN_RFI_Number & "|" & Me.Webcor_RFI_Number & "|" & Me.Description
    DoCmd.Close


    Instead of building a separated list that you assign to an array that you split into variables that you concatenate (whew!) why not
    A) in form 1 code, construct the filter clause and pass it entirely as the open arg and assign to the filter in form 2, or
    B) in form 1 code, construct the filter clause, open the second form hidden, assign the clause, apply the filter, show the form

  7. #7
    CAhawkeye10 is offline Novice
    Windows 8 Access 2013
    Join Date
    Apr 2015
    Posts
    21
    I changed the delimiters to apostrophes and the data type for each is short text.

    Micron,

    I'm sorry, I'm a newbie to VBA/MS Access and I'm not sure I follow. I simplified the code as provided but I'm not sure what you mean by constructing the filter clause in form 1 and passing it entirely as the open arg and assigning to the filter in form 2. If you could provide the code required or at least get me started in the right direction that would be greatly appreciated.

    Do you consider this entire section of code as the filter clause? Appreciate the help guys.

    Code:
    Dim strCAN_RFI_Number As String
    Dim strWebcor_RFI_Number As String
    Dim strDescription As String
    Dim strIDs() As String
    If Len(Me.OpenArgs & "") > 0 Then
        strIDs = Split(Me.OpenArgs, "|")
        strCAN_RFI_Number = strIDs(0)
        strWebcor_RFI_Number = strIDs(1)
        strDescription = strIDs(2)
        Me.Filter = "CAN_RFI_Number = " & strCAN_RFI_Number & " AND Webcor_RFI_Number = " & strWebcor_RFI_Number & " AND Description = " & strDescription
        Me.FilterOn = True
    End If

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Or use the WHERE CONDITION argument of OpenForm.

    DoCmd.OpenForm "New Change Estimate - RFI", , , "CAN_RFI_Number = '" & Me.CAN_RFI_Number & " AND Webcor_RFI_Number = '" & Me.Webcor_RFI_Number & "' AND Description = '" & Me.Description & "'"
    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.

  9. #9
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    June7 has another valid method. Since there are so many ways to do just about anything, part of the learning process is what is neater, or more buggy or sloppier or just plain not right for the situation over some other method. We all have our favouites that we seem to remember best as well. So here's something I put together. You cannot use it as code verbatim - it is missing the error line label that I put in at the beginning and who knows what else. No sense in coding it all for you as I don't know which approach you will settle on. It is also full of comments not intended to be part of a code module.

    You are testing for empty string "" which is likely OK for current use, but "" is not Null, so unless you test for both or know for sure it can be one and not the other, why not test for both by calling a function any time you want to know, and only write the code that tests one time? I recommend this snippet in a standard module called mdlCommonFunctions for stuff that gets used frequently. See the Select Case part below for how it is called and how to build a list of control names that are missing data.

    Function HasNoData(vCheckVal As Variant) As Boolean
    HasNoData = False
    If IsNull(vCheckVal) Or vCheckVal = "" Then HasNoData = True
    End Function

    Dim ctl As Control
    Dim svList as string, svList2 as string, svFilter as string
    Dim svCanRfiNum as string, svWebRfiNum as string, svDesc as string

    on error goto errHandler
    svList = "Please enter a value for: " & vbCrLf
    svlist2 = ""

    For Each ctl In Me.Controls
    Select Case ctl.ControlType
    'you can separate cases and customize the message if needed
    Case acTextBox, acComboBox 'add more comma separated if needed
    If HasNoData(ctl) Then svlist2 = svlist2 & ctl.Controls.Item(0).Caption & vbCrLf
    End Select
    Next

    If svlist2 <> "" Then
    MsgBox svList & svlist2, vbOKOnly, "Missing Information"
    Exit Sub
    End If

    don't think you need to test if Cancel = False but doesn't hurt
    You appear to be working with listboxes, but I don't follow their relevance. The filter criteria seems to be coming from Me.controlSomething that is not a listbox. If so, why not reference the listbox item that has been selected if that's what you're using as filter criteria? An example:

    svJobId = Me.lstJobList.Column(1)
    svDept = Me.lstJobList.Column(0)
    svFilter = "JobID=" & svJobId & " AND Dept='" & svDept & "'"

    'I'd assign the variables here, not before the possible exit sub statement
    'Using your code as a guide:
    svCanRfiNum = Me.CAN_RFI_Number
    svWebRfiNum = Me.Webcor_RFI_Number
    svDesc = Me.Description

    'BTW, why are you insinuating the values are numbers [CAN_RFI_Number] if they are text (strings)? Your fields, control names and variables should indicate the data type as much as is practical and you should read up on naming coventions for Access if you have not already. It helps to debug code if you name a varable intSomething - you are looking for an integer type and if debugging shows its value is text, it may immediately solve a problem. Also, I NEVER,NEVER,EVER use spaces or special characters (-) in naming anything. You are just asking for more work at best, and at worst, more problems.

    svFilter = "CAN_RFI_Number = '" & svCanRfiNum & "' AND Webcor_RFI_Number = '" & svWebRfiNum
    svFilter = svFilter & "' AND Description = '" & svDesc & "'"

    This next part as you have written, closes your form and passes the string as the open args in that form, specify that the filter = the string and apply the filter. I believe it is good form to always reference the object to be closed. Your way closes the active object which 99% of the time will be safe.
    I use Docmd.close acform, "MyForm",acsaveno (or whatever option is desired). Note that if you use acsaveno and allow code to close a form whose design you have edited, it will not save your unsaved changes

    If Me.myListBox.Value = 1 Then
    DoCmd.OpenForm "New Change Estimate - RFI", , , , , , svFilter 'this is passing the filter string to the form as an open arg
    DoCmd.Close
    Else
    DoCmd.Close
    End If

    OK, with respect to your question, here is the method I was referring to. When you get to the part where you open the second form (hidden)

    docmd.open "frmMyForm",,,,acHidden & whatever else you might need

    reference the database forms collection and a specific form & set the filter property that was created as before and apply it

    with CurrentProject.Allforms "frmMyForm"
    .filter = svFilter
    .filteron = true
    .visible = True
    end with

    Consider using the allforms reference in cases where a prior exit statement may have prevented opening it. You will get an error if you try to close a form that is not open:
    If currentproject.allforms ("frmMyForm").isloaded then docmd.close "frmMyForm" etc.
    P.S. not sure about the bracketing. May be required here because of the referende to the isloaded property whereas I didn't use them above.

    Hope some of this gets you where you want to be.

  10. #10
    CAhawkeye10 is offline Novice
    Windows 8 Access 2013
    Join Date
    Apr 2015
    Posts
    21
    Micron,

    I appreciate the help and I've updated my code to include your comments but the info is still not being passed. The Second Form is opening blank even though info was entered into the Main Form. And yes, this is my first experience with MS Access and I'm beginning to realize some of my novice mistakes (like text as a data type for a number field, using spaces/special characters in names, etc.) but I'm going to go back and clean all of that stuff up. That being said, is there something I'm missing on the Second Form that's causing this info to not populate the Second Form?

    I've included a copy of the file if you want to take a look at it when you get a chance.

    Thanks.

    Example2.zip

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    You have an option group frame named myListBox - at first I thought there should be a listbox on the form.

    Double click on buttons - drove me nuts until I realized that.

    Both forms have DataEntry property set to Yes - this means cannot display existing records, filter criteria is ignored.
    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.

  12. #12
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801

    Smile

    observations

    Rule #1 ALWAYS use Option Expicit in every module, otherwise, you deserve the grief you get! A simple spelling mistake will cause errors that you may not even notice right away. In the VBE editor, go to Tools \ Options and check "Require Variable Declaration". It's beyond me why this is even an option. When you do this and compile the code, you will be surprised as to how many potential problems it finds in your code.

    your design requires that users know what entries are permitted, so I may be off track here.

    The value returned for control type is a number, but I don't see where you've made the association between the number and the text you are using for the type when you check for empty controls. It's not as simple as you've written. Change your code to this

    Dim ctl As Control
    For Each ctl In Me.Controls
    Debug.Print ctl.ControlType & ", " & ctl.ControlName

    and watch in the immediate window for the number and its name. As a result, your test for missing data in the control does not run.
    Also, see http://access.mvps.org/access/forms/frm0004.htm

    However, that's not your main problem. In addition to what June7 says (which I mentioned above: "the lack of records could be due to an incorrect form design property setting such as opening the form for data input only."

    When I tested, your reference to listbox value was always 0 (sorry to say, a poor name choice for those who have to follow the logic. It is an option group).
    So I forced the value to be 1 and this is what I've concluded:

    It looks like "Main Form" as you called it is 'Returned RFI' and is based on table 'RFI Log'
    and the form you open is 'New Change Estimate' which is based on a different table, 'Change Estimate Log'

    If 100, 1001 and test is entered into the "main" form, the values are entered into the table because you are using a direct connection.
    Then when you dblclick the button to open New Change Estimate, you are trying to apply a filter but the data I just entered is not in the second table that your form is looking at. I stopped there rather than worry about the filter not being applied to the second form or if the data entry property is correct because if I understand what is going on, it will never work. Did I miss something? I've run out of time for tonight.

  13. #13
    CAhawkeye10 is offline Novice
    Windows 8 Access 2013
    Join Date
    Apr 2015
    Posts
    21
    Okay, so it appears that I've put the cart before the horse on this one by trying to filter out values that technically aren't in the table yet. If that's the case, how do I complete what I'm trying to do?

    Do I need to go back to using openargs to pass the information? If so, I'm back to square one where I wasn't able to get that info on the Second Form (a.k.a. "New Change Estimate - RFI").

  14. #14
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Why are you duplicating data? Why are these three fields in both tables?

    If you really want this setup, what you need to do is populate new record in Change form with the values passed in OpenArgs, not apply filter.

    Why not using form/subform arrangement?
    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.

  15. #15
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    I don't say this maliciously, but as for the cart/horse thing, you do seem to be getting ahead of yourself. By that, I mean you are trying to manipulate things via code but you don't seem to have an understanding as to how database objects such as forms work with queries and tables, how to set up the tables to support your end goal, or whether or not to base your form on a table or query. That should come first. If you have the time, you should search for some basic tutorials - there's lots of free info out there. As June7 states, your tables are not set up right, which will probably cause problems all the way through your project as you go.
    P.S. what I said about the Option Explicit thing: forgot to say that you have to manually add the line to existing modules.

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

Similar Threads

  1. Passing multiple values to a second form
    By WithoutPause in forum Forms
    Replies: 39
    Last Post: 02-12-2014, 04:03 PM
  2. Replies: 5
    Last Post: 01-27-2013, 06:04 PM
  3. Replies: 8
    Last Post: 08-03-2011, 09:09 AM
  4. Open a second form using the OpenArgs
    By blueraincoat in forum Forms
    Replies: 6
    Last Post: 06-02-2011, 06:19 AM
  5. Opening a form with openargs and things
    By mwabbe in forum Forms
    Replies: 5
    Last Post: 09-29-2010, 11:01 AM

Tags for this Thread

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