+ Reply to Thread
Results 1 to 12 of 12

Using an INI to save and restore check box values

  1. #1
    Registered User
    Join Date
    11-19-2008
    Location
    Oxford, UK
    MS-Off Ver
    2003 (Work) / 2007 (Home)
    Posts
    60

    Using an INI to save and restore check box values

    Hello,
    I've created an add in from which I am trying to build user settings but am getting stuck returning the stored values in the INI file.
    Please help.

    Code I have has is:-
    Please Login or Register  to view this content.
    Last edited by SystemsAccountant; 04-19-2010 at 04:07 AM. Reason: Solved

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Using an INI to save and restore check box values

    Hello System Accountant,

    The first rule of using API calls: Do not take short cuts when coding!

    It appears you have the API calls in the General Declarations section of the UserForm. The calls won't be available until the UserForm is loaded. These calls need to moved into a separate VBA module.

    Copy this code into its own VBA module. This contains 2 new macro that will be called by the UserForm. I added these for safety. The API does little error checking and if you are not familiar will all the options a call has, things can get screwed up fast.
    Please Login or Register  to view this content.


    UserForm Code
    Please Login or Register  to view this content.
    Here are the API calls with comments about how they function...
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    11-19-2008
    Location
    Oxford, UK
    MS-Off Ver
    2003 (Work) / 2007 (Home)
    Posts
    60

    Re: Using an INI to save and restore check box values

    Excellent Leith, thank you
    I have no real idea about most of the code and I've just been searching the web and playing with the code for many hours trying to get it to work.

    I've put the code in but get a compile error on:-

    Please Login or Register  to view this content.
    Within the Writeprivatedata


    Is this because the INI file does not exist yet? I of course would like it to write one if one does not exist yet with a default value of true.

    Kind Regards
    Matt

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Using an INI to save and restore check box values

    Hello Matt,

    The API will create the file if it doesn't exist. The problem is I didn't change my API code to match yours. I have made the correction in this version. The code will works correctly with Windows 2000 through Xp. If you are using 2007 or later, I am not sure it work correcly. Microsoft has made no notations to the API page about this. If you still have problems, post your workbook so I can review it.
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    11-19-2008
    Location
    Oxford, UK
    MS-Off Ver
    2003 (Work) / 2007 (Home)
    Posts
    60

    Re: Using an INI to save and restore check box values

    Thanks Leith, still have the same problem I'm afraid.

    Code used within the Userform:-
    Please Login or Register  to view this content.
    Code used on a separate module
    Please Login or Register  to view this content.

    I'm not really sure about any of this but as far as I can see I can't see anything that stipulates the INI file itself and it's directory (current directory of active workbook)?

    I tried attaching the file - it's an excel add in so resaved as abn xls file.

    Kind Regards
    Matt
    Attached Files Attached Files

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Using an INI to save and restore check box values

    Hello Matt,

    Okay, I found the problem. My mistake. I forgot that I created WritePrivateData as a SUb and not a Function. The error occurs because I was trying to assign it a return value. Copy this code over the WritePrivateData code of the macro in the UserProfileSettings.

    Correct Code
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    11-19-2008
    Location
    Oxford, UK
    MS-Off Ver
    2003 (Work) / 2007 (Home)
    Posts
    60

    Re: Using an INI to save and restore check box values

    Cheers Leith,

    Working now with the exception that when the userform is called it does not return the stored INI setting.

    It looks like when I run the userform and click the checkbox it's not passing this through to INI file.


    Separate question:
    Do I need to use CInt to pass the results of the checkbox to the INI file? Would it work just as well with True / False. I tried this and it doesn't help with the above problem.

    Regards
    Matt
    Attached Files Attached Files

  8. #8
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,015

    Re: Using an INI to save and restore check box values

    FWIW, you could also just write to the registry using SaveSetting and GetSetting with no APIs needed.
    Everyone who confuses correlation and causation ends up dead.

  9. #9
    Registered User
    Join Date
    11-19-2008
    Location
    Oxford, UK
    MS-Off Ver
    2003 (Work) / 2007 (Home)
    Posts
    60

    Re: Using an INI to save and restore check box values

    Hello,

    I still have not been able to figure out how to pull the value from the INI file into the userform checkbox.

  10. #10
    Registered User
    Join Date
    11-19-2008
    Location
    Oxford, UK
    MS-Off Ver
    2003 (Work) / 2007 (Home)
    Posts
    60

    Re: Using an INI to save and restore check box values

    I've not got this working.

    Created a message box in a module instead which works easy enough. Couldn't work out how to get the userform working.

  11. #11
    Forum Contributor
    Join Date
    08-21-2008
    Location
    Hamilton, New Zealand
    MS-Off Ver
    Office 2007
    Posts
    255

    Re: Using an INI to save and restore check box values

    Refer to attched workbook example with User Form prererence get, save & reset
    Attached Files Attached Files

  12. #12
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Using an INI to save and restore check box values

    FWIW, you could also just write to the registry using SaveSetting and GetSetting with no APIs needed.
    Or store the users' setting on a hidden sheet, with control names down the left column, usernames across the top, and control values in the body of the table.
    Entia non sunt multiplicanda sine necessitate

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1