+ Reply to Thread
Results 1 to 6 of 6

User Form Required Fields

Hybrid View

  1. #1
    Registered User
    Join Date
    05-22-2014
    Posts
    63

    User Form Required Fields

    I have a user form and I need the fields to be required to be filled before it can be closed. Right now I have. . .
    Private Sub BTNSave_Click()
    
    TargetRow = Range("A" & Rows.Count).End(xlUp).Row + 1
    X = TargetRow - 3
    DT = Format(Now, "mm/dd/yyyy hh:mm:ss AM/PM")
    
    
    
    Cells(TargetRow, 4).Value = TXTWhatsYourName.Text
    If TXTWhatsYourName = "" Then MsgBox ("You Must Enter A Name!")
    
    Cells(TargetRow, 7).Value = TXTDescription.Text
    If TXTDescription = "" Then MsgBox ("You Must Enter A Description!")
    
    Cells(TargetRow, 5).Value = TXTWOname.Text
    If TXTWOname = "" Then MsgBox ("You Must Enter A Work Order Number!")
    
    Cells(TargetRow, 6).Value = TXTPartsNumber.Text
    If TXTPartsNumber = "" Then MsgBox ("You Must Enter A PartNumber!")
    
    Cells(TargetRow, 8).Value = TXTPartsQTY.Text
    If TXTPartsQTY = "" Then MsgBox ("You Must Enter A Quantity!")
    
    Cells(TargetRow, 9).Value = TXTNeededbydateandtime.Text
    If TXTNeededbydateandtime = "" Then MsgBox ("You Must Enter A Date and Time!")
    
    Cells(TargetRow, 10).Value = DDUnitOnFloor.Value
    If DDUnitOnFloor = "" Then MsgBox ("You must choose from Unit on the Floor!")
    
    Cells(TargetRow, 11).Value = DDReason.Text
    If DDReason = "" Then MsgBox ("You must choose a Reason!")
    
    Cells(TargetRow, 12).Value = DDManufacturingArea.Text
    If DDManufacturingArea = "" Then MsgBox ("You must choose a Manufacturing Area!")
    
    Cells(TargetRow, 13).Value = DDDPUEntered.Text
    If DDDPUEntered = "" Then MsgBox ("Was DPU Entered?")
    
    Cells(TargetRow, 15).Value = DDDiditgotodesign.Text
    If DDDiditgotodesign = "" Then MsgBox ("Did it go to design?")
    
    Cells(TargetRow, 16).Value = TXTWhoDidDPU.Text
    If TXTWhoDidDPU = "" Then MsgBox ("Who Did DPU?")
    
    
    
    Cells(TargetRow, 1) = X
    Cells(TargetRow, 3).Value = DT
    Cells(TargetRow, 2).Interior.Color = vbGreen
    
    Me.Hide
    Unload Me
    ActiveWorkbook.Save
    
    End Sub
    I would like everything before Cells . . = X to be required before it can be closed. How is this done? Thank you!
    Last edited by hhpack3; 07-02-2014 at 09:38 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    01-19-2012
    Location
    Barrington, IL
    MS-Off Ver
    Excel 2007/2010
    Posts
    1,211

    Re: User Form Required Fields

    
    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    
    If 'yourcriteria = Not met then
    
    cancel = true
    
    Else
    Exit sub
    end if
    End Sub
    Last edited by Speshul; 07-01-2014 at 04:16 PM.

  3. #3
    Registered User
    Join Date
    05-22-2014
    Posts
    63

    Re: User Form Required Fields

    Im not exactly sure how to use that code you just put up.

  4. #4
    Valued Forum Contributor
    Join Date
    01-19-2012
    Location
    Barrington, IL
    MS-Off Ver
    Excel 2007/2010
    Posts
    1,211

    Re: User Form Required Fields

    Try this? Not sure if it's exactly what you need but you should see what I'm getting at
    Edit: Didn't realize they were all variables thought they were textboxes for some reason, make sub Public? I'm actually no good with VBA, I've just put code under this event before..

    
    Public Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    
    If TXTWhatsYourName = "" Then
    Cancel = True
    GoTo incomplete
    
    ElseIf TXTWOname = "" Then
    Cancel = True
    GoTo incomplete
    
    ElseIf TXTPartsNumber = "" Then
    Cancel = True
    GoTo incomplete
    
    ElseIf TXTPartsQTY = "" Then
    Cancel = True
    GoTo incomplete
    
    ElseIf TXTNeededbydateandtime = "" Then
    Cancel = True
    GoTo incomplete
    
    ElseIf DDUnitOnFloor = "" Then
    Cancel = True
    GoTo incomplete
    
    ElseIf DDReason = "" Then
    Cancel = True
    GoTo incomplete
    
    ElseIf DDManufacturingArea = "" Then
    Cancel = True
    GoTo incomplete
    
    ElseIf DDDPUEntered = "" Then
    Cancel = True
    GoTo incomplete
    
    
    ElseIf DDDiditgotodesign = "" Then
    Cancel = True
    GoTo incomplete
    
    ElseIf TXTWhoDidDPU = "" Then
    Cancel = True
    GoTo incomplete
    
    Else
    
    Exit Sub
    
    End If
    
    
    incomplete:
    MsgBox "Please complete form before closing"
    
    
    
    Exit Sub
    
    
    End Sub
    (Also make sure you replace UserForm in the first line with your forms name)
    Last edited by Speshul; 07-01-2014 at 04:28 PM.

  5. #5
    Registered User
    Join Date
    05-22-2014
    Posts
    63

    Re: User Form Required Fields

    I tweaked it a little but now it works! Thank You!

  6. #6
    Valued Forum Contributor
    Join Date
    01-19-2012
    Location
    Barrington, IL
    MS-Off Ver
    Excel 2007/2010
    Posts
    1,211

    Re: User Form Required Fields

    No problem, learning experience for me as well

    Make sure you mark as [SOLVED] if this issue is now resolved!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Make multiple fields required in form before save
    By tclemente in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 03-03-2014, 12:34 PM
  2. Filling form fields via worksheet dropdowns, user update via form, change form color
    By Demosthenes&Locke in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-11-2010, 08:58 AM
  3. Help required in user form
    By umayank in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-28-2009, 11:37 AM
  4. Best way to fill user form fields
    By evanzo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-23-2008, 09:41 PM
  5. Example User Form Required
    By robertguy in forum Excel General
    Replies: 0
    Last Post: 10-04-2005, 09:25 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1