How to create/open a file on button click

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ielement
    New Member
    • Apr 2007
    • 1

    #1

    How to create/open a file on button click

    I'm new to programing in Visual Basic, but what im wondering is, how can i open a file and create a file by clicking a button?
    Like when i click button 1 it creates a file (to desktop) and when i click another button it opens a specific file(declared in code)
    Could anyone help? Thanks in advance
  • devonknows
    New Member
    • Nov 2006
    • 137

    #2
    Originally posted by ielement
    I'm new to programing in Visual Basic, but what im wondering is, how can i open a file and create a file by clicking a button?
    Like when i click button 1 it creates a file (to desktop) and when i click another button it opens a specific file(declared in code)
    Could anyone help? Thanks in advance
    I believe That something along these lines can help you, This allows you to use something like

    Code:
    Call SetFileContents(App.Path & "\MyFileName.txt", "This Is My Content")
    that will create a file called MyFileName.txt, and write This Is My Content to the file. And it all depends on what you want to do with the file that you have created, wether you want to use it as a temp file to store vars to, or just to make a program think that there is somehting there... etc etc.. its a bit like recycling... the possibilities are endless :P lol But i hope this helps you out...
    Code:
    Public Sub SetFileContents(ByVal file_name As String, ByVal contents As String)
    Dim fnum As Integer
    
        ' Open the file.
        fnum = FreeFile
        Open file_name For Output As fnum
    
        ' Write the file's contents (without an
        ' extra trailing vbCrLf).
        Print #fnum, contents;
    
        ' Close the file.
        Close fnum
    End Sub
    Kind Regards
    Devon.

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Originally posted by ielement
      I'm new to programing in Visual Basic, but what im wondering is, how can i open a file and create a file by clicking a button?
      Like when i click button 1 it creates a file (to desktop) and when i click another button it opens a specific file(declared in code)
      Could anyone help? Thanks in advance
      You would place code in the button's Click event procuedure to use either FileSystemObjec t or the simpler (but less useful) Open/Print/Close statements.

      For details on how to use these specific parts of the language, check your documentation or look up a good beginner VB tutorial - there are lots of them around, and you can use the Search tool on this page. Just try key words like "VB tutorial" and so on.

      Comment

      Working...