Results 1 to 6 of 6

Thread: [RESOLVED] Reading The First Two Lines Of A Standard Textbox

  1. #1

    Thread Starter
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Resolved [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.

  2. #2
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    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.
    Doctor Ed

  3. #3
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Reading The First Two Lines Of A Standard Textbox

    this works.
    vb Code:
    1. Private Sub Form_Load()
    2.     Dim strContact, strAddress
    3.     strContact = """Mr. John Smith""" & vbCrLf & _
    4.                  "12345 Cherry Lane" & vbCrLf & _
    5.                  "Boston, MA 02100" & vbCrLf & vbCrLf & _
    6.                  "[email protected]" & vbCrLf & _
    7.                  "617-123-4567"
    8.     MsgBox strContact
    9.     'get the relevant information
    10.     strAddress = Mid$(strContact, 1, InStr(1, strContact, vbCrLf & vbCrLf, vbTextCompare) - 1)
    11.     strAddress = Mid$(strAddress, InStr(InStr(1, strAddress, """") + 1, strAddress, """", vbTextCompare) + 3, Len(strAddress))
    12.     MsgBox """" & strAddress & """"
    13. 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.

  4. #4
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    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

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Reading The First Two Lines Of A Standard Textbox

    Here is another way:
    vb Code:
    1. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    2.         (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    3. Private Declare Function SendMessageStr Lib "user32" Alias "SendMessageA" _
    4.         (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As String) As Long
    5. Private Const EM_GETLINECOUNT = &HBA
    6. Private Const EM_LINELENGTH = &HC1
    7. Private Const EM_LINEINDEX = &HBB
    8. Private Const EM_GETLINE = &HC4
    9.  
    10. Private Sub Command1_Click()
    11.     Dim lngCount As Long
    12.     Dim lngLineIndex As Long
    13.     Dim lngLength As Long
    14.     Dim strBuffer As String
    15.     Dim lngIndex As Long
    16.    
    17.     Randomize
    18.    
    19.     'Get the line count
    20.     lngCount = SendMessage(Text1.hwnd, EM_GETLINECOUNT, 0, 0)
    21.    
    22.     With Text1
    23.         For lngIndex = 0 To lngCount - 1
    24.             'Get line index of the chosen line
    25.             lngLineIndex = SendMessage(.hwnd, EM_LINEINDEX, lngIndex, 0)
    26.             'get line length
    27.             lngLength = SendMessage(.hwnd, EM_LINELENGTH, lngLineIndex, 0)
    28. '            'resize buffer
    29.             strBuffer = Space(lngLength)
    30.             If strBuffer = "" Then
    31.                 Exit For
    32.             End If
    33. '            'get line text
    34.             Call SendMessageStr(.hwnd, EM_GETLINE, lngIndex, ByVal strBuffer)
    35.             Debug.Print strBuffer
    36.         Next
    37.     End With
    38.  
    39. End Sub

  6. #6

    Thread Starter
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    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
  •  



Click Here to Expand Forum to Full Width