userform exit w/o entering data

KDS14589

Board Regular
Joined
Jan 10, 2019
Messages
182
Office Version
  1. 2016
Platform
  1. Windows
I have a userform with several textboxes that I enter data into. Then I have a 'Continue' button that flashes a message box with the data and a vbYesNoCancel asks me if I wish to enter the data. 'Yes' enters the data, 'No' redirects me back to that form by 'EXIT SUB', this all works until I 'Cancel', I can't get it to exit without loading the data. I've tried the 'UNLOAD' 'userform' but it just unloads the form while entering the data.

HELP
VBA Code:
    Dim OutPut As Integer


    'Example of vbYesNoCancel
    OutPut = MsgBox("Name: " & txtC_name & vbCrLf & "Acc. #: " & txtC_account & vbCrLf & "Status: " & ComboBoxc_status & vbCrLf & "Type: " & ComboBoxc_type & vbCrLf & "Interest Rate: " & txtC_interest & vbCrLf _
    & "Credit Limit: " & txtC_credit_limit & vbCrLf & "Due Day: " & ComboBoxC_due_day & "th", vbQuestion + vbYesNoCancel, "Enter Data?")

    If OutPut = 6 Then
        'Output = 6(yes)
       MsgBox "Data has been entered", vbInformation, "Entered"
    
    ElseIf OutPut = 7 Then
        'Output = 7(no)
       MsgBox "You will now redirected to the edit mode", vbInformation, "Edit Mode"
       Exit Sub
    Else
    'output = 2(cancel)
    MsgBox "You will now Exit w/o entering data", vbInformation, "Exit"
    Unload Card_Name_UF
       
    End If
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
provide the complete form code module, or at least the relevant parts of it - like what happens upon Terminate or QueryClose.
And how exactly is the data entered and where? Are the form controls bound to cells or not - check the ControlSource property?
If the controls are bound - the behaviour you describe is absolutely normal. Once you change the control value the bound cell is also changed, NOT upon closing the form.
If you want a different behaviour - unbind the controls and create a procedure to actually enter the data wherever you need it.
 
Upvote 0
provide the complete form code module, or at least the relevant parts of it - like what happens upon Terminate or QueryClose.
And how exactly is the data entered and where? Are the form controls bound to cells or not - check the ControlSource property?
If the controls are bound - the behaviour you describe is absolutely normal. Once you change the control value the bound cell is also changed, NOT upon closing the form.
If you want a different behaviour - unbind the controls and create a procedure to actually enter the data wherever you nee

provide the complete form code module, or at least the relevant parts of it - like what happens upon Terminate or QueryClose.
And how exactly is the data entered and where? Are the form controls bound to cells or not - check the ControlSource property?
If the controls are bound - the behaviour you describe is absolutely normal. Once you change the control value the bound cell is also changed, NOT upon closing the form.
If you want a different behaviour - unbind the controls and create a procedure to actually enter the data wherever you need it.
Here is the full code for that UserForm, my problem is about 1/2 way down

VBA Code:
Private Sub ComboBoxC_due_day_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)

  If Not IsNumeric(Me.ComboBoxC_due_day.Value) Then
    MsgBox "Please enter a number between 1 and 31", vbCritical, "Due Day"
    Cancel = True
  ElseIf Me.ComboBoxC_due_day.Value < 1 Or Me.ComboBoxC_due_day.Value > 31 Then
    MsgBox "Please enter a number between 1 and 31", vbCritical, "Due Day"
    Cancel = True
  End If

End Sub

Private Sub txtC_credit_limit_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)

If Not IsNumeric(Me.txtC_credit_limit.Value) Then
    MsgBox "Please enter a 5-digit number", vbCritical, "Credit Limit Number"
    Cancel = True
  ElseIf Me.txtC_credit_limit.Value < 0 Then
    MsgBox "Please enter a 5-digit number above 0", vbCritical, "Credit Limit Number"
    Cancel = True
  End If
Me.txtC_credit_limit.Value = Format(Me.txtC_credit_limit.Value, "$#,##0.00")

End Sub

Private Sub txtC_interest_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)

