locked
search button issues RRS feed

  • Question

  • I am using a search button to locate entry based on ID.  I am getting and Else If error.

    Private Sub btnSearchform_Click()

    'Create Variable
        Dim strID As String
        Dim strSql As String
       
        Dim dbs As DAO.Database
        Dim rst As DAO.Recordset
       
          
    'Get Specimen ID
        strID = InputBox("Please enter a Specimen ID.", "Specimen ID")

        Set dbs = CurrentDb
       
            strSql = "SELECT FormUsed FROM tblBodyFluidMain WHERE [Specimen ID Number] = '" & strID & "';"
            Set rst = dbs.OpenRecordset(strSql)
           
            If Not rst.RecordCount = 0 Then
                'This is a body fluid!
                Select Case rst("FormUsed").Value
               
                    Case "BFDilution"
                         DoCmd.OpenForm "frmBodyFluidDilutions2", acNormal, , "[Specimen ID Number] = '" & strID & "'"
                   
                    Case "Recovery"
                         DoCmd.OpenForm "frmBodyFluidRecoveries", acNormal, , "[Specimen ID Number] = '" & strID & "'"
               
                    Case "PTHFN"
                        DoCmd.OpenForm "frmBodyFluidPTHFN2", acNormal, , "[Specimen ID Number] = '" & strID & "'"
                   
                    Case "Remedy"
                        DoCmd.OpenForm "frmRemedyBodyFluids", acNormal, , "[Specimen ID Number] = '" & strID & "'"
                   
                End Select
               
            Else
                strSql = "SELECT FormUsed FROM tblSerum WHERE [Specimen ID Number] = '" & strID & "';"
                Set rst = dbs.OpenRecordset(strSql)
               
                If rst.RecordCount = 0 Then
                    MsgBox ("This specimen ID does not exists as Body Fluid or Serum.")
                    Exit Sub
                End If
               
                'This is a serum!
                Select Case rst("FormUsed").Value
               
                    Case "Dilution"
                        DoCmd.OpenForm "frmSerumDilutions1-2", acNormal, , "[Specimen ID Number] = '" & strID & "'"
                   
                    Case "Heterophile"
                         DoCmd.OpenForm "frmSerumHeterophile1", acNormal, , "[Specimen ID Number] = '" & strID & "'"
                   
                    Case "Miscellaneous"
                         DoCmd.OpenForm "frmSerumMiscellaneous2", acNormal, , "[Specimen ID Number] = '" & strID & "'"
                End Select
               
             Else
                strSql = "SELECT FormUsed FROM tblHeterophileA  WHERE [Specimen ID Number] = '" & strID & "';"
                Set rst = dbs.OpenRecordset(strSql)
               
                If rst.RecordCount = 0 Then
                    MsgBox ("This specimen ID does not exists as Heterophile.")
                    Exit Sub
                End If
               
                'This is a heterophile!
                Select Case rst("FormUsed").Value
                   
                    Case "Heterophile"
                         DoCmd.OpenForm "frmSerumHeterophile1", acNormal, , "[Specimen ID Number] = '" & strID & "'"
                End Select
           
            End If

    End Sub

    Thursday, June 11, 2020 6:39 PM

All replies

  • You can use only 1 Else.

    If Then

    Else

    End If

    Thursday, June 11, 2020 7:27 PM
  • You can use only 1 Else.

    If Then

    Else

    End If

    … but you can use ElseIf for a next condition:

        If (…..) Then
            …
        ElseIf (…..) Then
            …
        ElseIf (…..) Then
            …
        EndIf

    Personally I like more the construct with

        Select Case <variable>
        Case value1
            …
        Case value2
            …
        Case value3
            …
        End Select

    Imb.

    Edit: When re-reading the post I see that Select Case is already used quite a lot.
    • Edited by Imb-hb Thursday, June 11, 2020 8:35 PM edit
    Thursday, June 11, 2020 8:32 PM
  •             'This is a body fluid!
                Select Case rst("FormUsed").Value
               
                    Case "BFDilution"
                         DoCmd.OpenForm "frmBodyFluidDilutions2", acNormal, , "[Specimen ID Number] = '" & strID & "'"
                   
                    Case "Recovery"
                         DoCmd.OpenForm "frmBodyFluidRecoveries", acNormal, , "[Specimen ID Number] = '" & strID & "'"
               
                    Case "PTHFN"
                        DoCmd.OpenForm "frmBodyFluidPTHFN2", acNormal, , "[Specimen ID Number] = '" & strID & "'"
                   
                    Case "Remedy"
                        DoCmd.OpenForm "frmRemedyBodyFluids", acNormal, , "[Specimen ID Number] = '" & strID & "'"
                   
                End Select

    Hi salicem,

    I like very much short notations.

    So the above I would typically write as:

    Select Case rst("FormUsed") Case "BFDilution": formname = "frmBodyFluidDilutions2" Case "Recovery": formname = "frmBodyFluidRecoveries" Case "PTHFN": formname = "frmBodyFluidPTHFN2" Case "Remedy": formname = "frmRemedyBodyFluids" Case Else: formname = "" End Select If (formname > "") Then DoCmd.OpenForm formname, acNormal, , "[Specimen ID Number] = '" & strID & "'"


    Imb.

    Thursday, June 11, 2020 8:59 PM