Results 1 to 12 of 12
  1. #1
    CT_AccessHelp is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    40

    Opening a form to filter Data (VBA)

    I have an old database we are trying to resurrect.

    The database has 2 forms, one is used to open the other based on selected criteria.

    I have added a Practice list box to the form used to open the second form. The button on the first form currently opens the second form and queries the data to show up in the second form by county based on the county selected in the combo box. I have added the second practice field on the first form but I cannot figure out how to edit the code so it now queries the data in the second form by county and practice. Basically if I pick Anoka and CP23 I only want to see the entered CP23s entered for Anoka on the second form. Any Ideas? Thank you!
    Click image for larger version. 

Name:	Form1.jpg 
Views:	11 
Size:	54.8 KB 
ID:	24018

    Click image for larger version. 

Name:	Form2.jpg 
Views:	11 
Size:	136.9 KB 
ID:	24019


    Current Code tied to the button on the first form:


    Private Sub Command3_Click()
    On Error GoTo Err_Command3_Click



    Dim stDocName As String
    Dim stLinkCriteria As String

    stDocName = "frm_CountyRecords"
    stLinkCriteria = "[County]=" & "'" & Me![Combo1] & "'"
    DoCmd.OpenForm stDocName, , , stLinkCriteria

    Exit_Command3_Click:
    Exit Sub

    Err_Command3_Click:
    MsgBox Err.Description
    Resume Exit_Command3_Click

    End Sub

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Add the second field to the stLinkCriteria variable, separated from the first field by AND.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    CT_AccessHelp is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    40
    Quote Originally Posted by pbaldy View Post
    Add the second field to the stLinkCriteria variable, separated from the first field by AND.
    I'm just winging it on the VBA stuff, so any help you can provide is helpful - Thank you

    Like this? stLinkCriteria = "[County]=" And "[Practice]=" & "'" & Me![Combo1] & "'" And "'" & Me!
    [List20] & "'"

    I tried that and got a "mismatched type" message

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Well, the final outcome you're looking for is:

    "Field1 = 'abc' AND Field2 = 'def'"

    so more like

    stLinkCriteria = "[County]= '" & Me![Combo1] & "' And [Practice]='" & Me!

    [List20] & "'"
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    CT_AccessHelp is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    40
    Quote Originally Posted by pbaldy View Post
    Well, the final outcome you're looking for is:

    "Field1 = 'abc' AND Field2 = 'def'"

    so more like

    stLinkCriteria = "[County]= '" & Me![Combo1] & "' And [Practice]='" & Me!

    [List20] & "'"

    Thank you for the example - I tired it and got the type mismatched message again

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Try this to see what the finished string is:

    http://www.baldyweb.com/ImmediateWindow.htm

    and what are the data types of the 2 fields? They look like text, but are they lookup fields or anything?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    CT_AccessHelp is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    40
    Quote Originally Posted by pbaldy View Post
    Try this to see what the finished string is:

    http://www.baldyweb.com/ImmediateWindow.htm

    and what are the data types of the 2 fields? They look like text, but are they lookup fields or anything?

    Both Combo boxes look up to separate tables and both data types are text. Super frustrating I can't get it to work out

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    So you didn't try the debugging method? Can you attach the db here?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    CT_AccessHelp is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    40
    Quote Originally Posted by pbaldy View Post
    So you didn't try the debugging method? Can you attach the db here?
    Yea, it did nothing, maybe I did it wrong. Is there a way to attached it? It is actually 2 databases, tables are split in the backend. Thing is a mess LOL

    I have tried the code with only "[County]=" & "'" & Me![Combo1] & "'" and it works with the County and then with stLinkCriteria = "[Practice]=" & "'" & Me![Combo22] & "'" (I changed it to a combo box to see if that helped, didn't help) only and it works with the Practice but together stLinkCriteria = "[County]=" & "'" & Me![Combo1] & "'" And "[Practice]=" & "'" & Me![Combo22] & "'" they won't work together, I get the type mismatched error.

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    It can be attached in the "Go Advanced" area. If each worked separately my code earlier should have worked. You've got the quotes off. Try

    stLinkCriteria = "[County]=" & "'" & Me![Combo1] & "' And [Practice]=" & "'" & Me![Combo22] & "'"
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    CT_AccessHelp is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    40
    Quote Originally Posted by pbaldy View Post
    It can be attached in the "Go Advanced" area. If each worked separately my code earlier should have worked. You've got the quotes off. Try

    stLinkCriteria = "[County]=" & "'" & Me![Combo1] & "' And [Practice]=" & "'" & Me![Combo22] & "'"
    OMG!!!! IT WORKED!!! Thank you so much!!!!

  12. #12
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Whew! Happy to help.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Need to Speed up Form on Opening, Add Filter?
    By Jlacey91 in forum Access
    Replies: 2
    Last Post: 12-16-2015, 08:29 AM
  2. Best Way to Filter A Form on Opening
    By batowl in forum Forms
    Replies: 2
    Last Post: 05-14-2015, 07:10 PM
  3. Replies: 2
    Last Post: 02-27-2015, 02:49 AM
  4. Filter checkbox upon opening form
    By vickster3659 in forum Programming
    Replies: 9
    Last Post: 11-06-2014, 10:58 AM
  5. Replies: 1
    Last Post: 08-01-2012, 03:56 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