What's wrong with my VB code?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • flower88
    New Member
    • Jun 2010
    • 22

    What's wrong with my VB code?

    Hi, I'm working on an Access 2007 database. I'm trying to build a search form that opens up your search on a query. I was working before, but I feel like i change something and now, it doesn't give me what I'm asking for. I have three fields on the form (companycell, companyflux, companyribbon). I don't know how to incorporate the value from companycell to the field on the query.

    Code:
    Private Sub Findbtm_Click()
    
    If Me.CompanyCell.Value = "" Then
    Beep
        Select Case MsgBox("Cell Company is required", vbOK)
        Case vbOK: 'do nothing
        Case Else: 'do nothing
        End Select
    Else
    
        If [Companyflux] = [company name] Or [CompanyRibbon] = [Company] Or [Companyflux] = "" Or [CompanyRibbon] = "" Then
                
                DoCmd.OpenQuery "Sales"
        Else
         Beep
            Select Case MsgBox("Combination not found. Would you like possible combinations?", vbYesNo)
                Case vbYes: 'finds possible combinations
                     Beep
                     Select Case MsgBox("Would you like to make Flux unknown?", vbYesNo)
                          Case vbYes: 'Makes flux company unknown
                              Me.Companyflux.Value = ""
                              Me.newflux = Me.Companyflux
                              Me.newcell = Me.CompanyCell
                          Case vbNo: 'leaves flux company as it is
                              Me.newflux = Me.Companyflux
                              Me.newcell = Me.CompanyCell
                          Case Else: 'default case to trap any errors
                                     'do nothing
                     End Select
                    Beep
                    Select Case MsgBox("Would you like to make Ribbon unknown?", vbYesNo)
                          Case vbYes: 'makes ribbon company unknown
                              Me.CompanyRibbon.Value = ""
                              Me.newribbon = Me.CompanyRibbon
                              Me.newcell = Me.CompanyCell
                          Case vbNo: 'leaves ribbon company as it is
                              Me.newribbon = Me.CompanyRibbon
                              Me.newcell = Me.CompanyCell
                          Case Else: 'defaul case to trab any errors
                                      'do nothing
                    End Select
                    If [newflux] = [company name] Or [newribbon] = [Company] Or [newflux] = "" Or [newribbon] = "" Then
           
                     DoCmd.OpenQuery "Sales", acViewNormal
                 
                    Else
                     Beep
                       Select Case MsgBox("Combination not found. Would you like to add a New Product?", vbYesNo)
                    
                        Case vbYes: 'opens new product "entry form" and closes "sales" form
                            DoCmd.OpenForm "entry form"
                            DoCmd.Close acForm, "sales"
                            
                        Case vbNo: 'closes "sales" form and goes to "main menu"
                             DoCmd.OpenForm "main menu"
                             DoCmd.Close acForm, "sales"
                             
                       End Select
                    
                    End If
                
                Case vbNo:
                    'Do nothing
                  
                Case Else: 'Default case to trap any errors
                    'Do nothing
        
            End Select
              
        
        End If
    End If
    End Sub
    Any ideas? I'm knew at this and I'm getting frustrated because I can't find what is wrong with this.
    Thanks before hand!
  • flower88
    New Member
    • Jun 2010
    • 22

    #2
    ok, I changed the if statements. It works find for one field on the form, newflux. but it does not work for my second field, newribbon. Any ideas would be much appreciated, thank you.
    Code:
    Private Sub Findbtm_Click()
    
    If Me.CompanyCell.Value = "" Then
    Beep
        Select Case MsgBox("Cell Company is required", vbOK)
        Case vbOK: 'do nothing
        Case Else: 'do nothing
        End Select
    Else
    Me.cellbox = Me.CompanyCell
    Me.newcell = Me.CompanyCell
    Me.firstflux = Me.Companyflux
    Me.firstribbon = Me.CompanyRibbon
        If [firstflux] = [company name] Or [firstribbon] = [Company] Then
                
                DoCmd.OpenQuery "Sales"
        Else
         Beep
            Select Case MsgBox("Combination not found. Would you like possible combinations?", vbYesNo)
                Case vbYes: 'finds possible combinations
                Me.newcell = Me.CompanyCell
                     Beep
                     Select Case MsgBox("Would you like to make Flux unknown?", vbYesNo)
                          Case vbYes: 'Makes flux company unknown
                              Me.Companyflux.Value = ""
                              Me.newflux = Me.Companyflux
                              
                          Case vbNo: 'leaves flux company as it is
                              Me.newflux = Me.Companyflux
                              
                          Case Else: 'default case to trap any errors
                                     'do nothing
                     End Select
                    Beep
                    Select Case MsgBox("Would you like to make Ribbon unknown?", vbYesNo)
                          Case vbYes: 'makes ribbon company unknown
                              Me.CompanyRibbon.Value = ""
                              Me.newribbon = Me.CompanyRibbon
                              
                          Case vbNo: 'leaves ribbon company as it is
                              Me.newribbon = Me.CompanyRibbon
                              
                          Case Else: 'defaul case to trab any errors
                                      'do nothing
                    End Select
                    If [newflux] = "" Or [newribbon] = "" Then
           
                     DoCmd.OpenQuery "Sales", acViewNormal
                                       
                        ElseIf [newflux] = [company name] Or [newribbon] = [Company] Then
                         DoCmd.OpenQuery "Sales", acViewNormal
                         ElseIf [newflux] = "" Or [newribbon] = [Company] Then
                         DoCmd.OpenQuery "Sales", acViewNormal
                         ElseIf [newribbon] = "" Or [newflux] = [company name] Then
                        DoCmd.OpenQuery "Sales", acViewNormal
                    Else
                     Beep
                       Select Case MsgBox("Combination not found. Would you like to add a New Product?", vbYesNo)
                    
                        Case vbYes: 'opens new product "entry form" and closes "sales" form
                            DoCmd.OpenForm "entry form"
                            DoCmd.Close acForm, "sales"
                            
                        Case vbNo: 'closes "sales" form and goes to "main menu"
                             DoCmd.OpenForm "main menu"
                             DoCmd.Close acForm, "sales"
                             
                       End Select
                    
                    End If
                    
                       
                    
                Case vbNo:
                    'Do nothing
                  
                Case Else: 'Default case to trap any errors
                    'Do nothing
        
            End Select
              
        
        End If
    End If
    End Sub

    Comment

    • code green
      Recognized Expert Top Contributor
      • Mar 2007
      • 1726

      #3
      What do you mean by "but it does not work"?
      How do you know "It works find for one field on the form, newflux"?
      Also what does this mean
      I don't know how to incorporate the value from companycell to the field on the query

      Comment

      Working...