Visual Basic Q&A

As a software engineer, I focus on .NET, especially asp.net, C#, WCF and so on, and I am also very interested in Search Engine Optimization.

Entries Tagged ‘Key’

INFO: How Setup Wizard and PDW Use Dependency Files

Symptoms
A dependency (.DEP) file contains information about the run-timerequirements of an application or component; it contains informationregarding which files are needed, how the files are to be registered, andwhere they should be installed on the target computer. You can createdependency files for standard Visual Basic projects, ActiveX controls,ActiveX documents, and other ActiveX components.
When a component is used in another project, the dependency information forthe component is incorporated into the project’s dependency information.This dependency information is used by the Setup Wizard (known as thePackage and Deployment Wizard or PDW in Visual Basic 6.0). When you use theSetup Wizard or PDW to generate a setup for your project, the wizard refersto the project’s dependency information to determine which files should bedistributed with your project and how those files should be installed.
All of the ActiveX controls that ship with Visual Basic have a companiondependency file. It is strongly recommended that you generate a dependencyfile for your component, especially if you intend that the component beused in other Visual Basic projects. The Setup Wizard and PDW give you theoption to generate a dependency file for your project or component. Thedependency file may also be generated using a text editor if you cannot usethe Setup Wizard or PDW to create one. The remainder of this articledescribes the syntax for a dependency file.
Resolution
A dependency file is a standard Windows .INI file and can be read andwritten to using the standard Windows APIs (GetPrivateProfileString andWritePrivateProfileString).
There are a few items to note for dependency files:
Dependency files may have multiple sections. The section orderis not significant.Entries are not case-sensitive.Comments are allowed when preceded by a semi-colon (;).The dependency file name is important. The file name prefixmust match the file name of the component it describes. Forexample, a dependency file MyOCX.DEP would correspond to acomponent, such as MyOCX.OCX or MyOCX.DLL.Each section in a .DEP file uses the following syntax:

[Component File Name]Dest=<Destination>UsesN=<Dependent File>Register=<Method of Registration>Version=<Component version>Date=<Component Date>Time=<Component Time>ProgramIconTitle=<Title for Shortcut>ProgramIconCmdLine=<Command Line for Shortcut>
Dest=The Dest= key indicates the destination folder for the component. Thisvalue may be a relative path, a full path, or a path based on any of thefollowing macros (for example, it may be simply a macro, or it may be amacro followed by a backslash and a relative path):

$(AppPath)Indicates the path the user selectsduring setup. $(WinSysPath)Indicates the \Windows\System or\Winnt\System32 folder. $(WinPath)Indicates the \Windows or \Winnt folder. $(ProgramFiles)Indicates the folder specified atHKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\ProgramFilesDir. $(CommonFiles)Indicates the folder specified atHKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\CommonFilesDir. $(CommonFilesSys)Indicates $(CommonFiles)\System inWindows 95, Windows 98, NT 4.0, and Windows 2000,or the same as $(WinSysPath) in NT 3.51. $(MSDAOPath)The location stored in the registryfor DAO components. The Dest= key applies only to the component to which the section applies.By default, all files dependent on the component will also be placed in thesame location. To override the location for a “child,” you can create asection for the “child” and specify a different destination in thatsection.
UsesN=The UsesN= key (where N is an integer) indicates a dependent file for thecomponent. A file specified by a UsesN= key is required for the componentdefined by that section. In a section, N must be sequential and must startwith 1. For an illustration, refer to the following sample section:

[MyOCX.OCX]Dest=$(WinSysPath)Uses1=MyDLL.DLLUses2=MyOCX.TXT In this example, MyOCX.OCX depends upon (or requires) MyDLL.DLL andMyOCX.TXT.
NOTE: If the UsesN keys are not ordered sequentially, or if a number isskipped, dependent files may not be recognized in the section and may notbe acknowledged by the Setup Wizard or PDW.
Register=The Register= key indicates whether the file should be registered and, ifso, how it should be registered. The valid values for this key are:

