3

Bottom Line Fix: Changing my dynamicly named textbox to have a _ separator between the column and row to get rid of the ambiguity of the names.

Previous Code:

Set cCntrl = PickTicketForm.Controls.Add("Forms.TextBox.1", "PalletNumber" & i & r, True)

Fix:
Set cCntrl = PickTicketForm.Controls.Add("Forms.TextBox.1", "PalletNumber" & i & "_" & r, True)


I have a userform and it has 15 text boxes columns by X rows (dynamic).

The user inputs numbers into the text boxes. Then I want them to hit a Print button on the user form to run the PrintLabel() sub and put those values into a spreadsheet vertically (B24:Bxx). Then have it print out the spreadsheet and return to the userform.

My issue is I can't seem to get the values from the textboxes.

The textbox names are in an multi-dimensional array style format:

PalletNumber & "row" & "column"

So the first row would be PalletNumber0_0 through PalletNumber0_15. The next row will be PalletNumber1_0 to PalletNumber1_15.

Update:

The user enters the value "1234" into a textbox and clicks "Look Up" to run lookup(). This then searches a spreadsheet for the number and gets all the rows that match and puts them into the userform.

Here is the code snippet

For Each c In Worksheets("Sheet1").range("A2:A" & iRowCount)
  If c.value = OrderNumber Then
    ReDim Preserve aGetData(6, i)

    For a = 0 To 6 'Change this for total of columns Our first index will hold each col of data that is why
                   'it is set to 0 (arrays start at a base of zero, so
                   '0,1,2,3,4,5 will be each col(A,B,C).
      aGetData(a, i) = c.Offset(0, a) 'This gets each value from col A,B and C

    Next a


      'Get the data and set it into variables.
      ItemNumber = aGetData(5, i)
      ItemQty = aGetData(2, i)

      'Create "ItemQuantity" text box.
       Set cCntrl = PickTicketForm.Controls.Add("Forms.Label.1", "ItemQuantity" & i, True)
        With cCntrl
            .Caption = ItemQty
            .Width = 85
            .Height = 18
            .Top = 86 + (i * 20)  
            .Left = 40
            .TextAlign = 1 'Left
            .Font.Name = "Arial Black"
            .Font.Size = "10"
            .BackColor = BackgroundColor
        End With

      'Create "ItemNumber" box
       Set cCntrl = PickTicketForm.Controls.Add("Forms.Label.1", "ItemNumber" & i, True)
        With cCntrl
            .Caption = ItemNumber
            .Width = 340
            .Height = 18
            .Top = 86 + (i * 20)
            .Left = 86
            .TextAlign = 1 'Left
            .Font.Name = "Arial Black"
            .Font.Size = "10"
            .BackColor = BackgroundColor
        End With

    'Create each inputbox for the pallets.
    For r = 0 To 14
        Set cCntrl = PickTicketForm.Controls.Add("Forms.TextBox.1", "PalletNumber" & i & "_" & r, True)
            With cCntrl
                .Width = 28
                .Height = 18
                .Top = 86 + (i * 20)
                .Left = 354 + (r * 30) 
                .TextAlign = 1 'Left
                .Font.Name = "Arial Black"
                .Font.Size = "10"
                .BackColor = BackgroundColor
                .Text = i & r
            End With
    Next r               

    i = i + 1 'Increment for array in case we find another order number
              'Our second index "aGetData(index1,index2) is being resized
              'this represents each order number found on the sheet

    LineCount = LineCount + 1

    'Set the scroll bar height for the final form.
    ScrollBarHeight = 150 + (i * 20)

  End If

Next c

Here is that code snippet:

'Loop through first column completely ( 0 - 5 to test)
' i = 0 means go through every box in column 0 first
For i = 0 To 5
    'Loop through rows.
    For r = 0 To 2 '( 0 - 2 rows to test)
        ActiveCell.Offset(i, 0).value = PickTicketForm.Controls("PalletNumber" & i & r).Text
    Next r
Next i

Here is the userform layout:
userform

The spreadsheet output should look like this:

spreadsheet

10
  • 1
    Seems logical. What error (if any) are you getting? Commented Feb 27, 2017 at 20:23
  • 4
    Your "adding" loop goes r = 0 To 14, and your "reader" loop goes r = 0 To 2 and we don't know what i might be in the first loop. Also, how you're displaying the form to let the user input the values might be relevant, too. The code you've shown seems to work off the form's default instance - if you're entering the values in a New PickTicketForm (as you should, really) then the values don't exist in the default instance, you need to pass the form instance/object to the looping code. Commented Feb 27, 2017 at 20:23
  • 5
    Firstly, your naming is ambiguous. Should PalletNumber115 be row 11 column 5 or row 1 column 15. Either use fixed number of characters or add a _ between rows and columns Commented Feb 27, 2017 at 20:24
  • 2
    You can't use a dash as part of an identifier name, but an underscore is "fine" - not ideal, but fine (avoid underscores in public class module member names though). So the state lives in the global/default instance of the form object - if you have Unload Me anywhere in the form's code-behind, or if the form is closed with the "X" button /from the control menu, you'll lose that state. I'd warmly recommend treating forms as objects instead of as global state. Commented Feb 27, 2017 at 20:59
  • 2
    See UserForm best practices for a more OOP approach to user forms. Commented Feb 27, 2017 at 21:06

1 Answer 1

0

You should add your controls within a For i = 0 To 5 loop as well. If it remains empty you are adding PalletNumber0, PalletNumber1, PalletNumber2 etc. while you are searching for PalletNumber00, PalletNumber01, PalletNumber02 etc.

Sign up to request clarification or add additional context in comments.

1 Comment

Updated to the full code so it's more understandable. Also have the fix.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.