User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Visual Basic 4 / 5 / 6 section within the Software Development category of DaniWeb, a massive community of 426,811 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,932 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums
Views: 412 | Replies: 9
Reply
Join Date: Jul 2008
Posts: 13
Reputation: vbgirl is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
vbgirl vbgirl is offline Offline
Newbie Poster

How to seperate the text file data using Mid Function?

  #1  
Jul 24th, 2008
i have an question... this is my text file data
1234567,08072008,2
8767768,08072008,4
9988776,08072008,6
7891234,09072008,8
4567891,10072008,1
5213789,11072008,2
7854123,11072008,3
4561237,14072008,1
7879145,15072008,2
5218510,16072008,8


i would like to separate it to 3 part.
this code izit correct?

Dim a(50) As String
Dim c(50) As String
Dim ProductID(50) As String
Dim i As Integer
i = 0
ProductID(i) = UCase(Left(a(i), InStr(a(i), ",") - 1))
a(i) = Mid(a(i), Len(ProductID(i)) + 1, Len(a(i)))
c(i) = Right(c(i), 0)
MsgBox a(i), vbOKOnly, "Text File"
i = i + 1
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2006
Posts: 712
Reputation: QVeen72 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 97
QVeen72's Avatar
QVeen72 QVeen72 is offline Offline
Master Poster

Re: How to seperate the text file data using Mid Function?

  #2  
Jul 24th, 2008
Hi,

Have you Transferred all f your contents to array a..?

If not, You need to Open the Text File First:
Open "C:\MyFile.txt" For Input as #1

and Loop Thru File Contents using
Line Input...

Regards
Veena
Reply With Quote  
Join Date: Jul 2008
Posts: 13
Reputation: vbgirl is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
vbgirl vbgirl is offline Offline
Newbie Poster

Re: How to seperate the text file data using Mid Function?

  #3  
Jul 24th, 2008
yes i have
Dim a(50) As String
Dim c(50) As String
Dim ProductID(50) As String
Dim i As Integer
i = 0

cdbOpenFile.Filter = "Text Files (*.txt)|*.txt| "

'Specify default file name to open
cdbOpenFile.FileName = ""
' Specify default filter to *.txt
cdbOpenFile.FilterIndex = 1

' Display the Open dialog box, and
' save the selected file in the
' variable FileSelect
cdbOpenFile.ShowOpen



Open cdbOpenFile.FileName For Input As #1
Do Until EOF(1)

Line Input #1, a(i)
ProductID(i) = UCase(Left(a(i), InStr(a(i), ",") - 1))
a(i) = Mid(a(i), Len(ProductID(i)) + 2, Len(a(i)))

'MsgBox a(i), vbOKOnly, "Text File"
i = i + 1
Loop
Close #1

this is the code that i do..
Reply With Quote  
Join Date: Nov 2007
Posts: 28
Reputation: AUGXIS is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 4
AUGXIS's Avatar
AUGXIS AUGXIS is offline Offline
Light Poster

Re: How to seperate the text file data using Mid Function?

  #4  
Jul 24th, 2008
hi, can you try this code

Dim strText As String
Dim strText1 As String
Dim xwords As Integer
Dim xwords1 As Integer
Dim cPOs, cPos1

Set objRead = objAddress.OpenTextFile(App.Path + "\mytxtfile.txt", ForReading, False)
While Not objRead.AtEndOfStream
    strText = objRead.ReadLine
    
    For xwords = 1 To Len(strText)
        If InStr(xwords, strText, ",") >= 10 Then
        cPOs = xwords
        Text1 = Mid(strText, 1, cPOs - 2)
        strText1 = Mid(strText, cPOs, Len(strText))
        Exit For
        End If
    Next xwords
    
    For xwords1 = 1 To Len(strText1)
        If InStr(xwords1, strText1, ",") <= 8 Then
        cPos1 = xwords1
        Text2 = Mid(strText1, 1, cPos1 - 2)
        Text3 = Mid(strText1, cPos1, Len(strText1))
        Exit For
        End If
    Next xwords1
    
    grid.AddItem Text1 & vbTab & Text2 & vbTab & Text3
    
    Text1 = ""
    Text2 = ""
    Text3 = ""

Wend

objRead.Close
Reply With Quote  
Join Date: Nov 2006
Posts: 712
Reputation: QVeen72 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 97
QVeen72's Avatar
QVeen72 QVeen72 is offline Offline
Master Poster

Re: How to seperate the text file data using Mid Function?

  #5  
Jul 24th, 2008
Hi,

Just Use Split function..

  1. Dim TArr
  2. Line Input #1, a(i)
  3. TArr = Split(a(i),",")
  4. ProductID(i) = TArr(0)
  5. a(i) = TArr(1)
  6. c(i) = TArr(2)

Regards
Veena
Reply With Quote  
Join Date: Jul 2008
Posts: 13
Reputation: vbgirl is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
vbgirl vbgirl is offline Offline
Newbie Poster

