[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:
'Move Textbox data to Listbox
strParts = Split(txtNotSequential.Text, vbCrLf)
For lngIndex = 0 To UBound(strParts)
lstCheck.AddItem strParts(lngIndex)
Next
txtNotSequential.Text = ""
'Move Listbox data back to Textbox
For lngIndex = 0 To lstCheck.ListCount - 1
'Checks to see if the number is not less than 11 digits long long
If Len(lstCheck) <> 11 Then
MsgBox "Please enter a complete Mobile number including the " & vbCrLf & _
"X mobile code 01234", vbInformation, "Invalid Mobile Number"
For lngIndex = 0 To lstCheck.ListCount - 1
txtNotSequential.Text = Left$(txtNotSequential.Text, Len(txtNotSequential.Text) - 2)
Loop
lstCheck.Clear
Exit Sub
'Checks to see if the mobile code has been entered
ElseIf Mid(lstCheck, 1, 5) <> "01234" Then
MsgBox "Please enter the X Mobile Code 01234", vbInformation, "No need for code"
For lngIndex = 0 To lstCheck.ListCount - 1
txtNotSequential.Text = Left$(txtNotSequential.Text, Len(txtNotSequential.Text) - 2)
Loop
lstCheck.Clear
Exit Sub
End If
txtNotSequential.Text = txtNotSequential.Text & lstCheck.List(lngIndex) & vbCrLf
Next
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
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:
Option Explicit
Private Sub Command1_Click()
MsgBox Len(List1)
End Sub
Private Sub Form_Load()
List1.AddItem "Oranges"
List1.AddItem "Grapes"
List1.AddItem "Pears"
List1.AddItem "Peaches"
End Sub
The msgbox that popped up as a result of the command button click contained 0.
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
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:
Dim i As Long
For i = 0 To List1.ListCount - 1
MsgBox Len(List1.List(i))
Next
If I'm off base, let me know.
Re: Textbox to Listbox with Validation
I am getting somewhere, this is what I have changed the code to:
VB Code:
'Move Textbox data to Listbox
strParts = Split(txtNotSequential.Text, vbCrLf)
For l = 0 To UBound(strParts)
lstCheck.AddItem strParts(lngIndex)
Next
txtNotSequential.Text = ""
'Move Listbox data back to Textbox
For l = 0 To lstCheck.ListCount - 1
If Len(lstCheck.List(l)) <> 11 Then
MsgBox "NEEDS TO BE 11", vbInformation, "NEED TO BE 11"
txtNotSequential.Text = txtNotSequential.Text & lstCheck.List(l) & vbCrLf
lstCheck.Clear
Exit Sub
ElseIf Mid(lstCheck.List(l), 1, 5) <> "01234" Then
MsgBox "NEED 01234", vbInformation, "NEED 01234"
txtNotSequential.Text = txtNotSequential.Text & lstCheck.List(l) & vbCrLf
lstCheck.Clear
End If
Next
txtNotSequential.Text = txtNotSequential.Text & lstCheck.List(l) & vbCrLf
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 ! ! :confused: :confused:
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:
For l = 0 To UBound(strParts)
lstCheck.AddItem Trim(strParts(lngIndex))
Next
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:
For l = 0 To UBound(strParts)
If Len(strParts(l)) <> 11 Then
MsgBox "NEEDS TO BE 11", vbInformation, "NEED TO BE 11"
Exit Sub 'dont even add it to the listbox unless it is 11 characters
End If
'if we get this far then it is 11 characters, so add it
lstCheck.AddItem strParts(lngIndex)
Next
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.
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:
For l = 0 To UBound(strParts)
If Len(strParts(l)) <> 11 Then
MsgBox "NEEDS TO BE 11", vbInformation, "NEED TO BE 11"
Exit Sub 'dont even add it to the listbox unless it is 11 characters
End If
'if we get this far then it is 11 characters, so add it
lstCheck.AddItem strParts(lngIndex)
Next
within this code, is there a way that I can use a "Do Until" the end of that specific line using "vbCrLf"...??
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?
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!! :D
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!! :D
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. :)