Hi,
I just wanted to save the details a user select from userforms (ie. from option boxes, and checklists) so that when they reload that userform the details they selected earlier was still on it. Not sure how to do it though???
Thanks.
Printable View
Hi,
I just wanted to save the details a user select from userforms (ie. from option boxes, and checklists) so that when they reload that userform the details they selected earlier was still on it. Not sure how to do it though???
Thanks.
Save it in a table if you want it saved between sessions, or in global variables if the same session. It could be saved to the registry, but don't know if VBA can do that.
Is there any examples of this on the forum?? I'm not sure how to thats all...i couldn't find it in the book im looking at...if there is a simple example that someone can show me it would be really good...
Thanks.
GetSetting and SaveSetting functions are part of the core of VBA.
They read/write to the "HKEY_CURRENT_USER\Software\VB and VBA Program Settings" section.
VB Code:
'Write to the registry: SaveSetting "MyName", "MySection", "MyKey", "MyValue" 'Read from the registry: Dim strString As String 'The default parameter is for when there is no value for tthe specified key strString = GetSetting("MyName", "MySection", "MyKey", "MyDefaultValue") MsgBox strString
thanks rob, i'll test this out once i get home, and hassle u if i have problems ;)
Your welcome and its not a hassle att all. Just post back if you need anything else. :)
hi rob,
i'm not sure how to apply the SaveSetting and GetSetting to my program. basically i am creating a home auto program, so for example a room in my house will give the user options for light settings (i.e dim, medium, or high), temp settings,light sensors etc. So if the user clicks on the option button of dim (light setting), i want it to return dim when i open it up again?? Don't know if its helpful but i've copied and pasted some of the form code below:
VB Code:
Private Sub HighButton_Click() ShowSettings End Sub Private Sub DimButton_Click() ShowSettings End Sub Private Sub MediumButton_Click() ShowSettings End Sub Public Sub ShowSettings() 'Declare variables Dim Info Dim LFCR LFCR = Chr(13) + Chr(10) 'Dim Level If DimButton.Value = True Then Info = "Dim Light Settings: ON" End If 'Medium Level If MediumButton.Value = True Then Info = Info + LFCR + "Medium Light Settings: ON" End If 'High Level If HighButton.Value = True Then Info = Info + LFCR + "High Light Settings: ON" End If LabelChoice.Caption = Info End Sub
One other issue is if the user is always on the same machine. If they may be on more than one machine, saving to the registry may not be appropriate because those settings are local to a particular machine, and won't be on other machines.
If the user has a roaming profile, maybe you can save settings there, but that's beyond my knowledge. Or create some kind of user table where you save settings in that case.
I would do something like this...
VB Code:
Option Explicit Private msSelection As String Private Sub HighButton_Click() msSelection = "High" UpdateCaption End Sub Private Sub DimButton_Click() msSelection = "Dim" UpdateCaption End Sub Private Sub MediumButton_Click() msSelection = "Medium" UpdateCaption End Sub Private Sub UpdateCaption() Select Case msSelection Case "Dim" LabelChoice.Caption = "Dim Light Settings: ON" LabelChoice.Caption = "Medium Light Settings: OFF" LabelChoice.Caption = "High Light Settings: OFF" Case "Medium" LabelChoice.Caption = "Dim Light Settings: OFF" LabelChoice.Caption = "Medium Light Settings: ON" LabelChoice.Caption = "High Light Settings: OFF" Case "High" LabelChoice.Caption = "Dim Light Settings: OFF" LabelChoice.Caption = "Medium Light Settings: OFF" LabelChoice.Caption = "High Light Settings: ON" End Select End Sub Private Sub Form_Load() 'Read from the registry: msSelection = GetSetting("MyName", "MySection", "MyKey", "Medium") UpdateCaption End Sub Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) 'Write to the registry: SaveSetting "MyName", "MySection", "MyKey", msSelection End Sub
hi rob...i gave it a shot but it doesnt work the program compiles but doesnt save it?? i'll try to play around with it and see if something comes up..
Doh! forgot to write it in VBA.
Use these two events instead.
VB Code:
Private Sub UserForm_Initialize() 'Form_load End Sub Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) 'Form_queryunload End Sub
no....it's not saving...
i tried to even incorporate it back into my earlier code, but won't work (it compiles and all, just doesnt save what i selected, so that when i open the form again its a clear form without the previous selection)..
Just another thing...how do i get a vertical scrollbar on a userform to scroll the form up and down?? i placed a scrollbar on my form and when i run it it shows up on my form but wont let me use the scroll to move the form up and down?? i don't understand why they don't write this kind of stuff in the latest text im reading...."Idiot's guide to Visual Basic" :rolleyes:
post your code again, as i can't see anything obvious wrong
pete
hi..there is nothing wrong with my code. it works fine and let user select options and checklists on the userform BUT does not save it so if they run the userform again...the options they previously selected are not on the userform...thats what im trying to find out..
i can't make any suggestions why it is not saving without seeing some of your code, robdogs suggested code should work fine
pete
hi..my code is pasted above...but here it is again:
VB Code:
Private Sub HighButton_Click() ShowSettings End Sub Private Sub DimButton_Click() ShowSettings End Sub Private Sub MediumButton_Click() ShowSettings End Sub Public Sub ShowSettings() 'Declare variables Dim Info Dim LFCR LFCR = Chr(13) + Chr(10) 'Dim Level If DimButton.Value = True Then Info = "Dim Light Settings: ON" End If 'Medium Level If MediumButton.Value = True Then Info = Info + LFCR + "Medium Light Settings: ON" End If 'High Level If HighButton.Value = True Then Info = Info + LFCR + "High Light Settings: ON" End If LabelChoice.Caption = Info End Sub
but where is the part to save your settings??
pete
Ok, two things going on here.
1. You say your compiling the UserForm but userforms in VBA can not be compiled unless you have Office XP Developer ed. So are you using VB6?
2. You havent made any of the changes to the code that will make it save any settings.
hi...im using vb6...and i tried to use the code that you supplied, but that doesnt save the settings either....its compiling and letting me run it but when i re-open it wont save the settings...this is drivng me crazy.....maybe i'll skip this part for the time being - i cant find anything in the texts either which is weird.
can you give me some help on scrollbars?? how do i get a vertical scrollbar on a userform to scroll the form up and down?? i placed a vertical scrollbar on my form and when i run it, it shows up on my form but wont let me use the scroll to move the form up and down?? how do i get it to work so that i am able to view the form using the scrollbar??
i've switched on to scrollbar at the moment...its the last thing i have left to complete - just the scrollbar and saving the userform....maybe i'll have better luck with the scrollbar....helppppppp
Here is a VB6 example for you to do the registry saving.
hi rob,
i tried the zip file...your code does work...but it does exactly what my code did that i pasted above. The thing is its still not saving. Maybe i've been saying this wrong....but basically i want the user to be able to select all the changes they made (i.e light settings to Dim) so that when they close the form and re-open it again...the form should have Dim light settings selected instead of no selection?? thats the only prob im having...maybe i should have used temp instead of light settings as thats easier to understand....i've pasted the code below...what i have is a label which is blank and a user text screen...when the user enters number into text screen it automatically gets entered into a label...but when i close the form and open it again, the label is empty - which should really have the number the user previously typed in earlier in the text box:
VB Code:
Private Sub TempVBox_Change() NewTemp End Sub Public Sub NewTemp() TempLabel.Caption = TempVBox.Value End Sub
im searchin on net for stuff currently maybe i'll get something by the end of today :eek: but sorry for confusing you and everyone else that has read the post and thank you very very very much for taking so much time out to help me :thumb:
Maybe we have a communication issue here, but the example will save the current selection upon close to the registry. Then upon
open of the program it loads the saved setting into the app and populates the label with that selection.
but i downloaded the zip file...and placed it onto new userform and tested it out...when i select the command button - the label tells me which button i selected (dim, med, or high) but then when i close the form and reopen it label is empty and doesnt show previous selection (whichever button i had pressed?)...
hmmm....what am i missing here...??
Are you pasting the code into VB6 IDE project or behind an Office app's VBA editor?
Plus, try just running my example project. It should be showing the previously selected setting in the label.
i tested rob's example, it worked perfectly as i expected it would
pete
yeah okay - then maybe im going absolutely crazy!!
Did you unzip the two files (.vbp and .frm) and just run that?
i opened up the .frm file and copied and pasted it in MSWORD then compiled macro..i don't know how else to run it apart fm putting it in word.
So then its NOT VB6, its Word VBA. That makes a world of difference.
Here is a demo userform that opens when you open the word document. Make sure your macros are enabled.
have to go to work now - be back in a few hours....will test it out when i get back in the afternoon...sorry for not specifically saying word VBA i assumed cos it was a VBA forum...and word was the only application used here...or not!! :blush:
Actually you assumed kind of correct. This is the VBA forum, but sometimes threads get posted here when it concerns automating an office
app from VB6 since they are so closely related.
I guess you threw me off with the "compiles" since office vba apps can not compile only vb6 can, basically.
I'll be back online tomorrow. Time for bed. I will check things out tomorrow to see how it went.
Have "fun" at work. :)
IT WORKS!!! Rob you are B-R-I-L-L-I-A-N-T :D Now of course i need to ask you a few more questions about this..
I have put the code you did into my code..and now my userform saves..just want to ask how do i use GetSetting and SaveSetting to put 2 more values?? For example here is my code below that i have used thus far:
VB Code:
Option Explicit Private msSelection As String Private Sub CloseButton_Click() Unload BathroomForm End Sub Private Sub HighButton_Click() msSelection = "High" UpdateCaption End Sub Private Sub DimButton_Click() msSelection = "Dim" UpdateCaption End Sub Private Sub MediumButton_Click() msSelection = "Medium" UpdateCaption End Sub Private Sub SensorLight_Click() UpdateCaption End Sub Private Sub TempVBox_Change() NewTemp End Sub Private Sub UpdateCaption() Dim Info Dim LFCR LFCR = Chr(13) + Chr(10) Select Case msSelection Case "Dim" Info = "Dim Light Settings: ON" Info = Info + LFCR + "Med Light Settings: OFF" Info = Info + LFCR + "HIGH Light Settings: OFF" Case "Medium" Info = Info + LFCR + "Dim Light Settings: OFF" Info = Info + LFCR + "Med Light Settings: ON" Info = Info + LFCR + "High Light Settings: OFF" Case "High" Info = Info + LFCR + "Dim Light Settings: OFF" Info = Info + LFCR + "Med Light Settings: OFF" Info = Info + LFCR + "High Light Settings: ON" End Select 'Light Sensoring - if tick in box then sensor "on" or else "off" If SensorLight.Value = 0 Then Info = Info + LFCR + "Light Sensors: OFF" Else Info = Info + LFCR + "Light Sensors: ON" End If LabelChoice.Caption = Info End Sub 'Temperature procedure which lets users enter a new temperature value that overwrites 'the current temperature value in the label. Public Sub NewTemp() TempVLabel.Caption = TempVBox.Value End Sub Private Sub UserForm_Initialize() 'Read from the registry: msSelection = GetSetting("MyName", "MySection", "MyKey", "Medium") UpdateCaption End Sub Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) 'Write to the registry: SaveSetting "MyName", "MySection", "MyKey", msSelection End Sub
how do i add the new temp input value and light sensor value to save also?? currently it is not saving either because i havent saved it to registery...i tried to add the light sensor case onto the UpdateCaption function and it worked but did not add it with the current light settings options..more like overwrote it (if that makes sense)....if you could let me know that would be good....but im just soooo happy most of it saves now.....thank youuuuuuuuuuuu
actually maybe its better if i put my code on what i put to "save" the new temp settings instead of just saying i couldnt get it to work.....(will work on the light sensor later)..this is what i put in but it doesn't save..so had to comment out..
VB Code:
Private tempSelection As String Private Sub TempVBox_Change() tempSelection = "Temp" NewTemp End Sub 'Temperature procedure which lets users enter a new temperature value that overwrites 'the current temperature value in the label. Public Sub NewTemp() Select Case tempSelection Case "Temp" TempVLabel.Caption = TempVBox.Value End Select End Sub Private Sub UserForm_Initialize() 'Read from the registry: msSelection = GetSetting("MyName", "MySection", "MyKey", "Medium") ' tempSelection = GetSetting("MyName", "MySection", "MyKey", "Temp") UpdateCaption 'NewTemp End Sub Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) 'Write to the registry: SaveSetting "MyName", "MySection", "MyKey", msSelection ' SaveSetting "MyName", "MySection", "MyKey", tempSelection End Sub
each setting you want save has to have its own keyname, so you give each savesetting a meaningful key name, maybe like this
SaveSetting "MyName", "Bedroom", "Light Setting", LblLight.Caption
SaveSetting "MyName", "Bedroom", "Temp Setting", LabelTemp.Caption
this assumes you would have label for each of your settings
"My Name" could also have relevance as the unit, location or something
you can save the value from a variable msSelection, or directly from the label
on form load, you will need to use getsetting to get all the values from the registry using the same section and key names as they are saved to and put the values in to the labels
pete
thanks westconn for the theory behind SaveSetting and GetSetting - makes much more sense to me now :) you know i've gone through 5 VB textbooks in the last couple of days and none of them had mentioned it.....anyway am off to bed now....will try my luck on this whole light sensor/temp thing tomorrow morning :eek:
go here for more information on getsetting and anything else you want to know about vb functions, most of which work in vba
pete
alright the temp is still not saving...tried some other ways but still not working...
Are you compfortable browsing the registry?
Also, you do have macros enabled in Word?
what do you mean "comfortable browsing registery"?? yep macros are enabled, the light settings is being saved thanks to you - but am trying to get the temp saved, am thinking maybe the UserForm_Initialize() only takes into account 1 getSetting?? umm...i've just copied and pasted what i have below. also just out of curiousity - is this ur full-time job rob??
VB Code:
Private tempSelection As String Private Sub TempVBox_Change() tempSelection = "Temp" NewTemp End Sub 'Temperature procedure which lets users enter a new temperature value that overwrites 'the current temperature value in the label. Public Sub NewTemp() Select Case tempSelection Case "Temp" TempVLabel.Caption = TempVBox.Value End Select End Sub Private Sub UserForm_Initialize() 'Read from the registry: msSelection = GetSetting("MyName", "MySection", "MyKey", "Medium") ' tempSelection = GetSetting("MyName", "TempSection", "TempKey", "Temp") UpdateCaption 'NewTemp End Sub Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) 'Write to the registry: SaveSetting "MyName", "MySection", "MyKey", msSelection ' SaveSetting "TempName", "TempSection", "TempKey", tempSelection End Sub