NoneIf a value is omitted for this key,the file is not registered.$(DllSelfRegister)The file supports self-registrationand self-unregistration via a DLLentry point.$(ExeSelfRegister)The file is a local server thatsupports self-registration throughthe command-line parameter”/RegServer” and self-unregistrationthrough the command-line parameter”/UnRegServer.”$(TLBRegister)The file is a .TLB file and should beregistered.FilenameThe file is registered by callingRegedit.exe with this filename as aCommand-line parameter.$(Remote)Used only by Visual Basic to indicatethat the file is a .VBR file. If the Register= key is omitted altogether, the file is not registered.
Version=(Optional)
The Version= key indicates the version of the component that is needed. TheSetup Wizard and PDW can detect that a .dep file is out-of-date using theVersion= key. The version is specified in the format a.b.c.d, as in thefollowing example:

Version=4.50.10.11
Date=(Optional)
The Date= key is the date stamp of the component that is needed. The SetupWizard and PDW can detect that a .DEP file is out-of-date using this key.This value must always be in the format d/m/yyyy or dd/mm/yyyy, even whenusing a localized Setup Wizard or PDW. For example:

Date=10/5/1996 -or-

Date=04/03/1997 NOTE: This key is ignored if a value is specified for the Version= key.
Time=(Optional)
The Time= key is the time stamp of the component that is needed. The SetupWizard and PDW can detect that a .DEP file is out-of-date using this key.This value must always be in the 24-hour format hh:mm:ss, even when using alocalized Setup Wizard or PDW. For example:

Time=14:05:32 NOTE: This key is ignored if a value is specified for the Version= key.
ProgramIconTitle= and ProgramIconCmdLine=(Optional)
The ProgramIconTitle= key indicates that a shortcut (or Program Managericon for Windows NT 3.51) should be created if the file is installed. Thevalue for the ProgramIconTitle= key is the title given to the shortcut (oricon). The value may be quoted or unquoted. For example:

ProgramIconTitle=My Program Title If you indicate that a shortcut should be created, you can give the commandline for the shortcut by specifying a value for the ProgramIconCmdLine=key. The path that you use for the command-line value may contain themacros listed for the Dest= key. For example:

ProgramIconCmdLine=$(AppPath)\Readme.txt
Localized ComponentsSome components may have different dependencies based on the language. Toinstall a component based on the language setting for the client’scomputer, you may specify a section with the component name followed by thelanguage ID.
For example, VBRUN500.DLL has a satellite DLL for all languages (with theexception of English):

[VBRUN500.DLL]Dest=$(WinSysPath)[VBRUN500.DLL <000C>];000C = primary language ID for FrenchUses1=VB5FR.DLL[VBRUN500.DLL <0007>];0007 = primary language ID for GermanUses1=VB5DE.DLL In this example, if VBRUN500.DLL is installed and the client’s languagesettings are French, VB5FR.DLL will also be installed. Likewise, ifVBRUN500.DLL is installed and the client’s language settings are German,VB5DE.DLL will be installed.
Sample Dependency FileThe following is a sample .DEP file for a component named MyOCX.OCX. Thisdependency file would be named MyOCX.DEP:

