Results 1 to 10 of 10

Thread: help pls

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2002
    Posts
    60

    help pls

    Anyone know how to (using VB6):

    a) create a .txt file

    b) read from any of the created file

    c) write or put information into the .txt file

    I tried MSscript but it gives me a lot of problem(I'm not farmiliar

    with it).

    The text stored in the .txt file is just for information storage, not

    for controling any objects or script.

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub cmdLoad_Click()
    4.     Text1.Text = FileToString("C:\File.txt")
    5. End Sub
    6.  
    7. Private Sub cmdSave_Click()
    8.     StringToFile Text1.Text, "C:\File.txt"
    9. End Sub
    10.  
    11. Function FileToString(ByVal sFile As String) As String
    12.     Dim iFile As Integer
    13.     Dim sData As String
    14.    
    15.     iFile = FreeFile
    16.     Open sFile For Binary Access Read As iFile
    17.         sData = Space(LOF(iFile))
    18.         Get #iFile, , sData
    19.     Close iFile
    20.     FileToString = sData
    21. End Function
    22.  
    23. Sub StringToFile(ByVal sString As String, ByVal sFile As String)
    24.     Dim iFile As Integer
    25.    
    26.     If Len(Dir(sFile)) > 0 Then Kill sFile
    27.     iFile = FreeFile
    28.     Open sFile For Binary Access Write As iFile
    29.         Put #iFile, , sString
    30.     Close iFile
    31. End Sub

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2002
    Posts
    60
    the code have stopped at :

    Open sFile For Binary Access Write As iFile

    what sure i do??

  4. #4
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    What do you mean?

    Did you add 2 command buttons (cmdLoad, cmdSave) and a Multiline Textbox (Text1)?

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2002
    Posts
    60
    it says path/file access error, so i change i change the path to:

    App.Path & "\text.txt"

    but it still would not run.....

  6. #6
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    Do you have a C:\ drive (It's possible in W2K to have a different Drive Letter for the system drive.) or is the file File.txt already being used by something else, or do you not have Write permissions to the root of the Harddrive?

  7. #7

    Thread Starter
    Member
    Join Date
    Jan 2002
    Posts
    60
    thanks i got it working ......

    but there is one thing that i need to know,

    can i do indent on the text file, say have 2 rows of information

    like what i can do in Script????

    Using functions like:

    Skip(no of spaces)

    SkipLine

  8. #8
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    VB Code:
    1. sString = sString & vbCrLf ' Go To Next Line
    2. sString = sString & Space(20) ' Skip in 20 Spaces

  9. #9

    Thread Starter
    Member
    Join Date
    Jan 2002
    Posts
    60
    Thanks for your time and help,

    but last of all, when i have the spacing and lines, how do i read

    them??

    for ur information, I'm using matrix(array) to do calculation,

    but i have to check, save and read from the text file.

    i will have no problem writing it down(on the .txt) and check the

    procedures for constructing it but how do I read from the .txt

    and put it back into an array????

  10. #10
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    If you're storing data for calulcations why don't you store it in a CSV (Comma Separated Values) fashion,
    then you can use the Split() function to extract the values into an Array, i.e.

    VB Code:
    1. Dim sData As String
    2.     Dim vLine As Variant
    3.     Dim vValue As Variant
    4.     Dim lLine As Long
    5.        
    6.     sData = FileToString("C:\File.txt")
    7.  
    8.     vLine = Split(sData, vbCrLf)
    9.     For lLine = 0 To Ubound(vLine)
    10.         vValue = Split(vLine(lLine), ",")
    11.         ' Do Whatever with vValue Array
    12.     Next

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