Results 1 to 9 of 9
  1. #1
    tcheck is offline Competent Performer
    Windows 7 Access 2007
    Join Date
    Nov 2010
    Posts
    131

    Search for 2 different field criterea in VB command

    I have a simple question. I would like to open a sub form using 2 different fields within the open form. I have the following command with an on click;



    Private Sub Label27_Click()
    DoCmd.DoMenuItem acFormBar, acRecordsMenu, 5, , acMenuVer70
    DoCmd.OpenForm "frm_View_ActivityID", , , "[Mgr ID] = '" & Me![Mgr ID] & "'"
    End Sub


    This works fine but I would also like to use a date field added to the command but I can't seem to get it correct. I want to open it with Me!MGR ID and Me!AcctDate

    I tried
    Private Sub Label27_Click()
    DoCmd.DoMenuItem acFormBar, acRecordsMenu, 5, , acMenuVer70
    DoCmd.OpenForm "frm_View_ActivityID", , , "[Mgr ID] = '" & Me![Mgr ID] & "'" & "[ActDate] = '" & Me![ActDate] & "'"
    End Sub


    With no luck. Thanks for your help in advance.

  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,641
    You need the word " And " between the 2 criteria, and date values need to be surrounded by # rather than '.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    tcheck is offline Competent Performer
    Windows 7 Access 2007
    Join Date
    Nov 2010
    Posts
    131

    Thanks for the quick reply

    I tried
    DoCmd.OpenForm "frm_View_ActivityID", , , "[Mgr ID] = '" & Me![Mgr ID] & "'" AND "#[Actdate]# =" & Me!"#[ActDate]#"

    AndI am still getting errors.

  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,641
    The # would be positioned like the ' are around the text value, and like the text value, only around the value, not the field name. You are also closing off the string before the word AND, which leaves it out in the middle of nowhere.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    tcheck is offline Competent Performer
    Windows 7 Access 2007
    Join Date
    Nov 2010
    Posts
    131

    Thanks but still not getting it

    Can you give me an example?

  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,641
    Check out the multi-criteria example at the bottom:

    http://www.mvps.org/access/general/gen0018.htm

    The syntax of the wherecondition would be similar.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    tcheck is offline Competent Performer
    Windows 7 Access 2007
    Join Date
    Nov 2010
    Posts
    131
    Each one of the wher commands work independently fom one another, but when I I add them together such as;

    DoCmd.OpenForm "frm_View_Activity", , , "[Mgr ID] = '" & Me![Mgr ID] & "'" And "[ActDate] = #" & Me![Act_date] & "#"

    I get a VB error
    Run-Time error '12':

    Type mismatch

  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,641
    You're still closing the string off improperly. Try

    DoCmd.OpenForm "frm_View_Activity", , , "[Mgr ID] = '" & Me![Mgr ID] & "' And [ActDate] = #" & Me![Act_date] & "#"
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    tcheck is offline Competent Performer
    Windows 7 Access 2007
    Join Date
    Nov 2010
    Posts
    131
    That was it. Thank you very much. Have a great weekend.

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

Similar Threads

  1. Command button to change value of other field
    By teirrah1995 in forum Programming
    Replies: 8
    Last Post: 09-09-2010, 10:23 AM
  2. Replies: 2
    Last Post: 08-31-2010, 08:57 PM
  3. Changing Field Values with Command Button
    By avarusbrightfyre in forum Access
    Replies: 1
    Last Post: 08-22-2010, 12:48 PM
  4. Move to next field command...
    By mvolpone in forum Forms
    Replies: 1
    Last Post: 07-06-2010, 09:30 PM
  5. split form select records based on a criterea
    By ramkitty in forum Access
    Replies: 8
    Last Post: 03-12-2010, 06:19 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