[MyOCX.OCX]Register=$(DLLSelfRegister)Dest=$(WinSysPath)Date=1/23/1996Time=10:15:33Version=1.1.13.6Uses1=MyDLL.DLLUses2=MyServer.EXEUses3=VBRUN500.DLL[MyServer.EXE]Dest=$(WinPath)Date=1/23/1996Time=18:52:48Version=1.0.1.0Uses1=VBRUN500.DLLRegister=$(ExeSelfRegister)ProgramIconTitle=My ProgramProgramIconCmdLine=$(WinSysPath)\MyOCX.OCX[MyDLL.DLL]Register=$(DLLSelfRegister)Version=1.0.1.0[VBRUN500.DLL]Dest=$(WinSysPath);Additional Files for International Support[VBRUN500.DLL <0007>]Uses1=VB5DE.DLL[VBRUN500.DLL <000C>]Uses1=VB5FR.DLL MyOCX.OCX is a self-registering file that is installed in theWindows\System folder (or Winnt\System32 folder on NT or Windows 2000). MyOCX.OCX depends upon (requires) three files: MyDLL.DLL, MyServer.EXE, and VBRUN500.DLL.
MyServer.EXE is a self-registering file that is installed in the Windowsfolder (or Winnt folder on NT or Windows 2000). MyServer.EXE has only one dependent file, VBRUN500.DLL. If MyServer.EXE is installed during setup, a shortcut is created with the title “My Program” and the command lineWindows\System\MyOCX.OCX (or Winnt\System32\MyOCX.OCX on NT or Windows 2000).
MyDLL.DLL is also a self-registering file. A destination for MyDLL.DLL isnot specified; thus, it will be placed in the destination path for itsparent, MyOCX.OCX.
VBRUN500.DLL will not be registered because a Register= key is notspecified in its section. VBRUN500.DLL will be placed in the Windows\System(or Winnt\System32) folder. If VBRUN500.DLL is installed during setup,VB5DE.DLL will be installed if the language setting is German and VB5FR.DLLwill be installed if the language setting is French. If the languagesetting is set to something other than French or German, no additionalfiles will be installed.
How Setup Wizard and PDW Search for Dependency InformationWhen the Setup Wizard and PDW need to find dependency information for afile (for example, MyOCX.OCX), they follow a specific path. The wizards usethe following methods to locate a section entitled [MyOCX.OCX]; when thesection is found, the search ends.
If the file has been located in a .DEP file, it will use theinformation in the .DEP file in which it was first found.The Setup Wizard will look in the default master dependencyfile for Visual Basic 5.0 (VB5DEP.INI). The PDW will look in thedefault master dependency file for Visual Basic 6.0 (VB6DEP.INI).Information that is placed in the default master dependency fileoverrides that which might be found elsewhere.The Setup Wizard and PDW will look for a dependency file namedMyOCX.DEP. If the dependency file is found, the wizard will searchfor the section entitled [MyOCX.OCX].It will search for a .DEP file associated with the parent ofMyOCX.OCX and possibly continue up the parent chain until asection entitled [MyOCX.OCX] is located.

How To Use the Registry API to Save and Retrieve Setting