Re: How to seperate the text file data using Mid Function?

  #6  
Jul 24th, 2008
Originally Posted by QVeen72 View Post
Hi,

Just Use Split function..

  1. Dim TArr
  2. Line Input #1, a(i)
  3. TArr = Split(a(i),",")
  4. ProductID(i) = TArr(0)
  5. a(i) = TArr(1)
  6. c(i) = TArr(2)

Regards
Veena



thx... wat is TArr?
Reply With Quote  
Join Date: Jul 2008
Posts: 3
Reputation: hartoksi is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
hartoksi hartoksi is offline Offline
Newbie Poster

Re: How to seperate the text file data using Mid Function?

  #7  
Jul 24th, 2008
If it is fixed lengt all time it is not necessary to write such long code
for x=0 to ? step 17 '17 is the length of each line
Part1=mid$(line x,i,7)
Part2=mid$(line x,9,8)
part3=right$(line x,1)
next i
Reply With Quote  
Join Date: Nov 2006
Posts: 712
Reputation: QVeen72 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 97
QVeen72's Avatar
QVeen72 QVeen72 is offline Offline
Master Poster

Re: How to seperate the text file data using Mid Function?

  #8  
Jul 25th, 2008
Hi,

Initially, TArr, is a Variant. When assigned with results of "Split" function , it becomes an Array..

REgards
Veena
Reply With Quote  
Join Date: Jul 2008
Posts: 13
Reputation: vbgirl is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
vbgirl vbgirl is offline Offline
Newbie Poster

Re: How to seperate the text file data using Mid Function?

  #9  
Jul 25th, 2008
Originally Posted by QVeen72 View Post
Hi,

Initially, TArr, is a Variant. When assigned with results of "Split" function , it becomes an Array..

REgards
Veena


oh i see.. but my code still got problem why it only show out one data when update?

Public cnName As Connection
Public rsId As Recordset


Public cnName As Connection
Public rsId As Recordset



Private Sub cmdImport_Click()
Dim a(50) As String
Dim c(50) As String
Dim ProductID(50) As String
Dim i As Integer
i = 0

          cdbOpenFile.Filter = "Text Files (*.txt)|*.txt| "
          
          'Specify default file name to open
          cdbOpenFile.FileName = ""
          ' Specify default filter to *.txt
          cdbOpenFile.FilterIndex = 1
          
          ' Display the Open dialog box, and
          ' save the selected file in the
          ' variable FileSelect
            cdbOpenFile.ShowOpen



Open cdbOpenFile.FileName For Input As #1
Do Until EOF(1)
Dim id
Line Input #1, a(i)
id = Split(a(i), ".")
ProductID(i) = id(0)
a(i) = id(1)
c(i) = id(2)
'MsgBox a(i), vbOKOnly, "Text File"
i = i + 1
Loop
Close #1

Dim b As Integer
b = i

Path = App.Path
Set cnName = New ADODB.Connection
With cnName
    .CursorLocation = adUseClient
    .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Path & "\name.mdb;Persist Security Info=False;Jet OLEDB"
    .Open
End With

Set rsId = New ADODB.Recordset
rsId.Open "ID", cnName, adOpenKeyset, adLockOptimistic

'With rsId
'.MoveFirst
'While Not .EOF
'MsgBox .Fields(0), vbOKOnly, "Access file"
'.MoveNext
'Wend
'End With
With rsId
.MoveFirst
Dim d As Integer
d = 0

For i = 0 To b
Do While Not .EOF
If ProductID(d) = rsId.Fields(0) Then
rsId.Fields(5) = a(i)
rsId.Update
End If
rsId.MoveNext
Loop
rsId.MoveFirst
d = d + 1
Next
MsgBox "Done", vbOKOnly + vbInformation, "Complete"
End With

'when data done all will show a message box "DONE"
'MsgBox "Done", vbOKOnly + vbInformation, "Complete"
End Sub

please help me thx
Reply With Quote  
Join Date: Nov 2006
Posts: 712
Reputation: QVeen72 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 97
QVeen72's Avatar
QVeen72 QVeen72 is offline Offline
Master Poster

Re: How to seperate the text file data using Mid Function?

  #10  
Jul 25th, 2008
Hi,

I guess, You Can use "FIND" method of ADO Recordset...
Try This :

After opening the connection...
  1. Set rsId = New ADODB.Recordset
  2. rsId.Open "ID", cnName, adOpenKeyset, adLockOptimistic
  3. For i = 0 To b
  4. RSID.MoveFirst
  5. RSID.Find "PID='" & ProductID(i) & "'"
  6. If Not RSID.EOF Then
  7. rsId.Fields(5) = a(i)
  8. rsId.Update
  9. End If
  10. Next
  11.  
  12. MsgBox "Done", vbOKOnly + vbInformation, "Complete"
  13. End With
  14.  

Regards
Veena
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Visual Basic 4 / 5 / 6 Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the Visual Basic 4 / 5 / 6 Forum

All times are GMT -4. The time now is 8:07 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC