Results 1 to 4 of 4

Thread: problem loading and saving file

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    182

    problem loading and saving file

    i am using the following code which checks if a file exists if not it creates a file

    Code:
    Private Sub cmdload_Click()
    
    Dim fs As Object
    Set fs = CreateObject("Scripting.FileSystemObject")
    
    If FileExists("test.txt") Then
                Open "est.txt" For Input As #1
                    Print #1, label1.Caption
                Close #1
        Else
            Call create_test
        End If
    End Sub
    
    Sub create_test()
    
        Open "test.txt" For Append As 1
         
            Print #6, label1.Caption
        Close 6
      
    End Sub
    
    Function FolderExists(sPath As String) As Boolean
        FolderExists = CreateObject("Scripting.FileSystemObject").FolderExists(sPath)
    End Function
    
    Function FileExists(sFullFileName As String) As Boolean
        FileExists = CreateObject("Scripting.FileSystemObject").FileExists(sFullFileName)
    End Function
    but when i click load it says "bad file mode"

    what want to do is if after click load the file does not exist then it creates a file and loades the value in label1. but it seems i am doing something wrong. any ideas

  2. #2
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    Re: problem loading and saving file

    To check whether if the file exists and write, you can use just the Append mode as below

    Open "G:\test.txt" For Append As #1
    Print #1, "abcdefghi"
    Close #1

    it will take care of creating a file if not exists...and keep appending if one exists...
    Im Sorry, I dint go thru your post fully...
    If an answer to your question has been helpful, then please, Rate it!

    Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.


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

    Re: problem loading and saving file

    print #1 tries to write to a file that is open for input hence bad file mode,
    you need to use input or lineinput to read the file
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: problem loading and saving file

    Open "est.txt" For Input As #1
    Print #1, label1.Caption
    Close #1
    Yeah, westconn1 is right.
    For inputting, it should be
    Code:
    Open "est.txt" For Input As #1
                    Line Input #1, strTemp
                Close #1
    or
    Code:
    Open "est.txt" For Input As #1
                    Input #1, strTemp
                Close #1
    And for writing to a file,
    Code:
    Open "est.txt" For Output As #1
                    Print #1, strTemp
                Close #1

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

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