Symptoms
Although Visual Basic includes the SaveSetting and GetSetting functionsto save and retrieve information from the registry, these functions onlyoperate on a specific section of the registry, the Visual Basic and VBAProgram Settings of the HKEY_CURRENT_USER root key.
This article outlines the use of 32-bit Windows API functions, which can beused to set and retrieve values from anywhere in the registry. The topicsand function references in this article can be generalized to program the16-bit registry.
The 32-bit API functions also include support for security, although anoverview of security is outside the scope of this article.
NOTE: The SaveSetting and GetSetting functions are not part of the VBAfunction library. However, the sample code below still applies to 32-bitapplications that implement VBA.
Resolution
General Registry InformationThe registry is used by applications and Windows to store configurationdata. It is a replacement for the large numbers of INI files thatproliferated on Windows 3.x machines and is also used heavily by OLE.
The registry is organized using a hierarchical series of keys and valuesresembling a tree. Each key, beginning with one of the six predefined rootkeys, can have sub-keys and values associated with it. The keys areorganizational and naming units and appear in the Windows Registry Editorsas file folders. Values are data entries and appear as text entries in theright pane of the Registry Editor window. Keys need not have any associatedvalues, but may have many. Each value has an associated data type. The twomost commonly used registry data types are REG_SZ, a null-terminatedstring; and REG_DWORD, a 32-bit number.
The basic process used to write or read from a location in the registry isthe same. To reference any given key or value, you must have a handle tothe key. Once this handle is obtained, values and sub-keys of the key thatthis handle refers to can be read, set, or listed (enumerated).
Given a location in the registry, to obtain a handle to that key, you mustbegin with one of the six predefined keys (HKEY_CLASSES_ROOT,HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_CURRENT_CONFIG, andHKEY_DYN_DATA) and traverse the registry tree until the desired key isreached. User programs most often read and write from HKEY_CURRENT_USER andHKEY_LOCAL_MACHINE. If the keys being traversed exist already, you can usea series of calls to the RegOpenKey or RegOpenKeyEx functions. If the keysneed to be created, the RegCreateKey and RegCreateKeyEx functions do the job.
With the handle to the desired key, the functions used to list, set, andretrieve information can be called. In all cases, the functions with the Exsuffix will work only on 32-bit platforms. Functions without the suffix maywork on both 16-bit and 32-bit versions of Windows. Keep in mind that notall registry functions lacking the ‘Ex’ suffix are functions provided for16-bit compatibility. The Ex suffix was only added when the capabilities of16-bit functions were expanded. Functions that are totally new and specificto 32-bit platforms do not possess an Ex extension.
The RegSetValue and RegSetValueEx functions allow the settings of a valueto be modified, while RegQueryValue and RegQueryValueEx retrieve thecurrent setting of a value. The limitations of the non-Ex, 16-bit versionsof these APIs are very evident here. When using the 16-bit RegSetValuefunction there is no way to name a value, and because of this, RegSetValuecan’t be used to associate more than one value with each key. In addition,all values written with RegSetValue have a data type of REG_SZ. Theselimitations are inherent with the 16-bit Registry. RegSetValueEx allows thecreation of a multiple number of values with any available data type.
How to Write to a Specific Registry LocationAfter determining what functions you will need to use for your project,copy the relevant declares from the code at the end of this article to abasic module. The two Visual Basic procedures included (SetValueEx andQueryValueEx) are wrappers for the RegSetValueEx and RegQueryValueEx APIfunctions and greatly simplify their use. The notes below make use of theseVisual Basic functions; however, you are free to make calls directly to theAPI if you wish.
Creating/Modifying Keys and Values:
With the declarations and procedures available, you can create and openkeys, and add, modify, and read values. The three following sectionsexplain how to create a key, set or modify a value, and query a value.
Creating a New Key:
Creating a new key is as simple as using the following procedure.CreateNewKey takes the name of the key to create, and the constantrepresenting the predefined key to create the key under. The call toRegCreateKeyEx doesn’t take advantage of the security mechanisms allowed,but could be modified to do so. A discussion of Registry security isoutside the scope of this article.

Private Sub CreateNewKey (sNewKeyName As String, lPredefinedKey As Long)Dim hNewKey As Long’handle to the new keyDim lRetVal As Long’result of the RegCreateKeyEx functionlRetVal = RegCreateKeyEx(lPredefinedKey, sNewKeyName, 0&, _vbNullString, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, _0&, hNewKey, lRetVal)RegCloseKey (hNewKey)End Sub
With this procedure a call of:

CreateNewKey “TestKey”, HKEY_LOCAL_MACHINE
will create a key called TestKey immediately under HKEY_LOCAL_MACHINE.
Calling CreateNewKey like this:

CreateNewKey “TestKey\SubKey1\SubKey2″, HKEY_LOCAL_MACHINE
will create three-nested keys beginning with TestKey immediately underHKEY_CURRENT_USER, SubKey1 subordinate to TestKey, and SubKey3 underSubKey2.
Setting/Modifying a Value:
Creating and setting a value of a specified key can be accomplished withthe following short procedure. SetKeyValue takes the key that the valuewill be associated with, the name of the value, the setting of the value,and the type of the value (the SetValueEx function only supports REG_SZ andREG_DWORD, but this can be modified if necessary). Specifying a new valuefor an existing sValueName will modify the current setting of that value.

