By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,197 Members | 1,151 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,197 IT Pros & Developers. It's quick & easy.

How to create/open a file on button click

P: 1
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
Apr 15 '07 #1
Share this Question
Share on Google+
2 Replies


devonknows
100+
P: 137
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

Expand|Select|Wrap|Line Numbers
  1. 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...
Expand|Select|Wrap|Line Numbers
  1. Public Sub SetFileContents(ByVal file_name As String, ByVal contents As String)
  2. Dim fnum As Integer
  3.  
  4.     ' Open the file.
  5.     fnum = FreeFile
  6.     Open file_name For Output As fnum
  7.  
  8.     ' Write the file's contents (without an
  9.     ' extra trailing vbCrLf).
  10.     Print #fnum, contents;
  11.  
  12.     ' Close the file.
  13.     Close fnum
  14. End Sub
Kind Regards
Devon.
Apr 15 '07 #2

Expert 5K+
P: 8,434
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 FileSystemObject 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.
Apr 15 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.