[VBA] : Checking for the presence of a value in a textbox

Solved
anton26 Posted messages 33 Status Member -  
anton26 Posted messages 33 Status Member -
Hello,

I created a dynamic userform with a number n of dynamic textboxes. When I click on a validate button, the entered values appear on an Excel sheet. I would like an error message to appear if one or more textboxes do not have values. How could I program this?

Thank you in advance

Configuration: Windows XP / Firefox 13.0

10 answers

  1. f894009 Posted messages 17417 Registration date   Status Member Last intervention   1 717
     
    Hello,

    'Test example on dynamically created controls
    'in UserForm
    Private Sub CommandButton1_Click() Dim Cont As Control 'Loop through UF For Each Cont In Me.Controls 'Check if Control is a TextBox and empty If TypeOf Cont Is MSForms.TextBox And Cont.Object.Value = "" Then MsgBox "Warning! Missing Info" & Cont.Name End If Next Cont End Sub 
    0
  2. anton26 Posted messages 33 Status Member
     
    Hello,
    Thank you for this program.
    The problem is that I can't adapt it to my button which is created dynamically.
    0
  3. f894009 Posted messages 17417 Registration date   Status Member Last intervention   1 717
     
    Hello,
    You should have said that from the start!! I'm looking for the code to intercept a dynamically created button click.

    See you!
    0
  4. f894009 Posted messages 17417 Registration date   Status Member Last intervention   1 717
     
    Re,

    Retrieve the file and hang on

    https://www.commentcamarche.net/faq/31279-vba-ajouter-des-controles-dynamiquement-dans-un-userform

    Good luck
    0
  5. lermite222 Posted messages 9042 Status Contributor 1 199
     
    Hello,
    Hello fxx :-)
    He probably doesn't need such complicated code, with
    this demo it will be easier.
    Use your function.
    Use the demo to dynamically add code to the button.
    Of course, adapt Combo to the button's name.
    See you!
    Edit: But there might be an even simpler way, show the code used to add
    the dynamic controls.
    EDIT: I couldn't find that one anymore.

    If you hit a pot and it sounds hollow, it’s not necessarily the pot that’s empty. ;-)(Confucius)
    NOTE: I do not respond to private messages for technical questions.
    0
    1. f894009 Posted messages 17417 Registration date   Status Member Last intervention   1 717
       
      Hello and happy new year hermit,

      I only had this link on hand! But, with a bit of thought, it was doable.

      Bye
      0
    2. lermite222 Posted messages 9042 Status Contributor 1 199
       
      Re,
      It's already the 15th... and I don't think about it much anymore but... thank you and happy new year to you too.
      0
  6. anton26 Posted messages 33 Status Member
     
    ```html Here is the code that allows me to dynamically create my textboxes as well as the code that allows me to place the values in a sheet. Currently, with the code I wrote, it doesn’t work. I would like the error message to appear if one or more textboxes have not been filled in.

    For i = 1 To n Set Obj = Me.Controls.Add("forms.Textbox.1") 'Create n textboxes With Obj .Left = 90 'Position of the textboxes from the left edge of the userform .Top = 25 * i + 10 'Spacing between each textbox .Width = 100 'Length of the textbox .Height = 18 'Height of the textbox '.BackColor = &HC0FFFF 'Background color .BorderStyle = 0 'Border style (0 or 1) .FontName = "calibri" 'Font type .FontBold = False 'Bold text .FontSize = 10 'Font size .Enabled = True 'Ability to write in the box End With Next i Dim Bouton As MSForms.CommandButton 'Adding a validate button Dim Cl As ClasBT Set Collect = New Collection Set CollectBT = New Collection Set Bouton = Me.Controls.Add("Forms.CommandButton.1", "Bt1", True) CollectBT.Add Bouton, "1" 'Add to the object collection Set Cl = New ClasBT 'Add to the class collection Set Cl.GroupBoutons = Bouton Collect.Add Cl Public Sub ControlClick(nom As String, num As Long) 'Validate button control Dim Ctrl As Control Dim CtrlName As String Dim CtrlIndex As Integer Dim ctrlText As String For Each Ctrl In Me.Controls CtrlName = Ctrl.Name If InStr(CtrlName, "TextBox") = 1 Then 'Get the textbox number CtrlIndex = Val(Mid(CtrlName, 8)) 'MsgBox CtrlIndex Worksheets("trimestre 1").Cells(CtrlIndex + 9, 1).Value = Ctrl.Text 'Positioning the textboxes in a sheet (cells(row,column)) Worksheets("trimestre 2").Cells(CtrlIndex + 9, 1).Value = Ctrl.Text 'Positioning the textboxes in a sheet (cells(row,column)) Worksheets("trimestre 3").Cells(CtrlIndex + 9, 1).Value = Ctrl.Text 'Positioning the textboxes in a sheet (cells(row,column)) Else End If Next For i = 1 To n If Worksheets("trimestre 1").Cells(i + 9, 1).Value = "" Then 'Check the values entered in the textboxes Reponse = MsgBox("Please fill in each box." & Chr(10) & Chr(10) & "To complete your list click OK." & Chr(10) & "To return to the homepage click Cancel.", vbExclamation + vbOKCancel, "Error") If Reponse = vbCancel Then 'If you click Cancel Unload UserForm1 demarrage.Show Else End If End If Next i 


    Here is my class module (ClasBT) for my validation button:

    Option Explicit Public WithEvents GroupBoutons As MSForms.CommandButton Private Sub GroupBoutons_Click() Call UserForm1.ControlClick(GroupBoutons.Caption, GroupBoutons.Tag) End Sub
    ```
    0
  7. lermite222 Posted messages 9042 Status Contributor 1 199
     
    Huumm, I recognize a lot of parts of your code... :-)
    But I see that you haven't quite understood everything yet, that's normal, it's not easy.
    Do you only have one button? If so, there's no need for the collection of objects. (Class module)
    The simplest... post an example of your spreadsheet, I'll see about making it work.

    If you bump into a pot and it sounds hollow, it's not necessarily the pot that's empty. ;-)(Confucius)
    NOTE: I do not respond to private messages for technical questions.
    0
  8. anton26 Posted messages 33 Status Member
     
    Yes, it is quite possible that you will recognize it, but it is true that I am a beginner, so I don't understand everything.
    For the button, I created it dynamically because I want it to automatically place itself next to the last textbox of the userform.
    You will find my file attached if you want to take a look.
    Thank you and have a good evening.
    http://dl.free.fr/getfile.pl?file=/pb9pbCSv
    0
  9. lermite222 Posted messages 9042 Status Contributor 1 199
     
    Re,
    A workbook with your TextBox
    I saved the data on Sheet1, since there aren't individual sheets for each student.
    Do as stated on Sheet1.
    The button only validates when all the textBoxes are filled in.
    I added a lot of notes so that you can understand the purpose of the collections.
    You say..
    See you later
    If you bump into a pot and it sounds hollow, it's not necessarily the pot that's empty. ;-)(Confucius)
    NOTE: I do not respond to private messages for technical questions.
    0
  10. anton26 Posted messages 33 Status Member
     
    Hello,
    Thank you very much, it works perfectly. I'm starting to understand how collections work thanks to your comments. I hadn't thought of locking the validate button; that's a very good idea!
    Thank you for the time you dedicated to it!
    0