Private Sub SetKeyValue (sKeyName As String, sValueName As String, _vValueSetting As Variant, lValueType As Long)Dim lRetVal As Long’result of the SetValueEx functionDim hKey As Long’handle of open key’open the specified keylRetVal = RegOpenKeyEx(HKEY_CURRENT_USER, sKeyName, 0, _KEY_SET_VALUE, hKey)lRetVal = SetValueEx(hKey, sValueName, lValueType, vValueSetting)RegCloseKey (hKey)End Sub
A call of:

SetKeyValue “TestKey\SubKey1″, “StringValue”, “Hello”, REG_SZ
will create a value of type REG_SZ called “StringValue” with the setting of”Hello.” This value will be associated with the key SubKey1 of “TestKey.”
In this case, “TestKey” is a subkey of HKEY_CURRENT_USER, but this can bemodified by changing the call to RegOpenKeyEx. This call will fail if”TestKey\SubKey1″ does not exist. To avoid this problem, use a call toRegCreateKeyEx instead of a call to RegOpenKeyEx. RegCreateKeyEx will opena specified key if it already exists.
Querying a Value:
The next procedure can be used to ascertain the setting of an existingvalue. QueryValue takes the name of the key and the name of a valueassociated with that key and displays a message box with the correspondingvalue. It uses a call to the QueryValueEx wrapper function defined below,that only supports REG_SZ and REG_DWORD types.

Private Sub QueryValue (sKeyName As String, sValueName As String)Dim lRetVal As Long’result of the API functionsDim hKey As Long’handle of opened keyDim vValue As Variant’setting of queried valuelRetVal = RegOpenKeyEx(HKEY_CURRENT_USER, sKeyName, 0, _KEY_QUERY_VALUE, hKey)lRetVal = QueryValueEx(hKey, sValueName, vValue)MsgBox vValueRegCloseKey (hKey)End Sub
With this procedure, a call of:

QueryValue “TestKey\SubKey1″, “StringValue”
will display a message box with the current setting of the “StringValue”value, and assumes that “StringValue” exists in the “TestKey\SubKey1″ key.
If the Value that you query does not exist then QueryValue will return anerror code of 2 – ‘ERROR_BADKEY’.
Additional Notes:
The above examples use the extended 32-bit versions of the registryfunctions exclusively. These functions allow more than one value to beassociated with each key. As discussed above, the 16-bit RegSetValue andRegQueryValue act on a single value associated with the current key (whichis always of the type REG_SZ). These functions appear in the 32-bitRegistry Editor with a name of <NO NAME>. To set, modify, or query thisspecial associated value, one must use the 16-bit registry functions.Reading and writing from the registry in a 16-bit environment is muchsimpler than in a 32-bit environment. The same basic procedure is followed:open a key and get a handle and then call your modification function withthat handle, but no consideration needs to be made for multiple associatedvalues or for different value data types. A 16-bit application can createand modify keys and values with the declarations of the RegCreateKey,RegOpenKey, RegQueryValue, RegSetValue, and RegCloseKey functions.
In some cases, there is no need for any values to be associated with a key.An application may only need to know if a certain key or value exists, andnot care about the nature of the key’s values. In a situation like this,the RegEnumKey, RegEnumKeyEx, and RegEnumValue functions can be used todetermine whether a certain key or value exists. For more information onthese functions refer to the API Text Viewer and/or Windows API reference.
API Function and Constant Declarations

