Results 1 to 12 of 12

Thread: [RESOLVED] Textbox to Listbox with Validation

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2006
    Posts
    45

    Resolved [RESOLVED] Textbox to Listbox with Validation

    I am moving data from a textbox and listbox to validate each line individually, here is my code:

    VB Code:
    1. 'Move Textbox data to Listbox
    2.     strParts = Split(txtNotSequential.Text, vbCrLf)
    3.  
    4.     For lngIndex = 0 To UBound(strParts)
    5.         lstCheck.AddItem strParts(lngIndex)
    6.     Next
    7.    
    8.     txtNotSequential.Text = ""
    9.    
    10.     'Move Listbox data back to Textbox
    11.     For lngIndex = 0 To lstCheck.ListCount - 1
    12.    
    13.         'Checks to see if the number is not less than 11 digits long long
    14.         If Len(lstCheck) <> 11 Then
    15.                 MsgBox "Please enter a complete Mobile number including the " & vbCrLf & _
    16.                        "X mobile code 01234", vbInformation, "Invalid Mobile Number"
    17.                
    18.                 For lngIndex = 0 To lstCheck.ListCount - 1
    19.                
    20.                     txtNotSequential.Text = Left$(txtNotSequential.Text, Len(txtNotSequential.Text) - 2)
    21.                
    22.                 Loop
    23.                
    24.                 lstCheck.Clear
    25.            
    26.             Exit Sub
    27.  
    28.         'Checks to see if the mobile code has been entered
    29.         ElseIf Mid(lstCheck, 1, 5) <> "01234" Then
    30.                 MsgBox "Please enter the X Mobile Code 01234", vbInformation, "No need for code"
    31.            
    32.                 For lngIndex = 0 To lstCheck.ListCount - 1
    33.                
    34.                     txtNotSequential.Text = Left$(txtNotSequential.Text, Len(txtNotSequential.Text) - 2)
    35.                
    36.                 Loop
    37.                
    38.                 lstCheck.Clear
    39.            
    40.             Exit Sub
    41.  
    42.         End If
    43.    
    44.        txtNotSequential.Text = txtNotSequential.Text & lstCheck.List(lngIndex) & vbCrLf
    45.     Next
    46.  
    47.     lstCheck.Clear

    The problem I am having it that no matter what data is put into the textbox (even if it is correct) the first msgbox will error? Can anyone help with my coding?








    Added [RESOLVED] to thread title and green "resolved" checkmark - Hack
    Last edited by Hack; Jan 19th, 2006 at 10:02 AM.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Textbox to Listbox with Validation

    You are checking the length of a listbox. That will return 0.

    As a test, I did the following
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4. MsgBox Len(List1)
    5. End Sub
    6.  
    7. Private Sub Form_Load()
    8. List1.AddItem "Oranges"
    9. List1.AddItem "Grapes"
    10. List1.AddItem "Pears"
    11. List1.AddItem "Peaches"
    12. End Sub
    The msgbox that popped up as a result of the command button click contained 0.

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2006
    Posts
    45

    Re: Textbox to Listbox with Validation

    What can I do to amend this problem? That might sound stupid but I am a new to this

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Textbox to Listbox with Validation

    Quote Originally Posted by JerseyKid
    What can I do to amend this problem? That might sound stupid but I am a new to this
    Amend as necessary, but if I'm getting you right, you need to check the length of a listbox entry. This will do that
    VB Code:
    1. Dim i As Long
    2. For i = 0 To List1.ListCount - 1
    3.     MsgBox Len(List1.List(i))
    4. Next
    If I'm off base, let me know.

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2006
    Posts
    45

    Re: Textbox to Listbox with Validation

    I am getting somewhere, this is what I have changed the code to:

    VB Code:
    1. 'Move Textbox data to Listbox
    2.     strParts = Split(txtNotSequential.Text, vbCrLf)
    3.  
    4.     For l = 0 To UBound(strParts)
    5.         lstCheck.AddItem strParts(lngIndex)
    6.     Next
    7.    
    8.     txtNotSequential.Text = ""
    9.    
    10. 'Move Listbox data back to Textbox
    11. For l = 0 To lstCheck.ListCount - 1
    12.     If Len(lstCheck.List(l)) <> 11 Then
    13.         MsgBox "NEEDS TO BE 11", vbInformation, "NEED TO BE 11"
    14.         txtNotSequential.Text = txtNotSequential.Text & lstCheck.List(l) & vbCrLf
    15.         lstCheck.Clear
    16.         Exit Sub
    17.     ElseIf Mid(lstCheck.List(l), 1, 5) <> "01234" Then
    18.         MsgBox "NEED 01234", vbInformation, "NEED 01234"
    19.         txtNotSequential.Text = txtNotSequential.Text & lstCheck.List(l) & vbCrLf
    20.         lstCheck.Clear
    21.     End If
    22.  
    23. Next
    24.  
    25. txtNotSequential.Text = txtNotSequential.Text & lstCheck.List(l) & vbCrLf
    26.  
    27.     lstCheck.Clear

    This works fine for one mobile number (one line of data) moving it from the textbox to the listbox, doing the validation on that piece of data and sending it on from there, but when I add multiple lines within the textbox a msgbox will always appear saying that there is more than 11 digits within listbox? I would like each line (mobile number) of data validated individually, HELP ME ! !

  6. #6
    Member
    Join Date
    Aug 2005
    Posts
    34

    Re: Textbox to Listbox with Validation

    Use Hack's code and msgbox the length of the other elements. There may be spaces that are entered that make the length longer. Try using the "Trim" statement when loading the listbox

    VB Code:
    1. For l = 0 To UBound(strParts)
    2.         lstCheck.AddItem Trim(strParts(lngIndex))
    3. Next

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Textbox to Listbox with Validation

    I see no point in even adding it to your listbox if it doesn't meet your criteria. Play around with this
    VB Code:
    1. For l = 0 To UBound(strParts)
    2.     If Len(strParts(l)) <> 11 Then
    3.         MsgBox "NEEDS TO BE 11", vbInformation, "NEED TO BE 11"
    4.         Exit Sub 'dont even add it to the listbox unless it is 11 characters
    5.     End If
    6.         'if we get this far then it is 11 characters, so add it
    7.         lstCheck.AddItem strParts(lngIndex)
    8. Next

  8. #8
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Textbox to Listbox with Validation

    Why are you trying to send more than 1 number at the same time?
    The textbox should be used to add 1 number, then press a button to move that number to the list, clear the textbox. Enter another number in the textbox, then add to list and clear again, and so on. (This is just analysis, not programming).

    It would be better if you explain what are you trying to do in this program, I.E. if the user will enter this numbers manually the best would be as I said before, but if this numbers are coming from a file some different action will be needed.
    Last edited by jcis; Jan 19th, 2006 at 08:54 AM.

  9. #9

    Thread Starter
    Member
    Join Date
    Jan 2006
    Posts
    45

    Re: Textbox to Listbox with Validation

    Quote Originally Posted by Hack
    I see no point in even adding it to your listbox if it doesn't meet your criteria. Play around with this
    VB Code:
    1. For l = 0 To UBound(strParts)
    2.     If Len(strParts(l)) <> 11 Then
    3.         MsgBox "NEEDS TO BE 11", vbInformation, "NEED TO BE 11"
    4.         Exit Sub 'dont even add it to the listbox unless it is 11 characters
    5.     End If
    6.         'if we get this far then it is 11 characters, so add it
    7.         lstCheck.AddItem strParts(lngIndex)
    8. Next
    within this code, is there a way that I can use a "Do Until" the end of that specific line using "vbCrLf"...??

  10. #10
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Textbox to Listbox with Validation

    Quote Originally Posted by JerseyKid
    within this code, is there a way that I can use a "Do Until" the end of that specific line using "vbCrLf"...??
    I don't understand...

    According to your Split statement, your textbox is being parsed into an array with each element of the array being an individual entry. What is the purpose for checking for a carriage return/line feed?

  11. #11

    Thread Starter
    Member
    Join Date
    Jan 2006
    Posts
    45

    Re: Textbox to Listbox with Validation

    Quote Originally Posted by Hack
    I don't understand...

    According to your Split statement, your textbox is being parsed into an array with each element of the array being an individual entry. What is the purpose for checking for a carriage return/line feed?
    Sorry Hack, please ignore that message, I have just figured out the data being passed into an array!! I have it working and validating my data, THANKS for your patients and understand!!

  12. #12
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Textbox to Listbox with Validation

    Quote Originally Posted by JerseyKid
    Sorry Hack, please ignore that message, I have just figured out the data being passed into an array!! I have it working and validating my data, THANKS for your patients and understand!!
    No problem.

    Glad you got it worked out.

    If this is resolved, please pull down the Thread Tools menu and click the Mark Thread Resolved button. That will let everyone know that you have your answer.

    Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width