Solved

How to read textbox with multiple lines separated by slushes?

Posted on 2011-09-26
8
238 Views
Last Modified: 2012-05-12
I have a textbox name text1.text. It has a multiple text. Each line are separated with slushes till the last  line. here's the data of the textbox;

3356\Paracetamol 500mg\30
123490\Amoxicillin 350mg Cap\25
7685\Cefalexin\90

I want to read the first line each data separated by "\" till the last line. How? Pls...
0
Comment
Question by:Whing Dela Cruz
  • 4
  • 3
8 Comments
 
LVL 16

Assisted Solution

by:HooKooDooKu
HooKooDooKu earned 100 total points
ID: 36601686
What exactly do you mean "read"?

If the textbox is a multiline text box, then the entire contents of the textbox is actually a single string of text, with Carriage-Return/Line-Feed pairs denoting where the lines seperate (not sure, but a simple Carriage-Return by itself might also be able to denote the end of the line).

So one solution might be to so a simple string replace to convert the '\' to Carriage-Return/Line-Feed pairs, then use Instr() to find the Carriage-Return/Line-Feed pairs.

Dim A$(100)
Dim str as String
Din
str = Text1.text
Replace( str, "/", vbCrLf )
Dim Count as integer
Dim I as integer
Dim Last as integer

Last = 1
I = InStr( str, vbCrLf )
do while( I > 0 )
    Count = Count + 1
    A$(Count) = Mid$(str, Last, I - Last)
    Last = I + 2
    I = Instr( Last, str, vbCrLf )
loop
0
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 400 total points
ID: 36601744
You could do it like this:

Private Sub Command1_Click()
Dim strlines() As String
    Dim strItems() As String
    Dim l As Integer
    Dim I As Integer
    strlines = Split(Me.Text1.Text, vbCrLf)
    For l = 0 To 2
        strItems() = Split(strlines(l), "\")
        For I = 0 To 2
            Debug.Print l, I, strItems(I)
        Next I
    Next l
End Sub

Open in new window

0
 

Author Comment

by:Whing Dela Cruz
ID: 36601942
Hi HooKooDooKu,
 What I mean about read is;
msgbox first line 'read the first line each data separated by "\" and able to loop till the last line.  
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 76

Expert Comment

by:GrahamSkan
ID: 36602171
Does my code suit you? I don't know what you want to do with the data, so I just show it in the Immediate window.
0
 

Author Comment

by:Whing Dela Cruz
ID: 36602210
GrahamSkan, you got exactly what I need, Thanks a lot.. Could i ask more question? I need more help about the things I've trying to develop.  it is textbox to excell. Thru your help i was able develop the code below, but the problem is.. it only save one data, the last data of the textbox...
Private Sub Command1_Click()
   Dim oExcel As Object
   Dim oBook As Object
   Dim oSheet As Object

   'Start a new workbook in Excel
   Set oExcel = CreateObject("Excel.Application")
   Set oBook = oExcel.Workbooks.Add
   
   'Add data to cells of the first worksheet in the new workbook
   Set oSheet = oBook.Worksheets(1)
   oSheet.range("A1").Value = "Code"
   oSheet.range("B1").Value = "Description"
   oSheet.range("c1").Value = "Quantity"
   oSheet.range("A1:B1:C1").Font.Bold = True

Dim strlines() As String
    Dim strItems() As String
    Dim l As Integer
    Dim I As Integer
    strlines = Split(Me.Text1.Text, vbCrLf)
    For l = 0 To 2
        strItems() = Split(strlines(l), "\")
        For I = 0 To 2
        Next I
            oSheet.range("A2").Value = strItems(0)
            oSheet.range("B2").Value = strItems(1)
            oSheet.range("C2").Value = strItems(2)
            
    Next l

   'Save the Workbook and Quit Excel
   oBook.SaveAs "C:\Report.xlsx"
   oExcel.Quit
   
End Sub

Open in new window

0
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 400 total points
ID: 36602324
A couple of problems.

The For/Next loop lines 24 and 25 does nothing, and the lines 26 to 25 do not vary the row. Try this. (I've changed the variable I to c, because I looks a bit like l
Dim oExcel As Object
   Dim oBook As Object
   Dim oSheet As Object

   'Start a new workbook in Excel
   Set oExcel = CreateObject("Excel.Application")
   Set oBook = oExcel.Workbooks.Add
   oExcel.Visible = True
   'Add data to cells of the first worksheet in the new workbook
   Set oSheet = oBook.Worksheets(1)
   oSheet.range("A1").Value = "Code"
   oSheet.range("B1").Value = "Description"
   oSheet.range("c1").Value = "Quantity"
   oSheet.range("A1:B1:C1").Font.Bold = True

Dim strlines() As String
    Dim strItems() As String
    Dim l As Integer
    Dim c As Integer
    strlines = Split(Me.Text1.Text, vbCrLf)
    For l = 0 To 2
        strItems() = Split(strlines(l), "\")
        For c = 0 To 2
            oSheet.Cells(l + 2, c + 1).Value = strItems(c)
         Next c
           
    Next l

   'Save the Workbook and Quit Excel
   oBook.SaveAs "C:\Report.xlsx"
   oExcel.Quit

Open in new window

0
 

Accepted Solution

by:
Whing Dela Cruz earned 0 total points
ID: 36602375
Thanks a lot GrahamSkan, its really working... More Power to you! and God Bless!
0
 

Author Closing Comment

by:Whing Dela Cruz
ID: 36895909
Thank you so much... excellent solution...
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SSRS expression Issue finding a string 10 93
Spell Check in VB6 13 127
MsgBox 4 69
Excel Automation VBA 19 91
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question