Option ExplicitPublic Const REG_SZ As Long = 1Public Const REG_DWORD As Long = 4Public Const HKEY_CLASSES_ROOT = &H80000000Public Const HKEY_CURRENT_USER = &H80000001Public Const HKEY_LOCAL_MACHINE = &H80000002Public Const HKEY_USERS = &H80000003Public Const ERROR_NONE = 0Public Const ERROR_BADDB = 1Public Const ERROR_BADKEY = 2Public Const ERROR_CANTOPEN = 3Public Const ERROR_CANTREAD = 4Public Const ERROR_CANTWRITE = 5Public Const ERROR_OUTOFMEMORY = 6Public Const ERROR_ARENA_TRASHED = 7Public Const ERROR_ACCESS_DENIED = 8Public Const ERROR_INVALID_PARAMETERS = 87Public Const ERROR_NO_MORE_ITEMS = 259Public Const KEY_QUERY_VALUE = &H1Public Const KEY_SET_VALUE = &H2Public Const KEY_ALL_ACCESS = &H3FPublic Const REG_OPTION_NON_VOLATILE = 0Declare Function RegCloseKey Lib “advapi32.dll” _(ByVal hKey As Long) As LongDeclare Function RegCreateKeyEx Lib “advapi32.dll” Alias _”RegCreateKeyExA” (ByVal hKey As Long, ByVal lpSubKey As String, _ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions _As Long, ByVal samDesired As Long, ByVal lpSecurityAttributes _As Long, phkResult As Long, lpdwDisposition As Long) As LongDeclare Function RegOpenKeyEx Lib “advapi32.dll” Alias _”RegOpenKeyExA” (ByVal hKey As Long, ByVal lpSubKey As String, _ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As _Long) As LongDeclare Function RegQueryValueExString Lib “advapi32.dll” Alias _”RegQueryValueExA” (ByVal hKey As Long, ByVal lpValueName As _String, ByVal lpReserved As Long, lpType As Long, ByVal lpData _As String, lpcbData As Long) As LongDeclare Function RegQueryValueExLong Lib “advapi32.dll” Alias _”RegQueryValueExA” (ByVal hKey As Long, ByVal lpValueName As _String, ByVal lpReserved As Long, lpType As Long, lpData As _Long, lpcbData As Long) As LongDeclare Function RegQueryValueExNULL Lib “advapi32.dll” Alias _”RegQueryValueExA” (ByVal hKey As Long, ByVal lpValueName As _String, ByVal lpReserved As Long, lpType As Long, ByVal lpData _As Long, lpcbData As Long) As LongDeclare Function RegSetValueExString Lib “advapi32.dll” Alias _”RegSetValueExA” (ByVal hKey As Long, ByVal lpValueName As String, _ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As _String, ByVal cbData As Long) As LongDeclare Function RegSetValueExLong Lib “advapi32.dll” Alias _”RegSetValueExA” (ByVal hKey As Long, ByVal lpValueName As String, _ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, _ByVal cbData As Long) As Long
SetValueEx and QueryValueEx Wrapper Functions:

Public Function SetValueEx(ByVal hKey As Long, sValueName As String, _lType As Long, vValue As Variant) As LongDim lValue As LongDim sValue As StringSelect Case lTypeCase REG_SZsValue = vValue & Chr$(0)SetValueEx = RegSetValueExString(hKey, sValueName, 0&, _lType, sValue, Len(sValue))Case REG_DWORDlValue = vValueSetValueEx = RegSetValueExLong(hKey, sValueName, 0&, _lType, lValue, 4)End SelectEnd FunctionFunction QueryValueEx(ByVal lhKey As Long, ByVal szValueName As _String, vValue As Variant) As LongDim cch As LongDim lrc As LongDim lType As LongDim lValue As LongDim sValue As StringOn Error GoTo QueryValueExError’ Determine the size and type of data to be readlrc = RegQueryValueExNULL(lhKey, szValueName, 0&, lType, 0&, cch)If lrc <> ERROR_NONE Then Error 5Select Case lType’ For stringsCase REG_SZ:sValue = String(cch, 0)lrc = RegQueryValueExString(lhKey, szValueName, 0&, lType, _sValue, cch)If lrc = ERROR_NONE ThenvValue = Left$(sValue, cch-1)ElsevValue = EmptyEnd If’ For DWORDSCase REG_DWORD:lrc = RegQueryValueExLong(lhKey, szValueName, 0&, lType, _lValue, cch)If lrc = ERROR_NONE Then vValue = lValueCase Else’all other data types not supportedlrc = -1End SelectQueryValueExExit:QueryValueEx = lrcExit FunctionQueryValueExError:Resume QueryValueExExitEnd Function

