Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Storing/Restoring data from a file 2

Status
Not open for further replies.

Drofder2004

Technical User
Jan 3, 2005
10
GB
Hi, i'm a little stuck with a small project.

I am trying to create a multi-function tool for an online game. The problem is, I would like to be able to store IP addresses (which are typed into a total of 5 boxes incl. Port) into a file, which can later be restored.

As the file would contain a list of IP's the file would need to be loaded into a listbox or a combo box to select which IP address to restore.

Thanks in advance for any help and please remember I am very new to VB so be gentle :p

If there is something you feel I missed out or need more details, just ask and I will try my best to tell you.

btw, im using VB6.
 
There are many possibilities to do this:
Write into registry (if this is personal data, use SaveSetting,GetSetting)
Write into INI file (use standard API functions for this like GetPrivateProfileString and WritePrivateProfileString)
Write into plain text file (use Microsoft Scripting Library: FileSystemObject
Write into XML file (use DOM)
Write into Excel or any database (use ADO)
 
Thanks for the reply, do you have any sugestions as to which option is the most user friendly and easiest to create.

I do not want it to be registry as the program will not be installed (once the data is completed, invalid registries will be present).

Excel sounds the easiest and most common way to do it, but text files would be useful as the user can edit it without the need for excel.

XML, not sure what that is, but am sure it is databse related :S

The stored info will be 5 seperate numbers and 1 comment (text)
And the restored information needs to be restored from preferably a combo box.

Thanks.
 
Have a quick look through faq222-2244 which shows you how to get the best from these forums. Then look at the FAQ button at the top of this panel - you will find several faqs which show you how to use INI files, and at least one on how to use XML

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first
'If we're supposed to work in Hex, why have we only got A fingers?'
Essex Steam UK for steam enthusiasts
 
Thanks, I've never noticed the FAQ section :S

I would still prefer an opinion of which options is the best way before I look further. I do not want to dive into something that can be done in a more simple method.
 
The easiest way is to either use File Scripting Object or just to use combination of Put, Get, Seek, and Open VB statements. Search in this forum should produce the examples (as well as VB Help)
 
I just posted sample code not too long ago in this thread. thread222-1073644

Hope it helps.


-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Sorry, but after a long search of the forums and even reading that example code in from gmmastros I am still stuck.

The only information I have found on FSO is really complex and way out of my capabilities.

Is there no simple way of writing/reading to/from a file :S

Btw, I said I was using 5 text boxes to write the information to a file, but an easier way would be to make a string from those 5 boxes and write the string to a file?

Thanks for all the help so far,but I am still quite lost on what to do :(
 
Set reference to Microsoft Scripting Runtime first.

This is the simplest example:

Private Sub WriteToFile()

Dim objFSO As New FileSystemObject
Dim objTxtStrm As TextStream

Set objTxtStrm = objFSO.OpenTextFile("C:\test.txt", IOMode:=ForWriting, Create:=True)

objTxtStrm.WriteLine Text1.Text & "," & Text2.Text & "," & Text3.Text & "," & Text4.Text & "," & Text5.Text

objTxtStrm.Close

Set objFSO = Nothing
Set objTxtStrm = Nothing

End Sub

Private Sub ReadFile()

Dim objFSO As New FileSystemObject
Dim objTxtStrm As TextStream

Set objTxtStrm = objFSO.OpenTextFile("C:\test.txt", IOMode:=ForReading)

MsgBox objTxtStrm.ReadLine

objTxtStrm.Close

Set objFSO = Nothing
Set objTxtStrm = Nothing

End Sub
 
Here is a simple example.

Open a new VB project, then add 5 text boxes to the form.

Copy/paste this code to the code window.

Code:
Private Sub Form_Load()
    
    Dim iFile As Integer
    Dim Data1 As String
    Dim Data2 As String
    Dim Data3 As String
    Dim Data4 As String
    Dim Data5 As String
    
    
    ' make sure the file exists
    If Dir("C:\Data.txt") <> "" Then
        
        ' Get a file file handle
        iFile = FreeFile
        
        ' open the file for reading
        Open "C:\Data.txt" For Input As #iFile
        
        ' get the 5 data values
        Line Input #iFile, Data1
        Line Input #iFile, Data2
        Line Input #iFile, Data3
        Line Input #iFile, Data4
        Line Input #iFile, Data5
        
        ' close the file
        Close #iFile
        
    End If
    
    ' set the text property of the text boxes
    Text1.Text = Data1
    Text2.Text = Data2
    Text3.Text = Data3
    Text4.Text = Data4
    Text5.Text = Data5
    
End Sub

Private Sub Form_Unload(Cancel As Integer)
    
    Dim iFile As Integer
    
    ' get a free file handle
    iFile = FreeFile
    
    ' open the file for writing
    Open "C:\Data.txt" For Output As #iFile
    
    ' Save the 5 text boxes
    Print #iFile, Text1.Text
    Print #iFile, Text2.Text
    Print #iFile, Text3.Text
    Print #iFile, Text4.Text
    Print #iFile, Text5.Text
    
    ' close the file
    Close #iFile
    
End Sub

I would not recommend this method for larger files because reading and writing to the file on a line-by-line basis can be slow.

With 5 lines in the file (or even hundreds) the performance should be pretty good. If you had thousands of lines in the file, I would do things a little differently.

This site is NOT supposed to provide complete solutions. You haven't posted any of your own code. Please, read the code, see if you understand it. If you don't, post back with questions. We'll explain further.



-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Thanks both for the codes, I will adapt them in to what I have so far and see how far I can get.

I have not posted any of my own code as the only code I have is for other thigs on my form not related to this. I had no code as I had no idea where to start (which is why I asked for suggestions in the first post). :)

Thanks again.
 
Ok, I have got it mostly working. The only problem is multiple lines (I used vladk's example code). When using the code, I have been able to produce a text with only 1 line.

How would i go about having multiple lines (for when an extra IP is added)?

Also, not neccesary but could be nice, is it possible to load the data straight back into the individual textboxes or load them into a list/combo box? (If the only way to do this is a lot of scripting, then don't bother, there are other ways I can get around that).

 
multiple lines ...

Private Sub WriteToFile()

Dim objFSO As New FileSystemObject
Dim objTxtStrm As TextStream
Dim intNumberOfLines As Integer

Set objTxtStrm = objFSO.OpenTextFile("C:\test.txt", IOMode:=ForWriting, Create:=True)

For intNumberOfLines = 1 To 3

objTxtStrm.WriteLine "Line " & CStr(intNumberOfLines) & " " & Text1.Text & "," & Text2.Text & "," & Text3.Text & "," & Text4.Text & "," & Text5.Text
Next intNumberOfLines

objTxtStrm.Close

Set objFSO = Nothing
Set objTxtStrm = Nothing

End Sub

Private Sub ReadFile()

Dim objFSO As New FileSystemObject
Dim objTxtStrm As TextStream

Set objTxtStrm = objFSO.OpenTextFile("C:\test.txt", IOMode:=ForReading)

Do While Not objTxtStrm.AtEndOfStream
MsgBox objTxtStrm.ReadLine
Loop

objTxtStrm.Close

Set objFSO = Nothing
Set objTxtStrm = Nothing

End Sub


Since ReadLine returns a string you can split it into the fragments of your choice and load it back into your textboxes. You can use Split function for this. It returns an array of substrings. Read VB Help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top