-
Jul 9th, 2017, 08:37 AM
#1
Thread Starter
Member
find a number in textbox from vb6
Hi
I want vb6 code to find the number in the text box and display it in another box with one of the following conditions
1 . For example, find a four-digit number in the text
Or
2 . For example, find a number between 1000 and 2000 in the text box please guide me .
Thank you
Sorry for my English type
Last edited by aghamali; Jul 9th, 2017 at 08:55 AM.
-
Jul 9th, 2017, 09:19 AM
#2
Re: find a number in textbox from vb6
Do you know the number or are you looking to find all 4 digit numbers or all numbers between 1000 and 2000. Do the numbers contain thousand separators? More details would be helpful
-
Jul 9th, 2017, 09:37 AM
#3
Thread Starter
Member
Re: find a number in textbox from vb6
abc defgh kslkh52sdf khsdfklh1895gskdf jgkjjkg sdj9999gkjg
For example, look up the text above and if it finds a number between 1000 and 2000, it will show in a text box
-
Jul 9th, 2017, 09:51 AM
#4
Re: find a number in textbox from vb6
In this case, you will need to test every character in the textbox and keep track of whether the character is numeric or not. Once you have 4 consecutive numeric characters, you found what you are looking for. However, I think that logic is too simple, because what if you encounter 5, 6 or more consecutive numeric characters? As you can see, this is not terribly difficult, it will just require that you make some descisions
Some test code provided. Modify as needed. This is air-code, not tested so apologize if any errors.
Code:
Dim i As Long, lCount as Long, lNumber As Long
For i = 1 to Len(Text1.Text)
Select Case Asc(Mid$(Text1.Text, i, 1))
Case 48 to 57 ' numeric characters 0 - 9 inclusively
If lCount = 3 Then ' already have 3 numbers and this is the 4th
lNumber = CLng(Mid$(Text1.Text, i - 3, 4))
' test the value of lNumber. If it is what you are looking for then exit the loop
' else if you want to continue looking starting with the next character, set lCount to 0
' else other decision factors
MsgBox "4-digit number found: " & lNumber, vbOkOnly, "Test"
Exit For
Else
lCount = lCount + 1
End If
Case Else
lCount = 0
End Select
Next
Last edited by LaVolpe; Jul 9th, 2017 at 10:08 AM.
-
Jul 9th, 2017, 10:01 AM
#5
Thread Starter
Member
Re: find a number in textbox from vb6
Thank you for the answer
How should the code be used?
I am an amateur
-
Jul 9th, 2017, 10:07 AM
#6
Re: find a number in textbox from vb6
Code:
Private Sub Command1_Click()
Text2.Text = ExtractNumbers(Text1.Text)
End Sub
Private Function ExtractNumbers(nText As String, Optional nLowestValue As Long = 1000, Optional nHighestValue As Long = 2000) As String
Dim iChr As String
Dim c As Long
Dim iNum As String
Dim iVal As Long
For c = 1 To Len(nText) + 1
iChr = Mid(nText, c, 1)
If IsNumeric(iChr) Then
iNum = iNum & iChr
ElseIf iNum <> "" Then
iVal = Val(iNum)
If (iVal >= nLowestValue) And (iVal <= nHighestValue) Then
If ExtractNumbers <> "" Then ExtractNumbers = ExtractNumbers & " "
ExtractNumbers = ExtractNumbers & iNum
End If
iNum = ""
End If
Next c
End Function
-
Jul 9th, 2017, 10:08 AM
#7
Re: find a number in textbox from vb6
I'm sure others may be willing to write this for you. I've given you some sample logic, but am not willing to write your project for you. Am very happy to help, to offer suggestions, or help identify logic and logic errors. I will offer this:
Add a command button to your form. In the command button's click event, paste the sample code. Change Text1 in the sample code to your textbox control's name. I tweaked the sample code a bit to display the 1st 4-digit number found. Add sample text to your textbox and click the button when running for the test.
-
Jul 9th, 2017, 10:21 AM
#8
Thread Starter
Member
Re: find a number in textbox from vb6
I thank you very much
I understand you
You've helped a lot so far
-
Oct 19th, 2017, 01:45 PM
#9
New Member
Re: find a number in textbox from vb6
I'm not an expert in VB. Try this code. this may help you.
Code:
Public Sub ExtractNumbersToTextbox(SoureTestbox As TextBox, DestinationTextbox As TextBox, minValue As Integer, maxValue As Integer)
Dim AllNumbersFromSourceString As String
Dim SelectedNumbers() As String
DestinationTextbox.Text = ""
AllNumbersFromSourceString = ""
For i = 1 To Len(SoureTestbox.Text)
If IsNumeric(Mid(SoureTestbox.Text, i, 1)) = True Then
AllNumbersFromSourceString = AllNumbersFromSourceString & Mid(SoureTestbox.Text, i, 1)
Else
If Right(AllNumbersFromSourceString, 1) <> "#" Then
AllNumbersFromSourceString = AllNumbersFromSourceString & "#"
End If
End If
Next i
SelectedNumbers = Split(AllNumbersFromSourceString, "#")
For ai = 0 To UBound(SelectedNumbers)
If SelectedNumbers(ai) <> "" Then
If SelectedNumbers(ai) < (maxValue + 1) And SelectedNumbers(ai) > (minValue - 1) Then
DestinationTextbox.Text = DestinationTextbox.Text & SelectedNumbers(ai) & ", "
End If
End If
Next ai
End Sub
Private Sub Command1_Click()
ExtractNumbersToTextbox TextBox1, TextBox2, 1000, 2000
End Sub
-
Oct 20th, 2017, 03:32 PM
#10
Re: find a number in textbox from vb6
a different approach : but be sure to look at the last result
Code:
Private Sub Command1_Click()
Dim i As Integer
Dim S As String
Dim J As Integer
S = "abc1234567890qwerty1129876azerty12314E2"
Debug.Print S
For i = 1 To Len(S)
J = Val(Mid(S, i, 4))
If J > 999 Then
If J < 2001 Then
Debug.Print J; " ---- between 1000 and 2000 at position " & i
Else
Debug.Print J; " at position " & i
End If
End If
Next
End Sub
do not put off till tomorrow what you can put off forever
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|