Results 1 to 12 of 12
  1. #1
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581

    If statement to open in different forms

    I have a form [frmSupervisors]. On it, I have a list box, [lstUnapprovedRpts]. Here's the SQL to it: SELECT [ReportID], [ReportTypeID], [ReportType], [OccDate], [BldgName], [FullName] FROM qryRPTUnapproved ORDER BY [OccDate], [FullName];

    I'm trying to double click the list box and have it open them in a particular form. If [ReportTypeID]=2, I need it to open it up in [frmRptOR], If [ReportTypeID]=3, I need it to open it up in [frmRptSecurity]. If [ReportTypeID]=4, I need it to open it up in [frmRptAlarm]. All them need to go to the record, [ReportID].

    I figure that I need an if statement. I just can't figure it out.

  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,521
    You could use If/ElseIf, I'd probably use Select/Case. In each test, set a variable to the desired form name, then after use that variable with OpenForm. You could also have a table that related the ID to the name.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581
    I started off with just an if statement

    Private Sub lstUnapprovedRpts_DblClick(Cancel As Integer)
    If [ReportTypeID] = 4 Then
    DoCmd.OpenForm "frmRptAlarm", , , "[ReportID] = [ReportID]"
    End If

    End Sub

    I got this error:
    Click image for larger version. 

Name:	error1.jpg 
Views:	7 
Size:	22.1 KB 
ID:	33436
    Click image for larger version. 

Name:	error2.jpg 
Views:	7 
Size:	73.6 KB 
ID:	33437

    Figure I should fix it from the beginning

  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,521
    You have to refer to the listbox, perhaps including column, not a field name returned by the listbox.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,895
    Refer to the listbox to get the report type. What column has the type number? Also, must concatenate the ReportID parameter.
    Code:
    If Me.lstUnapprovedRpts = 4 Then
         DoCmd.OpenForm "frmRptAlarm", , , "ReportID=" & Me.ReportID
    EndIf
    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
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Oh, and the wherecondition syntax needs tweaking:

    http://www.baldyweb.com/wherecondition.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581
    It got past the first step. But it won't open the form. I'm getting an error:

    Click image for larger version. 

Name:	error1.jpg 
Views:	8 
Size:	22.1 KB 
ID:	33440
    Click image for larger version. 

Name:	error2.jpg 
Views:	8 
Size:	89.8 KB 
ID:	33441

  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,521
    The form has that field in its record source? While in debug mode, hover over the form reference and see what value it contains.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,973
    Two questions
    1. Is ReportID a number field? If not the code needs tweaking
    2. Are you aware that the first column in a listbox is column(0) so column(1) is the 2nd column
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  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,521
    If you're trying to get report ID from the listbox selection, you need to refer to the listbox there too.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581
    Yes. It is a number field. I did remember that the first column is 0.
    I completely forgot that I had to reference the list box again. That worked. Thanks. This is what I did.

    Private Sub lstUnapprovedRpts_DblClick(Cancel As Integer)
    If [lstUnapprovedRpts].Column(1) = 4 Then
    DoCmd.OpenForm "frmAlarmReport", , , "[ReportID] = " & Forms![frmSupervisors]![lstUnapprovedRpts]

    ElseIf [lstUnapprovedRpts].Column(1) = 3 Then
    DoCmd.OpenForm "frmRptSecurity", , , "[ReportID] = " & Forms![frmSupervisors]![lstUnapprovedRpts]

    End If

    End Sub

  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,521
    No problem, glad you got it working.
    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. open form within IF statement
    By joecamel9166 in forum Programming
    Replies: 9
    Last Post: 03-17-2016, 08:15 PM
  2. if statement about forms
    By redekopp in forum Programming
    Replies: 5
    Last Post: 12-10-2015, 11:11 AM
  3. Replies: 3
    Last Post: 10-23-2013, 08:11 AM
  4. Replies: 2
    Last Post: 08-07-2013, 07:44 AM
  5. Syntax of rs.Open Statement
    By Philosophaie in forum Access
    Replies: 5
    Last Post: 07-05-2013, 06:24 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