If Not IsNumeric(Me.txtC_interest.Value) Then
    MsgBox "Please enter up to 4-digit number between 0 & 100.0", vbCritical, "Interest Number"
    Cancel = True
  ElseIf Me.txtC_interest.Value < 0 Or Me.txtC_interest.Value > 100.01 Then
    MsgBox "Please enter up to 4-digit number between 0 & 100.0", vbCritical, "Interest Number"
    Cancel = True
  End If
 Me.txtC_interest.Value = Format((Me.txtC_interest.Value) / 100, "#0.00%")
 
End Sub

Private Sub txtC_interest_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

    If (KeyAscii >= 48 And KeyAscii <= 57) Or (KeyAscii = 46) Then
    KeyAscii = KeyAscii
    Else
    KeyAscii = 0
    End If
    
End Sub

Private Sub commandC_close_card_data_form_Click()

Unload Card_Name_UF

End Sub

Private Sub CommandC_continue_Click()
'when we click the enter record button

Dim TargetRow As Integer    'variable for position control
Dim Fullname As String  'full name
Dim UserMessage As String 'variable to configure user message at the end

Fullname = txtC_name & " " & txtC_account   'concatenate name and account for use in code below

'begin check if in 'edit' or 'add' mode

If ShCO01.Range("AN8").Value = "NEW" Then   'in 'new' mode

'begin validaton check 'check' if name already exsits

If Application.WorksheetFunction.CountIf(ShGE01.Range("G9:G109"), Fullname) > 0 Then

MsgBox Fullname & " " & "already exists", 0, "Check"

Exit Sub    'notify user and exit the routine

End If
' end validation check

' check for blanks
If txtC_name.Text = "" Then
txtC_name.BackColor = vbYellow
Cancel = 1
MsgBox "Name Box is empty"
txtC_name.SetFocus
Exit Sub
End If
If txtC_account.Text = "" Then
txtC_account.BackColor = vbYellow
Cancel = 1
MsgBox txtC_name & " Account # Box is empty"
txtC_account.SetFocus
Exit Sub
End If
If txtC_interest.Text = "" Then
txtC_interest.BackColor = vbYellow
Cancel = 1
MsgBox txtC_name & " " & txtC_account & " Interest Rate Box is empty"
txtC_interest.SetFocus
Exit Sub
End If
If txtC_credit_limit.Text = "" Then
txtC_credit_limit.BackColor = vbYellow
Cancel = 1
MsgBox txtC_name & " " & txtC_account & " Credit Limit Box is empty"
txtC_credit_limit.SetFocus
Exit Sub
End If
If ComboBoxC_due_day.ListIndex = -1 Then
ComboBoxC_due_day.BackColor = vbYellow
Cancel = 1
MsgBox txtC_name & " " & txtC_account & " ComboBox for days is empty"
ComboBoxC_due_day.SetFocus
Exit Sub
End If
 
    
TargetRow = ShCO01.Range("AN7").Value + 1 'make variable equal to COUNTA formula on worksheet + 1

'end of data entry message

    Dim OutPut As Integer

    'Example of vbYesNo
    OutPut = MsgBox("Name: " & txtC_name & vbCrLf & "Acc. #: " & txtC_account & vbCrLf & "Status: " & ComboBoxc_status & vbCrLf & "Brand: " & ComboBoxc_brand & vbCrLf & "Type: " & ComboBoxc_type & vbCrLf & "Interest Rate: " & txtC_interest & vbCrLf _
    & "Credit Limit: " & txtC_credit_limit & vbCrLf & "Due Day: " & ComboBoxC_due_day & "th", vbQuestion + vbYesNoCancel, "Enter Data?")

    If OutPut = 6 Then
        'Output = 6(yes)
       MsgBox "Data has been entered", vbInformation, "Entered"
    
    ElseIf OutPut = 7 Then
        'Output = 7(no)
       MsgBox "You will now redirected to the edit mode", vbInformation, "Edit Mode"
       Exit Sub
    Else
    'output = 2(cancel)
    MsgBox "You will now Exit w/o entering data", vbInformation, "Exit"
    Unload Card_Name_UF
       
    End If

Else    'in 'edit'mode
TargetRow = ShCO01.Range("AN9").Value   'make variable equal to value saved in 'controls'

