Link to home
Start Free TrialLog in
Avatar of raymoshea
raymoshea

asked on

visual basic automatic updater

i'm using visual basic 6.0 and i am trying to make a gui that automatically reads and displays the contents of a file every hour. can this  be done?
Avatar of Ryan_Kempt
Ryan_Kempt
Flag of Canada image

Absolutely it can be. I would likely use a Timer object that you can drag-and-drop and enter the delay for. I'll also post up the code to read and display text (in a listbox).
Open "C:\myCoolFile.txt" For Input As #1
Do While Not EOF(1)
  Input #1, d
  lstMyListbox1.AddItem d
Loop
Close #1

Open in new window

I do apologize, I should have had that clear the listbox before reading in the file again. Be sure to add the following to the top of the previous code snippet:
lstMyListbox1.Clear

Open in new window

Yes, you can make it poll a file every hour.  If however you would like to update as soon as the file changes, this is also possible without polling the file.  Let me know, I will poste it.

If you want to poll, then use a timer control to call a routine.

Rayan's example (above) would populate a listbox.  There is a minor problem with his code if you did wish to use it - "Input #1, d" aught to be "Line Input #1, d"

This is because commas can split up the line reading without the "Line" prefix.

My function below could be pasted into a form or a module and used to return the entire contents of a file.  Also as explicit filenumbers are not used, it will not interfear with any other calls.

example usage:
  text1.text = getFileContents(app.path & "\text1.txt")

To get the file, try this sub routine

Function getFileContents(ByVal myFilePath As String) As String
  Dim ff As Integer, strIn as String
  ff = FreeFile 'Gets available file number
  Open myFilePath For Binary As ff
  strIn = String(Lof(ff)," ")
  Get #ff, 1, strIn
  Close ff
  getFileContents = strIn
End Function

Open in new window

Avatar of tasky
tasky

If you use a timer, set the interval to 60000 (1 minute or 60 seconds) and then have a variable which you increment until you hit 60 minutes. Then use any code above to read the file.
ASKER CERTIFIED SOLUTION
Avatar of dentab
dentab
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you, I hope that helped.