MsgBox yes/no options to continue ot stop

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,226
Office Version
  1. 2007
Platform
  1. Windows
I am using the code below.

I start to type a name in Textbox1 & the results starts to appear in the listbox.
My first issue & may be more but at present unable to get past this step.
I select a customers name & i see a message Do i wish to delete this customer.
Yes should delete the customer & i should then get the confirmation message, customer is deleted but no confirmation message.
No should stop the process & selct TextBox 1, BUt i see customer has been deleted message followed by customer was not delete message, customer wasnt delete but i see the messages appear


Rich (BB code):
Private Sub ListBox1_Click()
  Dim answer As Integer
  
  Range("A" & ListBox1.List(ListBox1.ListIndex, 1)).Select
  
   Dim c As Range
   With Sheets("DETAILS")
    Set c = .Range("A:A").Find(What:=ListBox1.Value, _
                        After:=.Range("A5"), _
                        LookIn:=xlValues, _
                        LookAt:=xlWhole, _
                        SearchOrder:=xlByRows, _
                        SearchDirection:=xlNext, _
                        MatchCase:=False)
End With
If Not c Is Nothing Then
    If MsgBox("ARE YOU SURE YOU WISH TO DELETE CUSTOMER " & ListBox1.Text & "?", vbYesNo + vbInformation, "DELETE CUSTOMER FROM DATABASE") = vbYes Then
        Rows(c.Row).EntireRow.Delete
        Else:
        TextBox1.Value = ""
        TextBox1.SetFocus
        
        MsgBox "THE CUSTOMER " & Me.ListBox1.Value & " HAS NOW BEEN DELETED" & vbNewLine & "DELETE ANOTHER CUSTOMER ? ", vbYesNo + vbInformation, "CUSTOMER DELETED MESSAGE"
        If Result = vbYes Then
        TextBox1.Value = ""
           Else:
        Unload DeleteCustomer
        Range("A3").Select
        
    
        Unload DeleteCustomer
        MsgBox "THE CUSTOMER " & Me.ListBox1.Value & " WAS NOT DELETED", vbInformation, "CUSTOMER WAS NOT DELETED MEAASGE"
        Range("A3").Select
    End If
Set c = Nothing

      
    End If
    End If
    
End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Just an update & im 99% done but with one issue.

Here is the new code now in use.
All the code now does what it should depending on whether the user selected yes or no

The only issue i have is the following message box shown in red below
Selecting Yes clears textbox1 & sets focus on textbox1, this works ok
Selecting No should just unload the DeleteCustomer form & select cell A3 on the worksheet
But what actually happens textbox1 is cleard & userform is still showing




Rich (BB code):
Private Sub ListBox1_Click()
  Dim answer As Integer
  
  Range("A" & ListBox1.List(ListBox1.ListIndex, 1)).Select
  
   Dim c As Range
   With Sheets("DETAILS")
    Set c = .Range("A:A").Find(What:=ListBox1.Value, _
                        After:=.Range("A5"), _
                        LookIn:=xlValues, _
                        LookAt:=xlWhole, _
                        SearchOrder:=xlByRows, _
                        SearchDirection:=xlNext, _
                        MatchCase:=False)
End With
If Not c Is Nothing Then
    Result = MsgBox("ARE YOU SURE YOU WISH TO DELETE CUSTOMER " & ListBox1.Text & "?", vbYesNo + vbCritical, "DELETE CUSTOMER FROM DATABASE")
        If Result = vbYes Then
        Rows(c.Row).EntireRow.Delete
    Else:
        MsgBox "THE CUSTOMER " & Me.ListBox1.Value & " WAS NOT DELETED", vbInformation, "CUSTOMER WAS NOT DELETED MEAASGE"
        TextBox1.Value = ""
        TextBox1.SetFocus
        Exit Sub
    End If
    
       MsgBox "THE CUSTOMER " & Me.ListBox1.Value & " HAS NOW BEEN DELETED" & vbNewLine & vbNewLine & "DO YOU WISH TO DELETE ANOTHER CUSTOMER ? ", vbYesNo + vbInformation, "CUSTOMER DELETED MESSAGE"
        If Result = vbYes Then
        TextBox1.Value = ""
        TextBox1.SetFocus
    Else:
        Unload DeleteCustomer
        Range("A3").Select
        
        

    End If
    End If
    Set c = Nothing
End Sub
 
Upvote 0
Hi,
untested but see if this update to your code does what you want

VBA Code:
Private Sub ListBox1_Click()
    Dim answer      As VbMsgBoxResult
    Dim Search      As String
    Dim c           As Range
  
    Search = Me.ListBox1.Value
  
    With ThisWorkbook.Worksheets("DETAILS")
        Set c = .Range("A:A").Find(What:=Search, After:=.Range("A5"), LookIn:=xlValues, LookAt:=xlWhole, _
                                 searchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
    End With
  
    If Not c Is Nothing Then
      
        answer = MsgBox("ARE YOU SURE YOU WISH To DELETE CUSTOMER " & Search & "?", vbYesNo + vbInformation, "DELETE CUSTOMER FROM DATABASE")
      
        If answer = vbYes Then
            c.EntireRow.Delete
          
            answer = MsgBox("THE CUSTOMER " & Search & " HAS NOW BEEN DELETED" & vbNewLine & "DELETE ANOTHER CUSTOMER ? ", vbYesNo + vbInformation, "CUSTOMER DELETED MESSAGE")
            If answer = vbNo Then Unload Me Else TextBox1.Value = "": TextBox1.SetFocus
          
        Else
      
            MsgBox "THE CUSTOMER " & Search & " WAS Not DELETED", vbInformation, "CUSTOMER WAS Not DELETED MESSAGE"
          
        End If
      
    End If
  
    Set c = Nothing
  
End Sub

Dave
 
Upvote 0
Thanks,
I will take a look once home.

At the bottom of the code i see Set c = Nothing

Can you advise what it actually does.
 
Upvote 0
At the bottom of the code i see Set c = Nothing

Can you advise what it actually does.

As it was in your original code I left it there - what it does is clear the object variable c from memory but as the scope of the variable is procedure level (otherwise known as local ) it is accessible only within your procedure and as soon as the code finishes, the variable will lose its scope i.e the object variable should be nothing once code terminates.
Some coders though like to include a "clean up" for object variables at end of their procedures so does no harm.

Dave
 
Upvote 0

Forum statistics

Threads
1,214,965
Messages
6,122,499
Members
449,089
Latest member
Raviguru

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top