+ Reply to Thread
Results 1 to 4 of 4

Userform - Stop Command button working if a textbox is blank?

Hybrid View

revenge4ash89 Userform - Stop Command... 01-10-2012, 05:25 AM
royUK Re: Userform - Stop Command... 01-10-2012, 05:37 AM
revenge4ash89 Re: Userform - Stop Command... 01-10-2012, 05:42 AM
revenge4ash89 Re: Userform - Stop Command... 01-10-2012, 05:52 AM
  1. #1
    Registered User
    Join Date
    11-16-2011
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    34

    Userform - Stop Command button working if a textbox is blank?

    Hi There,

    I'm not sure if this is possible, but what I'm trying to do is create an error message that will prevent a user selecting the command button in a userform if a text bos is empty.

    I've attached a spreadsheet with a simple, example userform. What I would like to do is stop the user adding the record if the "service date" text box is empty. Idealy I would like an error message to display something like "please enter service date". Is this possible to do?

    Below is the code.

    Option Explicit
    
    Private Sub Auto_Open()
    UserForm1.Show
    End Sub
    
    
    Private Sub CommandButton1_Click()
    Dim iRow As Long
    Dim ws As Worksheet
    Dim Rng As Range
    Set ws = Worksheets("DataSource")
    Set Rng = ws.Range("A2")
    
    
                iRow = ws.Cells(Rows.Count, Rng.Column).End(xlUp).Row
                iRow = IIf(iRow < Rng.Row, Rng.Row, iRow + 1)
    
    ws.Cells(iRow, 1) = name1.Value
    ws.Cells(iRow, 2) = Vehicle.Value
    ws.Cells(iRow, 3) = date1.Value
    
    
    End Sub
    
    
    Private Sub UserForm_Initialize()
    
    Dim ws As Worksheet
    Set ws = Worksheets("DataSource")
    Dim iRow As Long
    iRow = Selection.Row
    
    UserForm.name1.Text = ws.Cells(iRow, 1)
    UserForm.Vehicle.Text = ws.Cells(iRow, 2)
    UserForm.date1.Text = ws.Cells(iRow, 3)
    
    End Sub
    Attached Files Attached Files
    Last edited by revenge4ash89; 01-10-2012 at 05:52 AM.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Userform - Stop Command button working if a textbox is blank?

    Why are you pre-populating the TextBoxes?

    Private Sub CommandButton1_Click()
    Dim iRow As Long
    Dim ws As Worksheet
    Dim Rng As Range
    Set ws = Worksheets("DataSource")
    Set Rng = ws.Range("A2")
    
    If Me.name1 <> Empty And Me.Vehicle <> Empty And Me.date1 <> Empty Then
    
                iRow = ws.Cells(Rows.Count, Rng.Column).End(xlUp).Row
                iRow = IIf(iRow < Rng.Row, Rng.Row, iRow + 1)
    
    ws.Cells(iRow, 1) = name1.Value
    ws.Cells(iRow, 2) = Vehicle.Value
    ws.Cells(iRow, 3) = date1.Value
    Else
    MsgBox "Form incomplete", vbCritical, "Input required"
    End If
    End Sub
    Last edited by royUK; 01-10-2012 at 07:10 AM.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Registered User
    Join Date
    11-16-2011
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    34

    Re: Userform - Stop Command button working if a textbox is blank?

    I'm pre-populating the textboxes because I have large amount of record information which can be easily edited through the userform . This spreadsheet is just an example.

  4. #4
    Registered User
    Join Date
    11-16-2011
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    34

    Re: Userform - Stop Command button working if a textbox is blank?

    Thank you so much for the speedy response RoyUK! That's worked perfectly.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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