• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 129
  • Last Modified:

I'm sure there is an easer way to ask this

Say I have some text in textbox1 e.g."10 Im 11 today". I then want to click on a command button, and the text goes to textbox2. but i also whant the command button to only read the first set of numbers e.g."10" and stops reading when it gets to the first space so textbox2 = "10" or what ever number is at the start of textbox1.
0
mangaman3000
Asked:
mangaman3000
1 Solution
 
mcoopCommented:
It will probably help if you can explain what is in textbox1 at the start (as your posting came out with non-printable characters)

The basic practice is to identify a consistent, unique character/sequence at or near the boundary of the two fields you are trying to separate.

Then you can use functions like instr() and split() to separate the two halves with left() and mid()

What you do with them after that is relatively straightforward.

0
 
mangaman3000Author Commented:
can u read it now?
and also im new to vb can u give me a code, it will much appreciated.
0
 
mangaman3000Author Commented:
can u read it now?
and also im new to vb can u give me a code, it will much appreciated.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
SilentRageCommented:
Private Sub Command1_Click()
Dim WordArray() As String
    'split the data into an array of words
    WordArray = Split(Trim(Text1.Text), " ")
    'the first word is the number or whatever you put in
    Text1.Text = WordArray(0)
End Sub
0
 
RhaedesCommented:
Try this. I asume two textboxes (text1 and text2) and a commandbutton (Command1)

Private Sub Command1_Click()
Dim aChar As String
Dim goodChars As String
goodChars = "01234567890.," 'Contains all the acceptable characters
Text1.Text = Trim(Text1.Text) 'Remove leading spaces just in case
Text2.Text = "" 'Clear the target textbox

For n = 1 To Len(Text1.Text) 'Loop through the characters in Text1
aChar = Mid$(Text1.Text, n, 1) 'Take each in turn
If InStr(1, goodChars, aChar) = 0 Then Exit For 'Stop when you get to a bad character
Text2.Text = Text2.Text + aChar 'Copy the character across to Text2
Next n

End Sub


Kindest regards,
Rhaedes
0
 
Abu_ObaidCommented:
'you can use this function to get the first number a string

Function GetNumber(Txt As String) As String
Dim strArry() As String
If Txt = "" Then
    GetNumber = ""
Else
    strArry = Split(Txt, " ", 2)
    If IsNumeric(strArry(0)) Then
        GetNumber = strArry(0)
    Else
        GetNumber = ""
    End If
End If
End Function

' to try this function add Text1, Text2 and Command1 to a standard for then use this code

Private Sub Command1_Click()
   Text2.Text = GetNumber(Text1.Text)
End Sub
0
 
mcoopCommented:
Yes- more readable !!

I think the fellows above are helping you move in the right direction for code.

Last point to consider - if your source string format is always the same,  "## Im ## today"   a hard coded routine will do, but if your source strng is formatted in a number of ways, then you should adopt the smarter approach, and identify the required substrings first, then separate them.
0
 
MCummings111400Commented:
Try this,

Const sDelim as String = " "

Text2.Text = Left(Text1.Text,InStr(Text1.Text,sDelim))
0
 
mcoopCommented:
Try this -
one textbox, two listboxes
list1 gets all substrings
list2 only gets substrings with numeric values

Dim substr() As String
Private Sub Text1_Change()
  List1.Clear
  List2.Clear
  substr = Split(Text1, " ")
  For x = 0 To UBound(substr)
    List1.AddItem Trim(substr(x))
    If IsNumeric(substr(x)) Then
      List2.AddItem Trim(substr(x))
    End If
  Next
End Sub
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now