Results 1 to 11 of 11

Thread: [Partially Resolved] Damm logic!

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Thumbs up [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:
    1. Public Main_HDD
    2.  
    3. Public Sub Save_Options(Company_Name As String, Program_Name As String, Key_ As String, Option_ As String, Optional Value_ As String = "")
    4.  
    5. Dim FileStr As String
    6. Dim File_Sav As String
    7. Dim ColsX() As String
    8. Dim RowsX() As String
    9. Dim DataX() As String
    10.  
    11. On Error Resume Next
    12.  
    13. Open Main_HDD & "\Program Settings\" & Company_Name & "\" & Program_Name & "\" & Key_ & ".ini" For Input As #1
    14. Line Input #1, FileStr
    15. Close #1
    16.  
    17. DataX() = Split(FileStr, "|X_=Data=_X|", -1, vbTextCompare)
    18.  
    19. RowsX() = Split(DataX(1), "|X_=Row=_X|", -1, vbTextCompare)
    20.  
    21. If UBound(DataX) < 0 Then
    22. Create_New_File Company_Name, Program_Name, Key_
    23. FileStr = "|X_=Data=_X|" & " " & "|X_=Row=_X|"
    24. GoTo Data_1
    25. End If
    26.  
    27. Dim I As Integer
    28.  
    29. If UBound(RowsX) = 0 Then GoTo Data_1
    30.  
    31. For I = 0 To UBound(RowsX)
    32. MsgBox RowsX(I)
    33. ColsX() = Split(RowsX(I), "|X_=Column=_X|", -1, vbTextCompare)
    34. If ColsX(0) = Option_ Then GoTo Exit_For
    35. Next I
    36.  
    37. Data_1:
    38.  
    39. File_Sav = FileStr & Option_ & "|X_=Column=_X|" & Value_ & "|X_=Row=_X|"
    40. MsgBox File_Sav
    41. GoTo Write_1
    42.  
    43. Exit_For:
    44.  
    45. File_Sav = Replace(FileStr, Option_ & "|X_=Column=_X|" & ColsX(1), Option_ & "|X_=Column=_X|" & Value_)
    46.  
    47. Write_1:
    48.  
    49. Open Main_HDD & "\Program Settings\" & Company_Name & "\" & Program_Name & "\" & Key_ & ".ini" For Output As #1
    50. Print #1, File_Sav
    51. Close #1
    52.  
    53. End Sub
    54.  
    55.  
    56.  
    57. Public Sub Create_New_File(Company_Name As String, Program_Name As String, Key_ As String)
    58.  
    59. On Error Resume Next
    60.  
    61. Dim File_Sav As String
    62.  
    63. File_Sav = "|X_=Data=_X|" & " " & "|X_=Row=_X|"
    64.  
    65. Open Main_HDD & "\Program Settings\" & Company_Name & "\" & Program_Name & "\" & Key_ & ".ini" For Output As #1
    66. Print #1, File_Sav
    67. Close #1
    68.  
    69. End Sub
    70.  
    71.  
    72. Public Sub Do_Variables() 'This is called in sub main
    73.  
    74. Main_HDD = Environ("SystemDrive")
    75.  
    76. 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.

  2. #2
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    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.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    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.

  4. #4

  5. #5
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Damm logic!

    having On Error Resume Next will make it difficult to debug because obviously no error will ever be displayed
    Chris

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    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.

  7. #7
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    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.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    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!

  9. #9

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Damm logic!

    Quote 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.

  11. #11
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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
  •  



Click Here to Expand Forum to Full Width