Results 1 to 9 of 9
  1. #1
    PBCN is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2016
    Posts
    24

    Opening Form with ElseIf Statement

    So what I want to do is have multiple forms sourcing the same table and the opening of which form is depending on which type of work is being carried out.



    What I have done so far is create a column that has a small statement referring to what what type of work is being done for each record, then under a click event (the Job No) a set of else if statements.
    Code:
     
    Private Sub JobNo_Click()
    Dim Strfun As String
    
    If Me.TypeOfApproval = "CDC Only" Then
        Strfun = "JobsFCDCOnly"
    ElseIf Me.TypeOfApproval = "Stage Inspections" Then
        Strfun = "JobsF"
    ElseIf Me.TypeOfApproval = "CDC, PCA and OC" Then
        Strfun = "JobsFCDCOC"
    ElseIf Me.TypeOfApproval = "CC, PCA and OC" Then
        Strfun = "JobsFCCPCAOC"
    ElseIf Me.TypeOfApproval = "CC Only" Then
        Strfun = "JobsFCCOnly"
    ElseIf Me.TypeOfApproval = "PC and OC Only" Then
        Strfun = "JobsFPCOC"
    ElseIf Me.TypeOfApproval = "BCA Report" Then
        Strfun = "JobsFReport"
    End If
    
    DoCmd.OpenForm Strfun, acNormal, , "ID=" & Me.ID
    Ive done some google research and what I have above should work but I am getting a run-time error "The action or method requires a Form Name argument"

    Anyone able to shed some light on where I've gone wrong?

  2. #2
    sgthuth is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Dec 2017
    Location
    SC
    Posts
    33
    I did not encounter any issues with the code. I removed the ID portion of the open form command as I do not have the ID set with in my form. I just did this as a test and it worked fine. I added the else statement with the msgbox because if the information is left blank within your choices, it will cause an error.

    Option Compare Database
    Option Explicit


    Private Sub JobNo_Click()
    Dim Strfun As String


    If Me.TypeOfApproval = "CDC Only" Then
    Strfun = "JobsFCDCOnly"
    ElseIf Me.TypeOfApproval = "Stage Inspections" Then
    Strfun = "JobsF"
    ElseIf Me.TypeOfApproval = "CDC, PCA and OC" Then
    Strfun = "JobsFCDCOC"
    ElseIf Me.TypeOfApproval = "CC, PCA and OC" Then
    Strfun = "JobsFCCPCAOC"
    ElseIf Me.TypeOfApproval = "CC Only" Then
    Strfun = "JobsFCCOnly"
    ElseIf Me.TypeOfApproval = "PC and OC Only" Then
    Strfun = "JobsFPCOC"
    ElseIf Me.TypeOfApproval = "BCA Report" Then
    Strfun = "JobsFReport"
    Else
    MsgBox "Please make a selection for the task at hand."
    End If


    DoCmd.OpenForm Strfun, acNormal
    End Sub

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    you should use a table,
    or a select statement:

    Code:
    Select Case Me.TypeOfApproval
        Case "CDC Only"
            Strfun = "JobsFCDCOnly"
        Case "Stage Inspections"
            Strfun = "JobsF"
        Case "CDC, PCA and OC"
            Strfun = "JobsFCDCOC"
        Case "CC, PCA and OC"
            Strfun = "JobsFCCPCAOC"
        Case "CC Only"
            Strfun = "JobsFCCOnly"
        Case "PC and OC Only"
            Strfun = "JobsFPCOC"
        Case "BCA Report"
            Strfun = "JobsFReport"
    End Select
    is : "CDC, PCA and OC"
    one word or 3 VALUES:
    "CDC", "PCA", "OC"

  4. #4
    PBCN is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2016
    Posts
    24
    Quote Originally Posted by sgthuth View Post
    I did not encounter any issues with the code. I removed the ID portion of the open form command as I do not have the ID set with in my form. I just did this as a test and it worked fine. I added the else statement with the msgbox because if the information is left blank within your choices, it will cause an error.

    Option Compare Database
    Option Explicit


    Private Sub JobNo_Click()
    Dim Strfun As String


    If Me.TypeOfApproval = "CDC Only" Then
    Strfun = "JobsFCDCOnly"
    ElseIf Me.TypeOfApproval = "Stage Inspections" Then
    Strfun = "JobsF"
    ElseIf Me.TypeOfApproval = "CDC, PCA and OC" Then
    Strfun = "JobsFCDCOC"
    ElseIf Me.TypeOfApproval = "CC, PCA and OC" Then
    Strfun = "JobsFCCPCAOC"
    ElseIf Me.TypeOfApproval = "CC Only" Then
    Strfun = "JobsFCCOnly"
    ElseIf Me.TypeOfApproval = "PC and OC Only" Then
    Strfun = "JobsFPCOC"
    ElseIf Me.TypeOfApproval = "BCA Report" Then
    Strfun = "JobsFReport"
    Else
    MsgBox "Please make a selection for the task at hand."
    End If


    DoCmd.OpenForm Strfun, acNormal
    End Sub
    Actually I just did a new test DB and it worked for me too, maybe my VB code is too damaged, I have been decompiling but Im still getting some actions that dont make sense.

  5. #5
    PBCN is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2016
    Posts
    24
    CDC, PCA and OC is one word but I dont think its the wording, it seems the Docmd line is not accepting the variable Strfun in the string.

  6. #6
    sgthuth is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Dec 2017
    Location
    SC
    Posts
    33
    PBCN,
    What are the errors? Before was the run-time error and I believe you were just passing a "" value to the string. I learned a long time ago that if I'm having issues troubleshooting to take things step by step. Have you tried stepping through your vb?

  7. #7
    sgthuth is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Dec 2017
    Location
    SC
    Posts
    33
    You can try moving the code to a module and making it a public function and just call it with in the form code. Then you can test the code with the form open without having to switch back and forth. And the choices for the Else-If Statement, are they in a dropdown? For the test I did earlier, that's what I used to limit the information passed. Also, I added a simple error check.

    Option Compare Database
    Option Explicit


    Private Sub JobNo_Click()
    Dim Strfun As String
    Dim OpenYN As Integer


    OpenYN = 1


    If Me.TypeOfApproval = "CDC Only" Then
    Strfun = "JobsFCDCOnly"
    ElseIf Me.TypeOfApproval = "Stage Inspections" Then
    Strfun = "JobsF"
    ElseIf Me.TypeOfApproval = "CDC, PCA and OC" Then
    Strfun = "JobsFCDCOC"
    ElseIf Me.TypeOfApproval = "CC, PCA and OC" Then
    Strfun = "JobsFCCPCAOC"
    ElseIf Me.TypeOfApproval = "CC Only" Then
    Strfun = "JobsFCCOnly"
    ElseIf Me.TypeOfApproval = "PC and OC Only" Then
    Strfun = "JobsFPCOC"
    ElseIf Me.TypeOfApproval = "BCA Report" Then
    Strfun = "JobsFReport"
    Else
    MsgBox "Please make a selection for the task at hand."
    OpenYN = 0
    End If


    If OpenYN = 1 Then
    DoCmd.OpenForm Strfun, acNormal
    End If


    End Sub

  8. #8
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    Problem is that if no selection matches, you try to open the form anyway, and Strfun is blank. Add the red line to get out after displaying the messagebox.

    Code:
    Else
    MsgBox "Please make a selection for the task at hand."
    Exit Sub
    End If
    
    
    DoCmd.OpenForm Strfun, acNormal
    End Sub
    Last edited by davegri; 12-13-2017 at 01:27 PM. Reason: edit

  9. #9
    PBCN is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2016
    Posts
    24
    So I found out what was wrong, I had all statements slightly misspelt.

    I guess I learnt a lesson about writing code at 1:30am

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

Similar Threads

  1. Help with multiple ElseIf statement
    By Evans2 in forum Access
    Replies: 2
    Last Post: 05-23-2017, 10:36 AM
  2. Replies: 10
    Last Post: 10-11-2016, 02:39 AM
  3. Replies: 21
    Last Post: 04-22-2016, 04:19 PM
  4. Automate data input with If, Elseif Statement
    By Exmark1 in forum Programming
    Replies: 4
    Last Post: 02-19-2015, 09:52 AM
  5. Replies: 5
    Last Post: 03-03-2013, 07:18 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