Solved

How to read textbox with multiple lines separated by slushes?

Posted on 2011-09-26
8
237 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

785 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