pjfatboy 05-25-2003, 08:58 PM I'm trying to save some settings to Windows Registry. I'm trying to save whether or not a menu item was checked or not. This is what I have so far. This is not working and I'm not sure what to put to test whether the item is checked.
Private Sub mnuClose_Click()
SaveSetting "Max Workout Program", "Startup", "Form1", mnuIntro.Checked = Not mnuIntro.Checked_
SaveSetting "Max Workout Program", "Startup", "Form1", mnuWeek1.Checked = Not mnuWeek1.Checked_
SaveSetting "Max Workout Program", "Startup", "Form1", mnuWeek2.Checked = Not mnuWeek2.Checked_
SaveSetting "Max Workout Program", "Startup", "Form1", mnuWeek3.Checked = Not mnuWeek3.Checked_
SaveSetting "Max Workout Program", "Startup", "Form1", mnuWeek4.Checked = Not mnuWeek4.Checked_
SaveSetting "Max Workout Program", "Startup", "Form1", mnuWeek5.Checked = Not mnuWeek5.Checked_
SaveSetting "Max Workout Program", "Startup", "Form1", mnuWeek6.Checked = Not mnuWeek6.Checked_
SaveSetting "Max Workout Program", "Startup", "Form1", mnuWeek7.Checked = Not mnuWeek7.Checked_
End Sub
OnErr0r 05-25-2003, 09:03 PM SaveSetting "Max Workout Program", "Startup", "Form1", CStr(mnuIntro.Checked)
pjfatboy 05-25-2003, 09:11 PM SaveSetting "Max Workout Program", "Startup", "Form1", CStr(mnuIntro.Checked)
Thanks
pjfatboy 05-25-2003, 09:17 PM SaveSetting "Max Workout Program", "Startup", "Form1", CStr(mnuIntro.Checked)
Thanks
I just tried it but it still doesn't work, do you see any other problems with my code?
Private Sub mnuClose_Click()
SaveSetting "Max Workout Program", "Startup", "Form1", CStr(mnuIntro.Checked)
SaveSetting "Max Workout Program", "Startup", "Form1", CStr(mnuWeek1.Checked)
SaveSetting "Max Workout Program", "Startup", "Form1", CStr(mnuWeek2.Checked)
SaveSetting "Max Workout Program", "Startup", "Form1", CStr(mnuWeek3.Checked)
SaveSetting "Max Workout Program", "Startup", "Form1", CStr(mnuWeek4.Checked)
SaveSetting "Max Workout Program", "Startup", "Form1", CStr(mnuWeek5.Checked)
SaveSetting "Max Workout Program", "Startup", "Form1", CStr(mnuWeek6.Checked)
SaveSetting "Max Workout Program", "Startup", "Form1", CStr(mnuWeek7.Checked)
Unload Me
End Sub
OnErr0r 05-25-2003, 11:22 PM Open the registry and take a look at what was written.
AntiSilence 05-26-2003, 03:28 AM SaveSetting "Max Workout Program", "Startup", "Form1", CStr(mnuIntro.Checked)
Thanks
I just tried it but it still doesn't work, do you see any other problems with my code?
Private Sub mnuClose_Click()
SaveSetting "Max Workout Program", "Startup", "Form1", CStr(mnuIntro.Checked)
SaveSetting "Max Workout Program", "Startup", "Form1", CStr(mnuWeek1.Checked)
SaveSetting "Max Workout Program", "Startup", "Form1", CStr(mnuWeek2.Checked)
SaveSetting "Max Workout Program", "Startup", "Form1", CStr(mnuWeek3.Checked)
SaveSetting "Max Workout Program", "Startup", "Form1", CStr(mnuWeek4.Checked)
SaveSetting "Max Workout Program", "Startup", "Form1", CStr(mnuWeek5.Checked)
SaveSetting "Max Workout Program", "Startup", "Form1", CStr(mnuWeek6.Checked)
SaveSetting "Max Workout Program", "Startup", "Form1", CStr(mnuWeek7.Checked)
Unload Me
End Sub
You seem to be trying to save the data to the SAME key ("Form1"). They need to be different otherwise it will be just overwriting itself each time and you'll end up the the last one (mnuWeek7.Checked) being set. Try changing your Key names to Week1, Week2... etc!
pjfatboy 05-26-2003, 08:16 AM SaveSetting "Max Workout Program", "Startup", "Form1", CStr(mnuIntro.Checked)
Thanks
I just tried it but it still doesn't work, do you see any other problems with my code?
Private Sub mnuClose_Click()
SaveSetting "Max Workout Program", "Startup", "Form1", CStr(mnuIntro.Checked)
SaveSetting "Max Workout Program", "Startup", "Form1", CStr(mnuWeek1.Checked)
SaveSetting "Max Workout Program", "Startup", "Form1", CStr(mnuWeek2.Checked)
SaveSetting "Max Workout Program", "Startup", "Form1", CStr(mnuWeek3.Checked)
SaveSetting "Max Workout Program", "Startup", "Form1", CStr(mnuWeek4.Checked)
SaveSetting "Max Workout Program", "Startup", "Form1", CStr(mnuWeek5.Checked)
SaveSetting "Max Workout Program", "Startup", "Form1", CStr(mnuWeek6.Checked)
SaveSetting "Max Workout Program", "Startup", "Form1", CStr(mnuWeek7.Checked)
Unload Me
End Sub
You seem to be trying to save the data to the SAME key ("Form1"). They need to be different otherwise it will be just overwriting itself each time and you'll end up the the last one (mnuWeek7.Checked) being set. Try changing your Key names to Week1, Week2... etc!
I don't see my program in the registry. Doesn't this code atomaticly creat "Max Workout Program" in the registry if it wasn't there before. Never mind I found it.
OnErr0r 05-26-2003, 08:26 AM You will see a single key at:
HKEY_CURRENT_USER\Software\VB and VBA Program Settings\Max Workout Program\Startup\Form1
Which is the value of the last menu item you saved. (mnuWeek7)
Good catch AntiSilence, I didn't even notice he wasn't changing the key.
pjfatboy 05-26-2003, 08:40 AM You will see a single key at:
HKEY_CURRENT_USER\Software\VB and VBA Program Settings\Max Workout Program\Startup\Form1
Which is the value of the last menu item you saved. (mnuWeek7)
Good catch AntiSilence, I didn't even notice he wasn't changing the key.
Yes and I fixed that by changing the key names. But now when I try to retrive the settings I get a mismatch error, heres my code, could you tell me were the problem is.
Private Sub Form_Load()
Dim intLastEntry As Integer
intLastEntry = GetSetting("Max Workout Program", "Startup", "Intro", CStr(mnuIntro.Checked))
intLastEntry = GetSetting("Max Workout Program", "Startup", "Week1", CStr(mnuWeek1.Checked))
intLastEntry = GetSetting("Max Workout Program", "Startup", "Week2", CStr(mnuWeek2.Checked))
intLastEntry = GetSetting("Max Workout Program", "Startup", "Week3", CStr(mnuWeek3.Checked))
intLastEntry = GetSetting("Max Workout Program", "Startup", "Week4", CStr(mnuWeek4.Checked))
intLastEntry = GetSetting("Max Workout Program", "Startup", "Week5", CStr(mnuWeek5.Checked))
intLastEntry = GetSetting("Max Workout Program", "Startup", "Week6", CStr(mnuWeek6.Checked))
intLastEntry = GetSetting("Max Workout Program", "Startup", "Week7", CStr(mnuWeek7.Checked))
Debug.Print intLastEntry
AntiSilence 05-26-2003, 08:41 AM Good catch AntiSilence, I didn't even notice he wasn't changing the key.
:D I had to look twice! It looked ok the first time I looked, then I was "oh, wait a minute!" lol
OnErr0r 05-26-2003, 08:53 AM Anti,
*hehe*
pj,
You're saving the boolean value (True/False) of whether the menu item is checked. GetSetting returns a string, If you CBool the output of GetSetting into an integer, you'll get -1 for True and 0 for false.
pjfatboy 05-26-2003, 09:01 AM Anti,
*hehe*
pj,
You're saving the boolean value (True/False) of whether the menu item is checked. GetSetting returns a string, If you CBool the output of GetSetting into an integer, you'll get -1 for True and 0 for false.
I'm sorry OnErrOr I'm just learning VB could you show me, @#*& I'm a visual learner. Thanks
OnErr0r 05-26-2003, 09:03 AM Debug.Print GetSetting("Max Workout Program", "Startup", "Week7", CStr(mnuWeek7.Checked))
Debug.Print CBool(GetSetting("Max Workout Program", "Startup", "Week7", CStr(mnuWeek7.Checked)))
Debug.Print CInt(CBool(GetSetting("Max Workout Program", "Startup", "Week7", CStr(mnuWeek7.Checked))))
AntiSilence 05-26-2003, 09:03 AM Yes and I fixed that by changing the key names. But now when I try to retrive the settings I get a mismatch error, heres my code, could you tell me were the problem is.
Private Sub Form_Load()
Dim intLastEntry As Integer
intLastEntry = GetSetting("Max Workout Program", "Startup", "Intro", CStr(mnuIntro.Checked))
intLastEntry = GetSetting("Max Workout Program", "Startup", "Week1", CStr(mnuWeek1.Checked))
intLastEntry = GetSetting("Max Workout Program", "Startup", "Week2", CStr(mnuWeek2.Checked))
intLastEntry = GetSetting("Max Workout Program", "Startup", "Week3", CStr(mnuWeek3.Checked))
intLastEntry = GetSetting("Max Workout Program", "Startup", "Week4", CStr(mnuWeek4.Checked))
intLastEntry = GetSetting("Max Workout Program", "Startup", "Week5", CStr(mnuWeek5.Checked))
intLastEntry = GetSetting("Max Workout Program", "Startup", "Week6", CStr(mnuWeek6.Checked))
intLastEntry = GetSetting("Max Workout Program", "Startup", "Week7", CStr(mnuWeek7.Checked))
Debug.Print intLastEntry
Right... When you saved the value of mnuWeek7.Checked, it would have saved the string True (or False) not a number (it being a Boolean toggle). So, when you read it back in, you will have to use either the CBool to convert to a boolean value to store in your integer
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week7", [default]))
(*[default] returned when registry key does not exist for example)
This would set intLastEntry to 0 for False or -1 for True. But again, your code is setting intLastEntry to mnuWeek7. Is that what you want? Does that make sense? :confused:
pjfatboy 05-26-2003, 09:30 AM Yes and I fixed that by changing the key names. But now when I try to retrive the settings I get a mismatch error, heres my code, could you tell me were the problem is.
Private Sub Form_Load()
Dim intLastEntry As Integer
intLastEntry = GetSetting("Max Workout Program", "Startup", "Intro", CStr(mnuIntro.Checked))
intLastEntry = GetSetting("Max Workout Program", "Startup", "Week1", CStr(mnuWeek1.Checked))
intLastEntry = GetSetting("Max Workout Program", "Startup", "Week2", CStr(mnuWeek2.Checked))
intLastEntry = GetSetting("Max Workout Program", "Startup", "Week3", CStr(mnuWeek3.Checked))
intLastEntry = GetSetting("Max Workout Program", "Startup", "Week4", CStr(mnuWeek4.Checked))
intLastEntry = GetSetting("Max Workout Program", "Startup", "Week5", CStr(mnuWeek5.Checked))
intLastEntry = GetSetting("Max Workout Program", "Startup", "Week6", CStr(mnuWeek6.Checked))
intLastEntry = GetSetting("Max Workout Program", "Startup", "Week7", CStr(mnuWeek7.Checked))
Debug.Print intLastEntry
Right... When you saved the value of mnuWeek7.Checked, it would have saved the string True (or False) not a number (it being a Boolean toggle). So, when you read it back in, you will have to use either the CBool to convert to a boolean value to store in your integer
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week7", [default]))
(*[default] returned when registry key does not exist for example)
This would set intLastEntry to 0 for False or -1 for True. But again, your code is setting intLastEntry to mnuWeek7. Is that what you want? Does that make sense? :confused:
So your saying I should type this
Dim intLastEntry As Integer
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Intro", [Default]))
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week1", [Default]))
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week2", [Default]))
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week3", [Default]))
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week4", [Default]))
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week5", [Default]))
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week6", [Default]))
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week7", [Default]))
Debug.Print intLastEntry
Instead of this
Dim intLastEntry As Integer
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Intro", CStr(mnuIntro.Checked)))
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week1", CStr(mnuWeek1.Checked)))
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week2", CStr(mnuWeek2.Checked)))
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week3", CStr(mnuWeek3.Checked)))
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week4", CStr(mnuWeek4.Checked)))
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week5", CStr(mnuWeek5.Checked)))
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week6", CStr(mnuWeek6.Checked)))
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week7", CStr(mnuWeek7.Checked)))
Debug.Print intLastEntry
AntiSilence 05-26-2003, 09:58 AM So your saying I should type this
Dim intLastEntry As Integer
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Intro", [Default]))
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week1", [Default]))
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week2", [Default]))
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week3", [Default]))
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week4", [Default]))
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week5", [Default]))
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week6", [Default]))
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week7", [Default]))
Debug.Print intLastEntry
Close! the [default] bit is where you specify a value that will be returned if the key ("Week7" for example) doesn't exist. Like, if "Week7" doesnt exisit, and it should be checked when the program is first run, set [default] to "-1" like:
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week7", "-1"))
pjfatboy 05-26-2003, 10:24 AM So your saying I should type this
Dim intLastEntry As Integer
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Intro", [Default]))
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week1", [Default]))
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week2", [Default]))
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week3", [Default]))
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week4", [Default]))
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week5", [Default]))
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week6", [Default]))
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week7", [Default]))
Debug.Print intLastEntry
Close! the [default] bit is where you specify a value that will be returned if the key ("Week7" for example) doesn't exist. Like, if "Week7" doesnt exisit, and it should be checked when the program is first run, set [default] to "-1" like:
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week7", "-1"))
But Week7 does exisit in the registry and it is set at false. All the keys are all there and have values True or False. What do I put where you have [default] to get the setting CStr(mnuWeek7.Checked) to come back as checked or not. I thought it needed to come back as true or false.
AntiSilence 05-26-2003, 11:26 AM intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week7", "-1"))
But Week7 does exisit in the registry and it is set at false. All the keys are all there and have values True or False. What do I put where you have [default] to get the setting CStr(mnuWeek7.Checked) to come back as checked or not. I thought it needed to come back as true or false.
You don't ned to put anything where [default] is, it's optional. The other part of the statement (to the end of "week7") will get the setting you want, so will work fine if you ommit [default], i.e.:
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week7"))
I'll try and explain the [default] part a bit better! When your program is first run on a system that does not have the "week7" key in it's registry (eg, on my PC) and your program tried to get it to set a menu checked or not, it can't get a key that does not exist yet (until your program creates it). Therefore, the default value is used to make it look right. If default is left off, a zero-length string is returned and may not set your menu checked property. So, for your "week7" mnuWeek7.Checked, when your program is first run (when no settings have been saved), is it set to True or False? If it's got to be true, then the [default] bit will be "-1" or "True" (CBool will convert "-1" to True as well as the string "True"). make sense? I'm not sure I can explain it any better!
pjfatboy 05-26-2003, 01:37 PM intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week7", "-1"))
But Week7 does exisit in the registry and it is set at false. All the keys are all there and have values True or False. What do I put where you have [default] to get the setting CStr(mnuWeek7.Checked) to come back as checked or not. I thought it needed to come back as true or false.
You don't ned to put anything where [default] is, it's optional. The other part of the statement (to the end of "week7") will get the setting you want, so will work fine if you ommit [default], i.e.:
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week7"))
I'll try and explain the [default] part a bit better! When your program is first run on a system that does not have the "week7" key in it's registry (eg, on my PC) and your program tried to get it to set a menu checked or not, it can't get a key that does not exist yet (until your program creates it). Therefore, the default value is used to make it look right. If default is left off, a zero-length string is returned and may not set your menu checked property. So, for your "week7" mnuWeek7.Checked, when your program is first run (when no settings have been saved), is it set to True or False? If it's got to be true, then the [default] bit will be "-1" or "True" (CBool will convert "-1" to True as well as the string "True"). make sense? I'm not sure I can explain it any better!
That does make sence. Thanks
pjfatboy 05-26-2003, 02:05 PM intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week7", "-1"))
But Week7 does exisit in the registry and it is set at false. All the keys are all there and have values True or False. What do I put where you have [default] to get the setting CStr(mnuWeek7.Checked) to come back as checked or not. I thought it needed to come back as true or false.
You don't ned to put anything where [default] is, it's optional. The other part of the statement (to the end of "week7") will get the setting you want, so will work fine if you ommit [default], i.e.:
intLastEntry = CBool(GetSetting("Max Workout Program", "Startup", "Week7"))
I'll try and explain the [default] part a bit better! When your program is first run on a system that does not have the "week7" key in it's registry (eg, on my PC) and your program tried to get it to set a menu checked or not, it can't get a key that does not exist yet (until your program creates it). Therefore, the default value is used to make it look right. If default is left off, a zero-length string is returned and may not set your menu checked property. So, for your "week7" mnuWeek7.Checked, when your program is first run (when no settings have been saved), is it set to True or False? If it's got to be true, then the [default] bit will be "-1" or "True" (CBool will convert "-1" to True as well as the string "True"). make sense? I'm not sure I can explain it any better!
That does make sence. Thanks
Works great thanks!
OnErr0r 05-26-2003, 05:54 PM The second line of code wiht the default entry should be preferable, that way you can make a menu item checked the first time they run the application, if needed.
The GetSetting line is merely retrieving the setting from the registry and storing it in a variable. At some point you need to set the Checked property of the menu from your variable.
mnuWeek7.Checked = intLastEntry
|