How To Create a Table with Primary Key Through ADOX

Symptoms
ADOX is an extension to ActiveX Data Objects that allows the manipulation of the database schema. This article illustrates how to use ADOX to create a table and add a Primary Key.
Resolution
NOTE: Not all OLE DB providers support the interfaces required to support ADOX methods. With those providers, you have to use Data Definition Queries or another object model to manipulate the database schema.
The first procedure in the example below creates a new table in an existing Microsoft Access database, creates a new field in that table, then creates a primary key index. When adding a single-field primary key, you do not need to use the ADOX Key object.
The second procedure utilizes the ADOX Key object to add a multiple field key to a table.
Steps to Create the Sample Application In Microsoft Visual Basic 5.0 or 6.0, create a new Standard EXE project. Form1 is created by default. On the Project menu, select References to add the following type libraries:
Microsoft ActiveX Data Objects 2.1 Library
Microsoft ADO Ext. 2.1 for DDL and Security Add two Command buttons (Command1 and Command2) and the following code to the Form1:

Option ExplicitPrivate Sub Command1_Click()” This code adds a single-field Primary key’Dim Cn As ADODB.Connection, Cat As ADOX.Catalog, objTable As ADOX.TableSet Cn = New ADODB.ConnectionSet Cat = New ADOX.CatalogSet objTable = New ADOX.Table’Open the connectionCn.Open “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=biblio.mdb”‘Open the CatalogSet Cat.ActiveConnection = Cn’Create the tableobjTable.Name = “Test_Table”‘Create and Append a new field to the “Test_Table” Columns CollectionobjTable.Columns.Append “PrimaryKey_Field”, adInteger’Create and Append a new key. Note that we are merely passing’the “PimaryKey_Field” column as the source of the primary key. This’new Key will be Appended to the Keys Collection of “Test_Table”objTable.Keys.Append “PrimaryKey”, adKeyPrimary, “PrimaryKey_Field”‘Append the newly created table to the Tables CollectionCat.Tables.Append objTable’ clean up objectsSet objKey = NothingSet objTable = NothingSet Cat = NothingCn.CloseSet Cn = NothingEnd SubPrivate Sub Command2_Click()” This code adds a multi-field Primary Key’Dim Cn As ADODB.Connection, Cat As ADOX.CatalogDim objTable As ADOX.Table, objKey As ADOX.KeySet Cn = New ADODB.ConnectionSet Cat = New ADOX.CatalogSet objTable = New ADOX.TableSet objKey = New ADOX.KeyCn.Open “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=biblio.mdb”Set Cat.ActiveConnection = CnobjTable.Name = “Test_Table2″objTable.Columns.Append “PrimaryKey_Field1″, adIntegerobjTable.Columns.Append “PrimaryKey_Field2″, adIntegerobjKey.Name = “PrimaryKey”objKey.Type = adKeyPrimaryobjKey.Columns.Append “PrimaryKey_Field1″objKey.Columns.Append “PrimaryKey_Field2″objTable.Keys.Append objKeyCat.Tables.Append objTable’ clean up objectsSet objKey = NothingSet objTable = NothingSet Cat = NothingCn.CloseSet Cn = NothingEnd Sub NOTE: You might have to adjust the connect string to point to a valid Jet database. Run the application and click the Command buttons. You can check the table definitions for Test_Table and TestTable2 in Microsoft Access 97, Microsoft Access 2000, or the Visual Basic Visual Data Manager add-in.