|
-
Nov 25th, 2006, 09:54 AM
#1
Thread Starter
Fanatic Member
[Partially Resolved] Damm logic!
Hey, i have no idea why this code doesn't want to work...
What it's suposed to do is mimic the SaveSettings generic function in VB.
Except this time it saves it into a file. I know it's just the way i'm thinking about it, but for the life of me i just can't figure it out.
It should work as such:
1. Open File, if it doesn't exist, call the Create_File sub
2. Check if the Option is there, if not create a new line and go to step 4
3. If it is there, then use the replace command to replace the VALUE and not the OPTION (the entire file gets replaced, but only the VALUE of the OPTION would be changed).
4. Write and save the file.
Here's how i constructed the file:
|X_=Data=_X| = Start of the file.
|X_=Row=_X| = Start of a new row
|X_=Column=_X| = Splits the OPTION from the VALUE
Here's a sample of what SHOULD happen:
|X_=Data=_X|
|X_=Row=_X|Form1.Left|X_=Column=_X|1990
|X_=Row=_X|Form1.Top|X_=Column=_X|5010
|X_=Row=_X|Text1.Text|X_=Column=_X|Hello Everyone!
It should extract something like if i were to load it (which i havent started to create yet):
Form1.Left = 1990
Form1.Top = 5010
Text1.Text = "Hello Everyone!"
And now for my code!
VB Code:
Public Main_HDD
Public Sub Save_Options(Company_Name As String, Program_Name As String, Key_ As String, Option_ As String, Optional Value_ As String = "")
Dim FileStr As String
Dim File_Sav As String
Dim ColsX() As String
Dim RowsX() As String
Dim DataX() As String
On Error Resume Next
Open Main_HDD & "\Program Settings\" & Company_Name & "\" & Program_Name & "\" & Key_ & ".ini" For Input As #1
Line Input #1, FileStr
Close #1
DataX() = Split(FileStr, "|X_=Data=_X|", -1, vbTextCompare)
RowsX() = Split(DataX(1), "|X_=Row=_X|", -1, vbTextCompare)
If UBound(DataX) < 0 Then
Create_New_File Company_Name, Program_Name, Key_
FileStr = "|X_=Data=_X|" & " " & "|X_=Row=_X|"
GoTo Data_1
End If
Dim I As Integer
If UBound(RowsX) = 0 Then GoTo Data_1
For I = 0 To UBound(RowsX)
MsgBox RowsX(I)
ColsX() = Split(RowsX(I), "|X_=Column=_X|", -1, vbTextCompare)
If ColsX(0) = Option_ Then GoTo Exit_For
Next I
Data_1:
File_Sav = FileStr & Option_ & "|X_=Column=_X|" & Value_ & "|X_=Row=_X|"
MsgBox File_Sav
GoTo Write_1
Exit_For:
File_Sav = Replace(FileStr, Option_ & "|X_=Column=_X|" & ColsX(1), Option_ & "|X_=Column=_X|" & Value_)
Write_1:
Open Main_HDD & "\Program Settings\" & Company_Name & "\" & Program_Name & "\" & Key_ & ".ini" For Output As #1
Print #1, File_Sav
Close #1
End Sub
Public Sub Create_New_File(Company_Name As String, Program_Name As String, Key_ As String)
On Error Resume Next
Dim File_Sav As String
File_Sav = "|X_=Data=_X|" & " " & "|X_=Row=_X|"
Open Main_HDD & "\Program Settings\" & Company_Name & "\" & Program_Name & "\" & Key_ & ".ini" For Output As #1
Print #1, File_Sav
Close #1
End Sub
Public Sub Do_Variables() 'This is called in sub main
Main_HDD = Environ("SystemDrive")
End Sub
My code is a bit wacked and stuff, but it's because i've been trouble shooting and testing, it has been cleaned up pretty good, but still.
Last edited by Slyke; Nov 25th, 2006 at 02:10 PM.
-
Nov 25th, 2006, 10:38 AM
#2
Re: Damm logic!
It seems like a helluva complicated way to store a few settings . I know it's not what you've asked for, but there's a very simple way of doing this, by using the APIs WritePrivateProfileStruct and GetPrivateProfileStruct. See my post #12 in this thread for a simple working example.
-
Nov 25th, 2006, 11:57 AM
#3
Thread Starter
Fanatic Member
Re: Damm logic!
Lol, it's not just for a few settings. I'll be using this method in all my programs as soon as it works perfectly, lol. It beats the registery in my oppinion.
-
Nov 25th, 2006, 12:05 PM
#4
-
Nov 25th, 2006, 12:06 PM
#5
Re: Damm logic!
having On Error Resume Next will make it difficult to debug because obviously no error will ever be displayed
-
Nov 25th, 2006, 12:12 PM
#6
Thread Starter
Fanatic Member
Re: Damm logic!
Yes, that's sort of what i'm looking for. But i just need it to work like the SaveSettings command does. The only difference is that it'll be saved as a file instead of to the registery.
When i try this code, either the file will be blank, or save the last option that i saved - depending if it finds the option already present or not.
For the On Error Thing, that was just so that it would create the file for me and if the file was blank, or currupted - it would still call the Create_File sub and wipe over it with the default format (The Data & then the Row character bits). The split will cause an error if its corrupted (or doesn't exist), and it will continue and create the new file.
Last edited by Slyke; Nov 25th, 2006 at 12:15 PM.
-
Nov 25th, 2006, 12:19 PM
#7
Re: Damm logic!
well as schoolbusdriver has already mentioned, that's what those APIs are for, creating INI files. If you want to be able to just dip in and out of the file then do it using the APIs, any other way is just going to involve a lot of pointless string manipulation
Edit: as to your problems, you're always opening the file for output, so that's going to overwrite everything each time. open for Append instead.
-
Nov 25th, 2006, 02:10 PM
#8
Thread Starter
Fanatic Member
Re: Damm logic!
Yeah, but to save his way you will need to create a user defined type for every setting you wanted to save?
Yes, i made it overwrite on purpose. It's suposed to load the entire file and then only replace one part of it, then write the entire file back onto the harddrive. If i used append, then i would get heaps of duplicates.
But the link you gave me looks like it works very well =D.
I have to go to bed now... but i'll test it sometime today.
Thanks!
-
Nov 25th, 2006, 02:15 PM
#9
Re: [Partially Resolved] Damm logic!
It would also help if you were to explain exactly what "doesn't want to work".
-
Nov 26th, 2006, 04:01 AM
#10
Thread Starter
Fanatic Member
Re: Damm logic!
 Originally Posted by Slyke
Yes, that's sort of what i'm looking for. But i just need it to work like the SaveSettings command does. The only difference is that it'll be saved as a file instead of to the registery.
When i try this code, either the file will be blank, or save the last option that i saved - depending if it finds the option already present or not.
For the On Error Thing, that was just so that it would create the file for me and if the file was blank, or currupted - it would still call the Create_File sub and wipe over it with the default format (The Data & then the Row character bits). The split will cause an error if its corrupted (or doesn't exist), and it will continue and create the new file.
Saying this, it only saves what i said to save last, so for example, if i tell it to:
Save_Settings "Form1.Left", Me.Left
Save_Settings "Form1.Top", Top.Left
Save_Settings "Form1.Caption", Me.Caption
Then it would only save the last setting, which was the form's caption.
-
Nov 26th, 2006, 06:47 AM
#11
Re: [Partially Resolved] Damm logic!
Since there's nesting, I would store the settings in an XML file and use the MSXML parser.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|