+ Reply to Thread
Results 1 to 15 of 15

Dont Allow Empty Userform Fields

Hybrid View

  1. #1
    Registered User
    Join Date
    02-07-2011
    Location
    Leeds
    MS-Off Ver
    Excel 2003-2007
    Posts
    8

    Exclamation Dont Allow Empty Userform Fields

    I have a userform and i need to make sure that when the user presses the add client button, it only adds the info from the userform to the sheet if all fields are filled.

    The code that i have at the minute doesnt allow you to proceed if a phone number has been added but i dont know how to make it work for multiple fields.

    Private Sub add_Click()
    
       If phonetxt.Value = "" Then
          MsgBox "Please enter a Phone Number"
         Exit Sub
         
    Else
    
    Dim FirstBlankRow As Long
    
    FirstBlankRow = Cells(Rows.Count, "M").End(xlUp)(2).Row
    
    Sheets("List").Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = Me.departmenttxt.Value
    Sheets("List").Range("B" & Rows.Count).End(xlUp).Offset(1, 0) = Me.firsttxt.Value
    Sheets("List").Range("C" & Rows.Count).End(xlUp).Offset(1, 0) = Me.lasttxt.Value
    Sheets("List").Range("D" & Rows.Count).End(xlUp).Offset(1, 0) = Me.emailtxt.Value
    Sheets("List").Range("E" & Rows.Count).End(xlUp).Offset(1, 0) = Me.phonetxt.Value
    Sheets("List").Range("F" & Rows.Count).End(xlUp).Offset(1, 0) = Me.notxt.Value
    Sheets("List").Range("G" & Rows.Count).End(xlUp).Offset(1, 0) = Me.daytxt.Value
    Sheets("List").Range("H" & Rows.Count).End(xlUp).Offset(1, 0) = Me.monthtxt.Value
    Sheets("List").Range("I" & Rows.Count).End(xlUp).Offset(1, 0) = Me.yeartxt.Value
    Sheets("List").Range("J" & Rows.Count).End(xlUp).Offset(1, 0) = Me.timeslottxt.Value
    Sheets("List").Range("L" & Rows.Count).End(xlUp).Offset(1, 0) = Me.nowtxt.Value
    Sheets("List").Range("M" & Rows.Count).End(xlUp).Offset(1, 0) = Me.timetxt.Value
    Sheets("List").Range("K" & Rows.Count).End(xlUp).Offset(1, 0) = notesform.notetxt.Value
    Sheets("List").Range("N" & Rows.Count).End(xlUp).Offset(1, 0) = vipbtn.Value
    Sheets("List").Range("O" & Rows.Count).End(xlUp).Offset(1, 0) = monobtn.Value
    Sheets("List").Range("P" & Rows.Count).End(xlUp).Offset(1, 0) = bookedbox.Value
    
    
        End If
        
        Unload Me
        
        home.Show
            
    Unload notesform
    End Sub

    Any help will be greatly appreciated

  2. #2
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Dont Allow Empty Userform Fields

    Why is the 'add client' button visible if not all compulsory textboxes have been filled ?



  3. #3
    Registered User
    Join Date
    02-07-2011
    Location
    Leeds
    MS-Off Ver
    Excel 2003-2007
    Posts
    8

    Re: Dont Allow Empty Userform Fields

    The reason that the button is available, is that i dont know how to make it not available when all the fileds are not filled.

  4. #4
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Dont Allow Empty Userform Fields

    Something like this:

    Private Sub add_Click()
    
    If phonetxt.Value = "" Then
        MsgBox "Please enter a Phone Number"
        Exit Sub
    ElseIf othertxt.Value = "" Then
        MsgBox "Please enter some other stuff"
        Exit Sub
    ElseIf anothertxt.Value = "" Then
        MsgBox "Please enter even more stuff"
        Exit Sub
    End If
        
    Dim FirstBlankRow As Long
    
    FirstBlankRow = Cells(Rows.Count, "M").End(xlUp)(2).Row
    
    Sheets("List").Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = Me.departmenttxt.Value
    Sheets("List").Range("B" & Rows.Count).End(xlUp).Offset(1, 0) = Me.firsttxt.Value
    Sheets("List").Range("C" & Rows.Count).End(xlUp).Offset(1, 0) = Me.lasttxt.Value
    Sheets("List").Range("D" & Rows.Count).End(xlUp).Offset(1, 0) = Me.emailtxt.Value
    Sheets("List").Range("E" & Rows.Count).End(xlUp).Offset(1, 0) = Me.phonetxt.Value
    Sheets("List").Range("F" & Rows.Count).End(xlUp).Offset(1, 0) = Me.notxt.Value
    Sheets("List").Range("G" & Rows.Count).End(xlUp).Offset(1, 0) = Me.daytxt.Value
    Sheets("List").Range("H" & Rows.Count).End(xlUp).Offset(1, 0) = Me.monthtxt.Value
    Sheets("List").Range("I" & Rows.Count).End(xlUp).Offset(1, 0) = Me.yeartxt.Value
    Sheets("List").Range("J" & Rows.Count).End(xlUp).Offset(1, 0) = Me.timeslottxt.Value
    Sheets("List").Range("L" & Rows.Count).End(xlUp).Offset(1, 0) = Me.nowtxt.Value
    Sheets("List").Range("M" & Rows.Count).End(xlUp).Offset(1, 0) = Me.timetxt.Value
    Sheets("List").Range("K" & Rows.Count).End(xlUp).Offset(1, 0) = notesform.notetxt.Value
    Sheets("List").Range("N" & Rows.Count).End(xlUp).Offset(1, 0) = vipbtn.Value
    Sheets("List").Range("O" & Rows.Count).End(xlUp).Offset(1, 0) = monobtn.Value
    Sheets("List").Range("P" & Rows.Count).End(xlUp).Offset(1, 0) = bookedbox.Value
        
    Unload Me
        
    home.Show
            
    Unload notesform
    End Sub

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  5. #5
    Registered User
    Join Date
    02-07-2011
    Location
    Leeds
    MS-Off Ver
    Excel 2003-2007
    Posts
    8

    Re: Dont Allow Empty Userform Fields

    Quote Originally Posted by Domski View Post
    Something like this:

    Private Sub add_Click()
    
    If phonetxt.Value = "" Then
        MsgBox "Please enter a Phone Number"
        Exit Sub
    ElseIf othertxt.Value = "" Then
        MsgBox "Please enter some other stuff"
        Exit Sub
    ElseIf anothertxt.Value = "" Then
        MsgBox "Please enter even more stuff"
        Exit Sub
    End If
        
    Dim FirstBlankRow As Long
    
    FirstBlankRow = Cells(Rows.Count, "M").End(xlUp)(2).Row
    
    Sheets("List").Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = Me.departmenttxt.Value
    Sheets("List").Range("B" & Rows.Count).End(xlUp).Offset(1, 0) = Me.firsttxt.Value
    Sheets("List").Range("C" & Rows.Count).End(xlUp).Offset(1, 0) = Me.lasttxt.Value
    Sheets("List").Range("D" & Rows.Count).End(xlUp).Offset(1, 0) = Me.emailtxt.Value
    Sheets("List").Range("E" & Rows.Count).End(xlUp).Offset(1, 0) = Me.phonetxt.Value
    Sheets("List").Range("F" & Rows.Count).End(xlUp).Offset(1, 0) = Me.notxt.Value
    Sheets("List").Range("G" & Rows.Count).End(xlUp).Offset(1, 0) = Me.daytxt.Value
    Sheets("List").Range("H" & Rows.Count).End(xlUp).Offset(1, 0) = Me.monthtxt.Value
    Sheets("List").Range("I" & Rows.Count).End(xlUp).Offset(1, 0) = Me.yeartxt.Value
    Sheets("List").Range("J" & Rows.Count).End(xlUp).Offset(1, 0) = Me.timeslottxt.Value
    Sheets("List").Range("L" & Rows.Count).End(xlUp).Offset(1, 0) = Me.nowtxt.Value
    Sheets("List").Range("M" & Rows.Count).End(xlUp).Offset(1, 0) = Me.timetxt.Value
    Sheets("List").Range("K" & Rows.Count).End(xlUp).Offset(1, 0) = notesform.notetxt.Value
    Sheets("List").Range("N" & Rows.Count).End(xlUp).Offset(1, 0) = vipbtn.Value
    Sheets("List").Range("O" & Rows.Count).End(xlUp).Offset(1, 0) = monobtn.Value
    Sheets("List").Range("P" & Rows.Count).End(xlUp).Offset(1, 0) = bookedbox.Value
        
    Unload Me
        
    home.Show
            
    Unload notesform
    End Sub

    Dom

    I dont think that will work as it needs to be told to look at all the fields before allowing the sheet to be populated.

  6. #6
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Dont Allow Empty Userform Fields

    Mine was just an example and could be extended for as many fields as required.

    snb's suggestion is a good one to only make the button available once the fields are completed. I'm sure he'll provide you with an example of how to achieve this if you want.

    I'm off home now.

    Dom

  7. #7
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Dont Allow Empty Userform Fields

    Private departmenttxt_Change()
      Add.visible=department.text<>""
      if add.visible then A_check
    end sub
    
    Private firsttxt_Change()
      Add.visible=first.text<>""
      if add.visible then A_check
    end sub
    
    Private lasttxt_Change()
      Add.visible=last.text<>""
      if add.visible then A_check
    end sub
    
    Private A_check()
     for each ct in controls
       if typename(ct)="Textbox" and ct.text="" then 
         add.visible=false
         end sub
       end if
     next
    End sub
    
    --------------------------
    
    Private Sub add_Click()
      Cells(Rows.Count, 12).End(xlUp).offset(2,-11).resize(,16)=array(departmenttxt.Value,firsttxt.Value,lasttxt.Value,emailtxt.Value,phonetxt.Value,notxt.Value,daytxt.Value,monthtxt.Value,Me.yeartxt.Value,timeslottxt.Value,nowtxt.Value,timetxt.Value,notesform.notetxt.Value,vipbtn.Value,monobtn.Value,bookedbox.Value)
    End Sub

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

    Re: Dont Allow Empty Userform Fields

    Here's an example to adapt
    Attached Files Attached Files
    Hope that helps.

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

    Free DataBaseForm example

  9. #9
    Registered User
    Join Date
    02-07-2011
    Location
    Leeds
    MS-Off Ver
    Excel 2003-2007
    Posts
    8

    Re: Dont Allow Empty Userform Fields

    Quote Originally Posted by royUK View Post
    Here's an example to adapt
    I dont think that this is right for me.

    The code that i have works but only for one field. I dont know how to make it work for multiple fields.

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

    Re: Dont Allow Empty Userform Fields

    Quote Originally Posted by Adam Hewitt View Post
    I dont think that this is right for me.

    The code that i have works but only for one field. I dont know how to make it work for multiple fields.
    Why it loops through controls & checks if they are complete. You can set the CommandButton's Visible or preferably Enabled Property according to the Function results

  11. #11
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,985

    Re: Dont Allow Empty Userform Fields

    Personally I would just loop through all the textboxes and check them before proceeding; if you want to disable the button instead, I would use its enabled property rather than have it suddenly appear or disappear.
    Everyone who confuses correlation and causation ends up dead.

  12. #12
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Dont Allow Empty Userform Fields

    I want tot warn all helpers:

    After 3 posts in this forum Adam Hewitt found it necessary to denunciate my question (that contained a solution implicitly) by a distraction to my reputation, adding 'Doesnt help answer the question'.
    I categorize this guy in the group of the 'too shortly fused'.
    Look for instance how gently he denounces other solutions.

  13. #13
    Registered User
    Join Date
    02-07-2011
    Location
    Leeds
    MS-Off Ver
    Excel 2003-2007
    Posts
    8

    Re: Dont Allow Empty Userform Fields

    i have tried the else if code that domski suggested and it just falls over and the debugger launches.

    I want it to look through all the fields and make sure that they are filled. if i could get the add button to appear when all the fields have been filed then that would be great but i dont know how to do that. As i am very new to this and quite basic at the minute any code that is posted, i would appreciate it if what the code is doing is explained.

    The best way that i can see this being achieved is if the code on the add button is looking through all the fields and if one of the fields is empty then do not allow the information to be added to the sheet. I would like the message box to say, for example, "please enter an email address and phone number".

    I want all empty fields to be named in the error message.
    Last edited by Adam Hewitt; 02-07-2011 at 01:03 PM.

  14. #14
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,985

    Re: Dont Allow Empty Userform Fields

    Dom's code should work if you added it correctly, but it would be simpler to loop through all textboxes as snb did.

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

    Re: Dont Allow Empty Userform Fields

    Adam

    Giving negative rep because you either don't understand a solution is no way to get help.

    I've reversed the rep

    You seem to have an idea of what you need to do, but won't check suggestions that differ slightly.
    Attached Files Attached Files
    Last edited by royUK; 02-08-2011 at 04:21 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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