Mark Anthony Biyo

Sample code

Home | About Visual Basic | History of Visual Basic | Sample code | Codes

Sample code

Here are some examples of the language:

Function that returns the area of a circle:

Private Function AreaOfCircle(Radius
                                    As Double) As Double
                                    Const PI = 3.14159265358979323846264
                                    AreaOfCircle = PI * (Radius ^ 2)
                                    End Function
                                    

Program to display a pop-up message box with the words "Hello World" on it:

Sub Main()
                                    MsgBox("Hello World")
                                    End Sub
                                    

Running Another Application Using Visual Basic:

Private Sub Run_Notepad()
                                    Shell("notepad.exe", MinimizedFocus)
                                    'This would open Notepad, as Notepad is in the system folder.
                                                                        '%SystemRoot% is an environment variable containing
                                    the path to the system folder.
                                                                        'which is not needed as it is already in the system
                                    folder
                                                                        End Sub
                                                                        



Visual Basic Source Code - Writing to the Registry

For: VB4, VB5, VB6

Visual Basic, by default using GetSetting and SaveSetting allows reading and writing only to the VB and VBA Programs key under HKEY_Current_User\Software. There can be any number of reasons to want to read or write other portions of the registry, and that can be accomplished using API calls. There are two typical ways that this is done; the first is with a Class Module, and the second is with a regular function or sub. I personally prefer the function/sub approach, which is shown in the code below. My earliest versions of this code were published on Compuserve in 1996, and have evolved steadily since then.

With this code, you can write strings to any portion of the registry for which you have permissions. A Read Function is on another page (link at bottom). First you need a number of declares. Not all of these are used for every registry access, but it will not hurt to include them in the declarations portion of the .BAS module that contains your function/subroutine. Copy/Paste from the box below:
Type SECURITY_ATTRIBUTES
        nLength As Long
        lpSecurityDescriptor As Long
        bInheritHandle As Long
End Type
Declare Function RegCreateKeyEx& Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hKey&, ByVal lpSubKey$, ByVal Reserved&, ByVal lpClass$, ByVal dwOptions&, ByVal samDesired&, lpSecurityAttributes As SECURITY_ATTRIBUTES, phkResult&, lpdwDisposition&)
Declare Function RegSetValueEx& Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey&, ByVal lpszValueName$, ByVal dwRes&, ByVal dwType&, lpDataBuff As Any, ByVal nSize&)
Declare Function RegCloseKey& Lib "advapi32.dll" (ByVal hKey&)
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const KEY_SET_VALUE = &H2&
Const KEY_CREATE_SUB_KEY = &H4&
Const REG_SZ = 1&
Const REG_DWORD = 4&
Const READ_CONTROL = &H20000
Const STANDARD_RIGHTS_WRITE = READ_CONTROL
Const Key_Write = STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY

Now we are ready for the subs themselves. If the value does not exist, it will be created. This sub will write string (REG_SZ) values:
Sub RegSetValue(H_KEY&, RSubKey$, ValueName$, RegValue$)
    'H_KEY must be one of the Key Constants
    Dim lRtn&         'returned by registry functions, should be 0&
    Dim hKey&         'return handle to opened key
    Dim lpDisp&
    Dim Sec_Att As SECURITY_ATTRIBUTES
    Sec_Att.nLength = 12&
    Sec_Att.lpSecurityDescriptor = 0&
    Sec_Att.bInheritHandle = False
    If RegValue = "" Then RegValue = " "
   
        lRtn = RegCreateKeyEx(H_KEY, RSubKey, 0&, "", 0&, Key_Write, Sec_Att, hKey, lpDisp)
        If lRtn <> 0 Then
            Exit Sub       'No key open, so leave
        End If
        lRtn = RegSetValueEx(hKey, ValueName, 0&, REG_SZ, ByVal RegValue, CLng(Len(RegValue) + 1))
        lRtn = RegCloseKey(hKey)
End Sub
I prefer a separate subroutine to accept numeric values. This sub will write number (REG_DWORD) values:
Sub RegSetValueNum(H_KEY&, RSubKey$, ValueName$, RegValue&)
    'H_KEY must be one of the Key Constants
    Dim lRtn&         'returned by registry functions, should be 0&
    Dim hKey&         'return handle to opened key
    Dim lpDisp&
    Dim Sec_Att As SECURITY_ATTRIBUTES
    Sec_Att.nLength = 12&
    Sec_Att.lpSecurityDescriptor = 0&
    Sec_Att.bInheritHandle = False
   
        lRtn = RegCreateKeyEx(H_KEY, RSubKey, 0&, "", 0&, Key_Write, Sec_Att, hKey, lpDisp)
        If lRtn <> 0 Then
            Exit Sub       'No key open, so leave
        End If
        lRtn = RegSetValueEx(hKey, ValueName, 0&, REG_DWORD, RegValue, 4)
        lRtn = RegCloseKey(hKey)
End Sub
 

Enter supporting content here