Data validation UserForm Text Only

Karmadyl

New Member
Joined
Feb 22, 2019
Messages
4
Good day,

I am currently trying to validate data using the user form. What I am trying to accomplish is to make mandatory fields filled before the user is able to submit the entered data. I am trying to use something like IsNumeric to validate, but can't find something similar regarding strings

Rich (BB code):
Private Sub OnlyNumbers()If TypeName(Me.ActiveControl) = "TextBox" Then


        With Me.ActiveControl


            If Not IsNumeric(.Value) And .Value <> vbNullString Then


                MsgBox "Sorry, only numerical data may be entered"


                .Value = vbNullString


            End If


        End With


    End If


End Sub


I've tried something like:


Rich (BB code):
Private Sub FreightFowarder_Change()
If FreigtFowarder.Text = "" Then
MsgBox "Please enter a Freight Fowarder"
Exit Sub
End if
End Sub 


But the data was still inserted in my table. I would like that the data doesn't get submitted until all mandatory fields are filled.

What should I do? Here's my command button code :

Rich (BB code):
Private Sub CommandButton1_Click()    
    Dim R As Long
    Dim Rng As Range
    Dim ws As Worksheet


    Set ws = Sheets("OutgoingBagIssues")


    With ws.ListObjects("ExhibitorList")
        Set Rng = .ListColumns(1).Range
        R = Rng.Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False).Row
        .ListRows(R).Range.Cells(1, 1).Select
        'freight fowarder
        .ListRows(R).Range.Cells(1, 1).Value = Me.FreightFowarder.Value
        'Mission
        .ListRows(R).Range.Cells(1, 2).Value = Me.Mission.Value
        'Classification
        .ListRows(R).Range.Cells(1, 3).Value = Me.Classification.Value
        'Total Bags Shipment
        .ListRows(R).Range.Cells(1, 4).Value = Me.TotalShipment.Value
        'Numbers of Bags Affected
        .ListRows(R).Range.Cells(1, 5).Value = Me.BagsAffected.Value
        'AWBs
        .ListRows(R).Range.Cells(1, 6).Value = Me.AWBs.Value
        'Bag Numbers
        .ListRows(R).Range.Cells(1, 7).Value = Me.BagNumbers.Value
        'Issues
        .ListRows(R).Range.Cells(1, 8).Value = Me.Issues.Value
        'Date
        .ListRows(R).Range.Cells(1, 9).Value = Me.ManualDate.Value
        'Extra notes
        .ListRows(R).Range.Cells(1, 10).Value = Me.ExtraNotes.Value
        'Submitter
        .ListRows(R).Range.Cells(1, 11).Value = Me.Submitter.Value
        'IBP #
        .ListRows(R).Range.Cells(1, 12).Value = Me.InfoBank.Value
'        .ListRows.Add AlwaysInsert:=True
    
    Dim xOutApp As Object
    Dim xOutMail As Object
    Dim xMailBody As String
    On Error Resume Next
    Set xOutApp = CreateObject("Outlook.Application")
    Set xOutMail = xOutApp.CreateItem(0)
    xMailBody = "Freight Fowarder: " & Me.FreightFowarder.Value & vbNewLine & _
                "Mission: " & Me.Mission.Value & vbNewLine & _
                "Classification: " & Me.Classification.Value & vbNewLine & _
                "Total Shipment (Bags): " & Me.TotalShipment.Value & vbNewLine & _
                "Number Of Bags Affected: " & Me.BagsAffected.Value & vbNewLine & _
                "Bag Numbers: " & Me.BagNumbers.Value & vbNewLine & _
                "AWBs: " & Me.AWBs.Value & vbNewLine & _
                "Issues: " & Me.Issues.Value & vbNewLine & _
                "Date: " & Me.ManualDate & vbNewLine & _
                "Notes: " & Me.ExtraNotes & vbNewLine & _
                "Submitter: " & Me.Submitter & vbNewLine & _
                "Info Bank Path: " & Me.InfoBank.Value
                  On Error Resume Next
    With xOutMail
        .To = "" 'Email Needs to be added keeping this as a reference
        .CC = ""
        .BCC = ""
        .Subject = "Diplomatic Bag Issues"
        .Body = xMailBody
        .Send
    End With
    On Error GoTo 0
    Set xOutMail = Nothing
    Set xOutApp = Nothing




    End With
    Call Userform_Initialize
End Sub


Best regards,

 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Good day SilentWolf, I will surely listen to the above! Thanks :)

In the meantime, i've figured a way to validate my TextBoxes and ComboBoxes with the following code (pretty sure that is not efficient however) .This way I make sure no numerical data has been entered in my strings only TextBoxes and ComboBoxes (However only at the beginning) :

Code:
Private Sub TextOnlyMission()If TypeName(Me.ActiveControl) = "TextBox" Then
    With Me.ActiveControl
        If Mission.Value = IsNumeric(.Value) Then
        MsgBox "No numerical data may be entered, please enter the mission's acronym"
        End If
        End With
        End If
End Sub

And to make sure all mandatory fields are filled, I only had to add Exit Sub in order for my form to not fill my table before all fields are filled :

Code:
If Mission.Value = "" Then
        MsgBox "Please enter the mission's acronym"
        Mission.BackColor = RGB(255, 0, 0)
        Exit Sub
        End If


Best regards,
 
Upvote 0

Forum statistics

Threads
1,214,397
Messages
6,119,273
Members
448,883
Latest member
fyfe54

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