Microsoft Office Tutorials and References
In Depth Information
If GetRegistryValue returned an empty string ( len = 0 ), c remains an empty string.
Otherwise, the appropriate connection string is created for an Access database using the value
extracted from the registry.
The same approach is used for SQL Server databases ( DBType = 1 ). The value for
DBWindowsAuth is extracted directly from the registry because its default value is not a
string, whereas the values for DBName , DBPassword , DBServer , and DBUserId are extracted
from the registry using the GetRegistryValue routine.
Unlike Access connection strings, the routine attempts to substitute intelligent values in case
the user hasn’t supplied a default value. For instance, if the length of DBServer is zero, the
program assumes that the user is running the query against the local computer. If the length
of DBName isn’t zero, the program attempts to build a partial connection string using DBName
and DBServer . Then, if Windows authentication is used, Integrated Security=SSPI is
appended to the connection string and the information in DBUserId and DBPassword
If DBWindowsAuth is False , the DBUserId and DBPassword are checked to see if they contain
a value. If both contain information, these fields are appended to the connection string. If
one of these fields doesn’t have any information, c is set to the empty string, destroying the
partial connection string that already exists.
If the user selected Advanced on the DBInfo form, the ConnectionString value is extracted
from the registry using the GetRegistryValue helper function. No further checking is required
here because GetRegistryValue ensures that an empty string is returned if the default value is
retrieved from the registry.
Finally, the routine ends by returning the temporary variable c as the value of the function.
The GetRegistryValue helper function calls the GetSetting function to retrieve the desired key
from the registry. If the return value is the same as the supplied default value, the return value
is set to the empty string.
Function GetRegistryValue(key As String, default As String) As String
r = GetSetting("Excel2k3 VBA", "Query", key, default)
If r = default Then
GetRegistryValue = r