There are a few ways to go about using the registry, (the first way keeps the code inside a specific part of the registry called VB/VBA) the easiest are Called SaveSetting and GetSetting:
To start, lets save our forms X,Y values so it starts off in the same place as it last closed at:
VB Code:
Private Sub Form_Load() Me.Left = GetSetting(App.EXEName, "Options", "X")'Retrieves The value of X Me.Top = GetSetting(App.EXEName, "Options", "Y")'Retrieves the value of Y End Sub Private Sub Form_Unload(Cancel As Integer) SaveSetting App.EXEName, "Options", "X", Me.Left 'Saves the value of me.left to X SaveSetting App.EXEName, "Options", "Y", Me.Top 'Saves the value of me.top to Y End Sub
Now if you want to check if a registry key exists youd do the following:
VB Code:
If len(GetSetting(App.EXEName, "Options", "X"))>0'then it exists
--------
Now onto the hard way. This way is EXTREMELY useful. It lets you read and write ANYWHERE in the registry.
Download this Class Module:Reg.Cls
Applying the same thought as above, we will save the forms top and left coordinates on form unload, and load them on form load.
VB Code:
Option Explicit Dim reg As cRegistry Private Sub SaveSettings() 'Writes to Registry Set reg = New cRegistry 'This will create a new value in the Run section 'so your program will automatically run when Windows starts 'This will create a new section to store your own keys in reg.Classkey = HKEY_CURRENT_USER reg.SectionKey = "Software\My Company\My App" 'create the above key reg.CreateKey 'Save the Form settings to the registry reg.Valuetype = REG_DWORD reg.Valuekey = "FormTop" reg.Value = CLng(Me.Top) reg.Valuekey = "FormLeft" reg.Value = CLng(Me.Left) reg.Valuekey = "FormWidth" reg.Value = CLng(Me.Width) reg.Valuekey = "FormHeight" reg.Value = CLng(Me.Height) End Sub Private Sub GetSettings() 'Reads the Registry Set reg = New cRegistry reg.ClassKey = HKEY_CURRENT_USER reg.SectionKey = "Software\My Company\My App" reg.ValueKey = "FormTop" Me.Top = reg.Value reg.ValueKey = "FormLeft" Me.Left = reg.Value End Sub Private Sub Form_Load() Call GetSettings End Sub Private Sub Form_Unload(Cancel As Integer) Call SaveSettings End Sub
Now what ive done with these function below, is simplified EVERYTHING.VB Code:
''For Saving Private Function CreateKeys(HKey As String, SectionKey As String, ValueKey As String, Valuetype As String, Value As String) Set reg = New cRegistry 'This will create a new section to store your own keys in reg.Classkey = Hkey reg.SectionKey = SectionKey 'create the above key reg.CreateKey 'Save the Form settings to the registry reg.Valuetype = Valuetype reg.Valuekey = Valuekey reg.Value = Value End Function
Usage:VB Code:
Call CreateKeys(HKEY_CURRENT_USER, "SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyServer", Reg_sz, "80.2523.135.315:70")
For Reading:
VB Code:
Dim reg As cRegistry Private Function GetKeys(Hkey As String, Section As String, Valuekey As String) 'Reads the Registry Set reg = New cRegistry reg.Classkey = Hkey reg.SectionKey = Section reg.Valuekey = Valuekey Msgbox reg.value End Function
Usage:
VB Code:
Private Sub Form_Load() Call GetKeys(HKEY_CURRENT_USER, "SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyServer") End Sub
Hope this cleared some stuff up, it took me forever to understand how it all works, hope i helped you
![]()





Reply With Quote