|
-
Mar 28th, 2007, 10:27 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] Reading The First Two Lines Of A Standard Textbox
I have a standard, multiline, textbox on one of my screens. A typical entry will be:
"Mr. John Smith"
12345 Cherry Lane
Boston, MA 02100
[email protected]
617-123-4567"
I need to take the address part, and store it in a variable for latter use.
Here is the problem. The address part could be two, or three lines before there is a double space with email address and phone number. It could read:
"Mr. John Smith"
No Address Listed
[email protected]
617-123-4567"
Regardless of how many lines it is, how do I get just the address part?
Beantown Boy
Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
-
Mar 28th, 2007, 10:32 AM
#2
Re: Reading The First Two Lines Of A Standard Textbox
Pseudocode:
The @ sign is a unique identifier in the e-mail address. Search for that and count the number of lines with hard returns are in the textbox before you find it.
That way you will know exactly how many lines are in the address before the e-mail line appears.
-
Mar 28th, 2007, 10:47 AM
#3
Re: Reading The First Two Lines Of A Standard Textbox
this works.
vb Code:
Private Sub Form_Load()
Dim strContact, strAddress
strContact = """Mr. John Smith""" & vbCrLf & _
"12345 Cherry Lane" & vbCrLf & _
"Boston, MA 02100" & vbCrLf & vbCrLf & _
"617-123-4567"
MsgBox strContact
'get the relevant information
strAddress = Mid$(strContact, 1, InStr(1, strContact, vbCrLf & vbCrLf, vbTextCompare) - 1)
strAddress = Mid$(strAddress, InStr(InStr(1, strAddress, """") + 1, strAddress, """", vbTextCompare) + 3, Len(strAddress))
MsgBox """" & strAddress & """"
End Sub
when ran, the above will return this (keep in mind, the quotes around it are added on the MsgBox call to make sure no extra line breaks were there):
Code:
"12345 Cherry Lane
Boston, MA 02100"
I tested it with 1, 2 and 3 lined addresses. either way, it should work with any amount of lines. It looks for a double space and then grabs the information after the second quote before it (meaning that all of your names must have the quotes around them, otherwise it wouldn't work.. but that's possible to change, too). I manually entered the address into a variable because I didn't want to open a file or anything, but it should be fine with whatever variable you're holding your contact information in. let me know.
edit: made a quick fix to remove an extra call to Left() and removed an un-needed variable
Last edited by kows; Mar 28th, 2007 at 10:51 AM.
-
Mar 28th, 2007, 10:58 AM
#4
Re: Reading The First Two Lines Of A Standard Textbox
Is there a danger that the email part could be "No email listed", would searching for VbNewline & VbNewline not be more reliable? i.e.
Code:
Address = Left$(Text1.Text, InStr$(Text1.Text, vbNewLine & vbNewLine) - 2)
Edit: did not see your post kows, soz
-
Mar 28th, 2007, 11:02 AM
#5
Re: Reading The First Two Lines Of A Standard Textbox
Here is another way:
vb Code:
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function SendMessageStr Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As String) As Long
Private Const EM_GETLINECOUNT = &HBA
Private Const EM_LINELENGTH = &HC1
Private Const EM_LINEINDEX = &HBB
Private Const EM_GETLINE = &HC4
Private Sub Command1_Click()
Dim lngCount As Long
Dim lngLineIndex As Long
Dim lngLength As Long
Dim strBuffer As String
Dim lngIndex As Long
Randomize
'Get the line count
lngCount = SendMessage(Text1.hwnd, EM_GETLINECOUNT, 0, 0)
With Text1
For lngIndex = 0 To lngCount - 1
'Get line index of the chosen line
lngLineIndex = SendMessage(.hwnd, EM_LINEINDEX, lngIndex, 0)
'get line length
lngLength = SendMessage(.hwnd, EM_LINELENGTH, lngLineIndex, 0)
' 'resize buffer
strBuffer = Space(lngLength)
If strBuffer = "" Then
Exit For
End If
' 'get line text
Call SendMessageStr(.hwnd, EM_GETLINE, lngIndex, ByVal strBuffer)
Debug.Print strBuffer
Next
End With
End Sub
-
Mar 28th, 2007, 12:23 PM
#6
Thread Starter
Frenzied Member
Re: Reading The First Two Lines Of A Standard Textbox
Thank you Martin. That worked just fine with one minor alteration.
I added
Code:
Dim strNameAddress As String
'I substituted this line
Debug.Print strBuffer
'with
strNameAddress = strNameAddress & vbCrLf & strBuffer
Works great!
Beantown Boy
Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
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
|