'end of data edit message
MsgBox "Name: " & txtC_name & vbCrLf & "Acc. #: " & txtC_account & vbCrLf & "Status: " & ComboBoxc_status & vbCrLf & "Brand: " & ComboBoxc_brand & vbCrLf & "Type: " & ComboBoxc_type & vbCrLf & "Interest Rate: " & txtC_interest & vbCrLf _
    & "Credit Limit: " & txtC_credit_limit & vbCrLf & "Due Day: " & ComboBoxC_due_day & "th", vbOKOnly, "Data edited" 'configure user message for edit entry

    'Example of vbYesNo
    OutPut = MsgBox("Do you want to enter his data?", vbQuestion + vbYesNo, "Enter Data?")

    If OutPut = 6 Then
        'Output = 6(yes)
       MsgBox "Data has been entered", vbInformation, "Entered"
    
    If OutPut = 7 Then
        'Output = 7(no)
       MsgBox "You will now redirected to the card intake form to edit or to fully exit (to exit please press 'Cancel' button on that form)", vbInformation, "Edit/Exit"
       Exit Sub
       End If
    End If

End If
'end check if in 'edit' or 'new' mode

'''Begin DATA Entry'''
ShGE01.Range("Data_Start").Offset(TargetRow, 0).Value = TargetRow  'ref
ShGE01.Range("Data_Start").Offset(TargetRow, 1).Value = txtC_name  'name
ShGE01.Range("Data_Start").Offset(TargetRow, 2).Value = txtC_account  'acc. Number
ShGE01.Range("Data_Start").Offset(TargetRow, 3).Value = txtC_name & " " & txtC_account    'fullname concatenate
ShGE01.Range("Data_Start").Offset(TargetRow, 4).Value = ComboBoxc_status
ShGE01.Range("Data_Start").Offset(TargetRow, 5).Value = ComboBoxc_brand
ShGE01.Range("Data_Start").Offset(TargetRow, 6).Value = ComboBoxc_type
ShGE01.Range("Data_Start").Offset(TargetRow, 7).Value = txtC_interest
ShGE01.Range("Data_Start").Offset(TargetRow, 8).Value = txtC_credit_limit
ShGE01.Range("Data_Start").Offset(TargetRow, 9).Value = ComboBoxC_due_day
'''End DATA Entry'''

Unload Card_Name_UF 'close form
'MsgBox Fullname & UserMessage, 0, "Completed"   'display message box (according to mode)

End Sub

Private Sub txtC_account_Enter()

    Me.txtC_account.MaxLength = 4
    
End Sub

Private Sub txtC_account_Exit(ByVal Cancel As MSForms.ReturnBoolean)

    Dim entry       As Integer
    If Not Me.Visible Then Exit Sub
    With Me.txtC_account
        .Value = Format(Val(.Value), "0000")
        entry = Val(.Value)
    End With
    If entry = 0 Then
   lblc_account = "Please enter a 4-digit number from 0001 To 9999"
        Cancel = True
    End If
    
End Sub

Private Sub txtC_account_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

    If (KeyAscii >= 48 And KeyAscii <= 57) Then
        KeyAscii = KeyAscii
    Else
        KeyAscii = 0

    End If
    
End Sub

Private Sub txtC_name_Change()

txtC_name.Text = Application.Proper(txtC_name.Text)

End Sub

Private Sub txtC_credit_limit_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

If (KeyAscii >= 48 And KeyAscii <= 57) Or (KeyAscii = 46 Or KeyAscii = 36 Or KeyAscii = 44) Then
    KeyAscii = KeyAscii
    Else
    KeyAscii = 0
    End If
    
End Sub

Private Sub txtC_name_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
 If (KeyAscii >= 65 And KeyAscii <= 90) Or (KeyAscii >= 97 And KeyAscii <= 122) Or (KeyAscii = 46) Or (KeyAscii = 45) Or (KeyAscii = 32) Or (KeyAscii = 35) Or (KeyAscii >= 48 And KeyAscii <= 57) Then
    KeyAscii = KeyAscii
    Else
    KeyAscii = 0
    End If
End Sub

Private Sub UserForm_Initialize()
Me.Height = 409
Me.Width = 275
End Sub
 
Upvote 0
Unloading the form does not stop the routine.
And then you should start getting error messages when referring to controls which are already unloaded.
So just include Exit Sub after Unload.
 
Upvote 0
Solution
Unloading the form does not stop the routine.
And then you should start getting error messages when referring to controls which are already unloaded.
So just include Exit Sub after Unload.
it works, THANKS
 
Upvote 0

Forum statistics

Threads
1,214,826
Messages
6,121,797
Members
449,048
Latest member
greyangel23

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