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
Why is the 'add client' button visible if not all compulsory textboxes have been filled ?
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.
Here's an example to adapt
Hope that helps.
RoyUK
--------
If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need
For Excel Tips & Solutions, free examples and tutorials why not check out my downloads
New members please read & follow the Forum Rules
Remember to mark your questions Solved and rate the answer(s)
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.
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
"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.
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
Hope that helps.
RoyUK
--------
If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need
For Excel Tips & Solutions, free examples and tutorials why not check out my downloads
New members please read & follow the Forum Rules
Remember to mark your questions Solved and rate the answer(s)
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.
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 12:03 PM.
Dom's code should work if you added it correctly, but it would be simpler to loop through all textboxes as snb did.
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.
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.
Last edited by royUK; 02-08-2011 at 03:21 AM.
Hope that helps.
RoyUK
--------
If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need
For Excel Tips & Solutions, free examples and tutorials why not check out my downloads
New members please read & follow the Forum Rules
Remember to mark your questions Solved and rate the answer(s)
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks