Results 1 to 27 of 27

Thread: Saving to a text file:

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Saving to a text file:

    First off, sorry for buggin you guys with these stupid "noob" posts, thanks for your help-im just a beginner

    okay, heres my code:
    Code:
    Private Sub Command1_Click()
    
       Dim hFile As Long
       Dim sFilename As String
    
       sFilename = (App.Path & "\demo.txt")
       
      'obtain the next free file handle from the
      'system and and save the text box contents
       hFile = FreeFile
       Open sFilename For Output As #hFile
          Print #hFile, Text1.Text, Text2.Text
       Close #hFile
       
    End Sub
    works good, but i want it so that if there is something already saved to demo.txt that it will not over write it..
    i cant figure out if i should be like
    if demo.txt = full then
    , Print #hFile, Text1.Text, Text2.Text

    hope you understand..thanks for all your help

  2. #2
    Addicted Member
    Join Date
    Jan 2005
    Posts
    131

    Re: Saving to a text file:

    you want to check if it is empty or if it has something in it already?
    I so use the LOF(SFileName).

    Cant remember the code off hand but it might go something like this:
    VB Code:
    1. if LOF(SFileName) > 0 then
    2.  msgbox "data in file"
    3. else
    4.  msgbox "File Empty"
    5. end if
    Things fall apart which the center cannot hold...

  3. #3

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Saving to a text file:

    Code:
    Open sFilename For Append As #hFile

    Will automatically start writing at the end of your file, leaving the current file informationt intact. You don't have to check to see if it is empty. If you want to overwrite it, just change APPEND back to OPEN

  5. #5
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,236

    Re: Saving to a text file:

    Just a little tip not that it will make a great deal of difference. hFile should be an integer and use FreeFile().

    VB Code:
    1. Dim hFile As Integer
    2. Dim sFilename As String
    3.  
    4. 'obtain the next free file handle from the
    5. 'system and and save the text box contents
    6. hFile = FreeFile()
    Or you could use a CommonDialog and
    VB Code:
    1. CommonDialog1.Flags = cdlOFNOverwritePrompt Or cdlOFNPathMustExist
    2. CommonDialog1.ShowSave
    Last edited by Keithuk; Jan 14th, 2005 at 08:26 PM.
    Keith

    I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

  6. #6

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Saving to a text file:

    @ dglienna...THANKS! works good, need to figure out how to make it read the dates from the .txt, and if the date is today make a message box..

  7. #7

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Saving to a text file:

    anyone know how to do that?
    ive heard :time: and :date: work, but not here

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Saving to a text file:

    you would have to open your file for input, read a line or the whole file or whatever is apporpriate, find the component that is the date you want
    isolate that and compare it with todays date, using the Date function.


    hope this helps pete

  9. #9

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Saving to a text file:

    okay i found this code:
    Code:
    Private Sub Form_Load()
    Dim InptStr As String
    Dim CompStr As String
    
    Open (App.Path & "\demo.txt") For Input As #(1)
      While Not EOF(1)
        Line Input #1, InptStr
        If Left(InptStr, Len(CompStr)) = CompStr Then
          ' Now you have a Line starting with the string you've been looking for
            End If
      Wend
    Close #1
    End Sub
    now how would i have the textbox appear with, if there is an event,the event name the time and date?

  10. #10
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Saving to a text file:

    I think you want the MSGBOX function.

    answer=msgbox("The data is " & compSTR & " !", vbOK)

    look it up. there are quite a few options as to what flags you use, like YesNo, Critical, Ok, Cancel. Whatever you want gets returned, and you can check if the answer is VbYES for instance.

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Saving to a text file:

    Private Sub Form_Load()
    Dim InptStr As String
    Dim CompStr As String

    Open (App.Path & "\demo.txt") For Input As #(1)
    While Not EOF(1)
    Line Input #1, InptStr
    If Left(InptStr, Len(CompStr)) = CompStr Then
    ' Now you have a Line starting with the string you've been looking for
    End If
    Wend
    Close #1
    End Sub
    line input reads 1 line from your file if the date you want is onevery line a the beginning, the left function, will get it, but depending how it is formatted in the file as to the length you need for the left, if the date is elswhere on the line isolating it would be slightly different

    you need to post a sample from your file

    the compstr would be the date you want to compare to, but as it is a date it also would have to be done differently, again depending how the date is formatted in the file.

    to place the information in a textbox you would add text1.text = Inptstr
    again this might need to formatted to make some sort of sense to the user

    as this is in a loop that goes to the end of the file the textbox would be overwritten each time todays date is found, if it could be in the file more than once, in which case you may need to use an array of text boxes to display all the results

    p.

  12. #12

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Saving to a text file:

    okay i meant msgbox, sorry and thankyou, ill look over and get back to you

  13. #13

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Saving to a text file:

    okay, heres an example, the demo.txt it creates posts this:

    Code:
    2/9/05        2:30          Birthday
    and i changed my values according to what you said (or i interpreted),
    Code:
    Private Sub Form_Load()
    Dim InptStr As String
    Dim CompStr As String
    
    Open (App.Path & "\demo.txt") For Input As #(1)
      While Not EOF(1)
        Line Input #(1), InptStr
        If Left(InptStr, Len(":Date:")) = (":Date:") Then
        answer = MsgBox("The data is " & CompStr & " !", vbOK)
          ' Now you have a Line starting with the string you've been looking for
            End If
      Wend
    Close #1
    End Sub
    if thats all okay, then i gotta move onto the messagebox
    Last edited by |2eM!x; Jan 15th, 2005 at 02:09 AM.

  14. #14
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Saving to a text file:

    If Left(InptStr, Len("ate:")) = ("ate:") Then
    answer = MsgBox("The data is " & CompStr & " !", vbOK)
    ' Now you have a Line starting with the string you've been looking for
    End If
    VB Code:
    1. Private Sub Form_Load()
    2. Dim InptStr As String
    3. 'Dim CompStr As String 'not used
    4. Dim mydate as Date
    5.  
    6.  
    7. Open (App.Path & "\demo.txt") For Input As #(1)
    8.   While Not EOF(1)
    9.     Line Input #(1), InptStr
    10.     mydate = Left(inptstr,Len(Date))
    11.     If mydate = Date Then
    12.     answer = MsgBox("The data is " & CompStr & " !", vbOK)
    13.       ' Now you have a Line starting with the string you've been looking for
    14.         End If
    15.   Wend
    16. Close #1
    17. End Sub


    you will have to change the bit about the date

    answer= msgbox(Inptstr)

    p.

  15. #15
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Saving to a text file:

    or MyDate

  16. #16

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Saving to a text file:

    okay got to see how it prints the date itself, with the word "now"
    printed "1/15/2005 1:40:14 AM"

    but i still cant get the darn textbox to popup with the event of the day
    this is what im trying
    Code:
    Open (App.Path & "\demo.txt") For Input As #(1)
      While Not EOF(1)
        Line Input #(1), InptStr
        If Left(InptStr, Len(Now)) = (Now) Then
        answer = MsgBox("The data is " & CompStr & " !", vbOK)
          ' Now you have a Line starting with the string you've been looking for
            End If
      Wend
    Close #1
    End Sub
    any ideas?

  17. #17
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Saving to a text file:

    you will have to change the bit about the date

    answer= msgbox(Inptstr)

    using Now won't give you the correct result because of the way the information is in the file, between the dae and the time is a gap, of unknown characters, probably spaces or tabs, using Date worked for me. if you need to use Now you will have to trim out the characters between the date and the time.

    change the msgbox line, so that it displays the information you want


    p.

  18. #18
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Saving to a text file:

    you have to build the date string.

    Code:
    Str = format(now, "MM") & "/" & format(now,"DD") & "/" & format(now,"YY")
    I'd bet that your string won't ever equal the current date and time. just use the date the way that I have it. if you use anything else, you could end up with 1 character strings. I had a filename end up as 1105 instead of 010105.
    I save the date in the filename itself.

  19. #19

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Saving to a text file:

    yeah your right, all i wanted was the date to be looked for, the time would be a pain..my friend told me to do split(now)(0) and it worked but you guys probably know better..

    heres all my forms check them out and tell me whats wrong, im really tired..

    http://s18.yousendit.com/d.aspx?id=0...801F0Y4K4ID0GB

  20. #20
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Saving to a text file:

    Code:
    Dim arr() As String
    If instr(InptStr, "  ") Then
    Do
      InptStr = Replace(InptStr, "  ", " ")
    Loop Until instr(InptStr, "  ")= 0
    endif
    arr() = split(InptStr, " ")
    will give you arr(0), arr(1), and arr(2) for date, time, and event.

    that will help, no doubt.

    EDIT: You link does not work.

  21. #21
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Saving to a text file:

    david,

    i tested it, pasting his sample from file to a str variable, then changed the numbers to todays date.

    i actually didn't expect it to work right away as my local date is dd/mm/yy
    and his file though it didn't say i took to be mm/dd/yy, though as todays date 15th is above 12 date sorts it out if it had been the 10th it may have been a problem. so yes using you method will fix that problem but you will need to give him a more complete example

    p.

  22. #22
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Saving to a text file:

    Just switch DD with MM and it should be fine.

    Are you talking about removing the spaces?

  23. #23
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Saving to a text file:

    david,

    our posts keep crossing

    i didn't see your previous post when i posted

    as he only wants the date, only need to read the first part.

    shouldn't have to change the way the date is formatted, whichever way it is done it should work for all locales, i have struggled with this before, and is another of those hard to test things if all your machines have the same locale set.

    p.

  24. #24

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Saving to a text file:

    Im So Confused!
    i dont understand what you guys mean at all

  25. #25
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Saving to a text file:

    post what you have, and describe the problem

  26. #26

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Saving to a text file:

    Code:
    Private Sub Command1_Click()
    
       Dim hFile As Long
       Dim sFilename As String
    
       sFilename = (App.Path & "\demo.txt")
       
      'obtain the next free file handle from the
      'system and and save the text box contents
       hFile = FreeFile
       Open sFilename For Append As #hFile
          Print #hFile, Text1.Text, Text2.Text, Text3.Text
       Close #hFile
       
    End Sub
    
    Private Sub Command2_click()
    Dim hFile As Long
       Dim sFilename As String
    sFilename = (App.Path & "\demo.txt")
       
    If vbYes = MsgBox("Are you SURE you want to clear all??", _
                          vbQuestion + vbYesNo, _
                          "Clear all?") Then
      
      'obtain the next free file handle from the
      'system and and save the text box contents
       hFile = FreeFile
       Open sFilename For Output As #hFile
          Print #hFile, " "
       Close #hFile
    End If
    End Sub
    
    Private Sub Form_Load()
    Dim InptStr As String
    Dim CompStr As String
    Dim strdate As String
    
    strdate = Split(Now)(0)
    If strdate = Now Then
        MsgBox (Now("There is an event scheduled"))
    
    Open (App.Path & "\demo.txt") For Input As #(1)
      While Not EOF(1)
        Line Input #(1), InptStr
        If Left(InptStr, Len(Now)) = (Now) Then
        answer = MsgBox("The data is " & CompStr & " !", vbOK)
          ' Now you have a Line starting with the string you've been looking for
            End If
      Wend
    Close #1
    End If
    End Sub
    this is my entire form 1, there are 3 textbox's,and 2 buttons

    everything works good except i cant get the program to read thru demo.text (on my desktop) and make a msgbox "EVENT FOUND: <"Event Name"/Now>,
    even though it writes to the demo.text, it cant read it..

    im really not good at this, so ' notes would be really appreciated

  27. #27
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Saving to a text file:

    Code:
    Option Explicit
    Private Sub Command1_Click()
       Dim arr() As String
       Dim Str As String
       Dim hFile As Long
       Dim sFilename As String
      ' Assuming that the first textbox is the Date "MM/DD/YY" but it
      ' doesn't really matter which way it is, As Long As it matches.
      ' You could also just Print it out without checking the date.
       sFilename = (App.Path & "\demo.txt")
       hFile = FreeFile
       Open sFilename For Append As #hFile
        arr() = Split(Text1.Text, "/")
        Str = arr(0) & "/" & arr(1) & _
              "/" & arr(2)
          Print #hFile, Str, Text2.Text, Text3.Text
       Close #hFile
    End Sub
    
    Private Sub Command2_click()
      Dim hFile As Long
      Dim sFilename As String
      sFilename = (App.Path & "\demo.txt")
      If vbYes = MsgBox("Are you SURE you want To clear all??", _
                          vbQuestion + vbYesNo, _
                          "Clear all?") Then
        'obtain the Next free file handle from the
        'system And and save the text box contents
         hFile = FreeFile
         Kill sFilename
       End If
    End Sub
    
    Private Sub Form_Load()
      Dim answer As String
      Dim InptStr As String
      Dim CompStr As String
      Dim strdate As String
      Dim Str As String
      Dim ff As Integer
      Dim arr() As String
      On Error GoTo FileError
      ff = FreeFile()
      Open (App.Path & "\demo.txt") For Input As #(ff)
      On Error GoTo 0
      Do While Not EOF(ff)
        Line Input #(ff), InptStr
        If InStr(InptStr, "  ") Then
          Do
            InptStr = Replace(InptStr, "  ", " ")
          Loop Until InStr(InptStr, "  ") = 0
        End If
        arr() = Split(InptStr, " ")
        Str = Format(Now, "MM") & "/" & Format(Now, "DD") & _
              "/" & Format(Now, "YY")
        If arr(0) = Str Then
        answer = MsgBox("The data is " & arr(2) & " !", vbOK)
          ' Now you have a Line starting with the String you've been looking For
        End If
        If arr(0) = Str Then
          MsgBox ("There is an Event scheduled")
        End If
      Loop
      Close #ff
      Exit Sub
    FileError:
      MsgBox "File Not found"
      On Error GoTo 0
    End Sub
    should get you started on the right track.

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