Implement Control or Checks on Data entry in Textbox

hhap

New Member
Joined
Aug 13, 2002
Messages
5
I have a userform with five(5) textboxes that must be filled with figures based on entry in another textbox (Me.txtAmtRemainToSplit.Text). I want to implement some checks such that if the first textbox (Me.txtSplitAmt1st.Text ) is not filled the user cannot fill the 2nd textbox. If 2nd textbox is not filled the 3rd textbox cannot be filled and so on. The figures in the textbox are used to populate cells in a range selected by user . In other words there should not be empty texbox between any entries. I have implemented the checks or controls below. When the userform initializes the 3rd texboxes to the 5th are locked or disabled. As and when the 2nd textbox is filled , the next textbox, 3rd one is enabled. The 4th textbox is enabled the same way, so is the 5th one . The issue is that when any of the disabled texboxes are enabled, the user may fill them and delete the entry in the 1st or 2nd texboxes. I have tried several ways to go around this but I have not found a way out. Any suggestions are welcome.

Note T1, T2, T3 etc have already been defined on top of the private module

Sub MultipleReceiptCtrl()

T1 = Val(Me.txtSplitAmt1st.Text)
T2 = Val(Me.txtSplitAmt2nd.Text)
T3 = Val(Me.txtSplitAmt3rd.Text)
T4 = Val(Me.txtSplitAmt4th.Text)
T5 = Val(Me.txtSplitAmt5th.Text)

Select Case Val(Me.txtAmtRemainToSplit.Text)

Case Is <> 0
MsgBox "Multiple Receipts must be equal to amount to Split. Remainder Must be Zero"
Me.txtSplitAmt2nd.SetFocus

Case Is = 0
If Val(Me.txtSplitAmt1st.Text) = Val(Me.txtAmtToSplit.Text) Then

MsgBox "There is no Multiple Receipts. Fill the 2nd Box"
Me.txtSplitAmt2nd.SetFocus
End If

Case Else

Select Case True

Case (T4 = "" Or T3 = "" Or T2 = "" Or T1 = "")
If T5 > 0 Then

MsgBox " The top empty box(es) should be filled before this box"
Exit Sub

End If

Case (T3 = "" Or T2 = "" Or T1 = "")

If T4 > 0 Then
MsgBox " The top empty box(es) should be filled before this box"
Exit Sub

End If

Case (T2 = "" Or T1 = "")

If T3 > 0 Then
MsgBox " The top empty box(es) should be filled before this box"
Exit Sub

End If

Case T1 = ""

If T2 > 0 Then
MsgBox " The top empty box should be filled before this box"
Exit Sub

End If

End Select

End Select
End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Here’s my take on what you need:

To test:

Create form with:

5 text boxes: txtSplitAmt1, txtSplitAmt2, txtSplitAmt3, txtSplitAmt4, txtSplitAmt5

1 Controlbutton: cbOK

Put the following code in the form’s module:

Test

Let me know the results


Code:
Option Explicit

Private Sub txtSplitAmt1_Change()
    setTBenabled
End Sub
Private Sub txtSplitAmt2_Change()
    setTBenabled
End Sub
Private Sub txtSplitAmt3_Change()
    setTBenabled
End Sub
Private Sub txtSplitAmt4_Change()
    setTBenabled
End Sub
Private Sub txtSplitAmt5_Change()
    setTBenabled
End Sub
Sub setTBenabled()
    Dim tbs, i
    tbs = Array(Me.txtSplitAmt1, Me.txtSplitAmt2, Me.txtSplitAmt3, Me.txtSplitAmt4, Me.txtSplitAmt5)
    For i = UBound(tbs) To 1 Step -1
        tbs(i).Enabled = IIf(tbs(i - 1).Enabled = False Or Len(tbs(i - 1)) = 0, False, True)
    Next i
    Me.cbOK.Enabled = IIf(Len(tbs(4)) > 0 And tbs(4).Enabled = True, True, False)
End Sub
 
Upvote 0
Hi Tlowry, thanks for replying to my question and very sorry for the delay in giving you my feedack. I was under pressure to make it work the next day. I implemented your code , however it disables the command button such that even when you have filled the textboxes you cannot click the button to enter the figures in the sheet.

Finally I decided to use the long method below but it worked.

T1 = Val(Me.txtSplitAmt1st.Value)
T2 = Val(Me.txtSplitAmt2nd.Value)
T3 = Val(Me.txtSplitAmt3rd.Value)
T4 = Val(Me.txtSplitAmt4th.Value)
T5 = Val(Me.txtSplitAmt5th.Value)


If T5 > 0 Then

If T4 = 0 Or T3 = 0 Or T2 = 0 Or T1 = 0 Then

MsgBox " There is a blank field(s) before the last one you filled. Please fill it "
Exit Sub

End If
End If

If T4 > 0 Then
If T3 = 0 Or T2 = 0 Or T1 = 0 Then

MsgBox " There is a blank field(s) before the last one you filled. Please fill it "
Exit Sub

End If
End If

If T3 > 0 Then
If T2 = 0 Or T1 = 0 Then

MsgBox "There is a blank field(s) before the last one you filled. Please fill it "
Exit Sub

End If
End If

If T2 > 0 Then
If T1 = 0 Then

MsgBox "There is a blank field(s) before the last one you filled. Please fill it"
Exit Sub

End If

End If
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,990
Messages
6,175,815
Members
452,672
Latest member
missbanana

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