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 ‘table’

BUG: Design Option in Pop-up Menu Data View Window Not Available

Symptoms
If you use a Windows NT 4.0 computer and are not a member of the administrators group, you will not get the Design option in the pop-up menu for a table when you right-click on on the table in the Data View Window of Microsoft Visual Basic 6.0.
NOTE: This bug does not occur on Windows 2000 machines.
Resolution
The Visual Basic IDE is incorrectly determining that the Design option should not be shown unless you belong to the administrators group.

PRB: Error When You Create SQL Server TEMP Tables Using Remote Data Objects (RDO)

Symptoms
When you create a SQL Server local temporary table using the rdoConnection object by calling its .Execute method with default parameters, and then attempt to access the table after the Remote Data Objects (RDO) method has run, you may receive one of the following error messages:

Run-time error ‘40002′: 37000:[Microsoft][ODBC SQL Server Driver][SQL Server] Statement(s)could not be prepared
-or-

Run-time error ‘40002′: S0002:[Microsoft][ODBC SQL Server Driver][SQL Server] Invalid Object Name ‘#<Name of the temporary table>’
Resolution
The creation and use of temporary database tables to facilitate the storage and manipulation of volatile intermediate data is a common programming practice. The default behavior of the SQL Server Open Database Connectivity (ODBC) driver is to create and use temporary stored procedures to run prepared statements. The .Execute method of the rdoConnection object uses the SQLPrepare() and SQLExecute() ODBC application programming interface (API) calls by default to run a SQL statement as a prepared statement. Temporary tables that are created by a stored procedure are automatically dropped when the procedure completes execution. As a result, when you attempt to access a SQL Server temporary table that was created by calling the .Execute method of an rdoConnection object with default parameters, in subsequent statements you receive one of the error messages specified in the “Symptoms” section.

PRB: Dynaset, Snapshot, and Table Objects No Longer Available

Symptoms
If you try to dimension a Dynaset, Snapshot, or Table object variable andusing DAO 3.0 (Visual Basic version 4.0 data access objects), you willreceive this runtime error:

User-defined type not defined.If you replace the Dynaset, Snapshot, or Table object with the Object orRecordSet type and use methods such as CreateDynaset, CreateSnapshot, orOpenTable that are tied to the Dynaset, Snapshot, or Table types, you willreceive this error:

Feature not available.
Resolution
Visual Basic version 4.0 has replaced the older Dynaset, Snapshot, andTable objects with a single RecordSet object. This object can take on theattributes of any of the older three making it much more powerful andflexible.

PRB: Cannot Update VFP Free Table Using FoxPro ODBC ISAM Drive

Symptoms
In systems that do not have MDAC 2.1 installed on them, when you use the FoxPro ODBC ISAM Driver (named “Microsoft FoxProDriver(*.dbf)” in the ODBC Administrator) for 2.5 or 2.6 tables, it allowsyou to view the contents of a table created in Visual FoxPro. However, whenyou try to update or add new records, the following error message mightappear:

Operation must use an Updateable Query.
Resolution
The older ISAM driver is not able to accurately read the table header ofthe Visual FoxPro table. Visual FoxPro supplies additional information inthe header of the table for links to a database container, which the olderISAM ODBC driver does not accommodate. Snapshot recordsets are updateableas long as the table is in 2.5 or 2.6 format.

How To Open a Password-Protected Paradox Table Using DAO

Symptoms
The Data Access Object (DAO) documentation that comes with Microsoft Access and Visual Basic Online help shows how to open a Paradox table. However, it does not show how to open a table that is password protected.
Resolution
To open a Paradox table, the documentation states that you must specify Paradox 3.x, Paradox 4.x, or Paradox 5.x in the connect string. However, the documentation omits the PWD argument in the connect string. PWD is used to specify the Paradox password, just as it is used when opening password-protected ODBC or Jet tables.
The following code samples show the correct use of the PWD option when opening password-protected Paradox tables. In each of the samples, the table is C:\MYDATA\Table1.DB and the password is “ABC”.
NOTE: The period in the Paradox file name needs to be replaced by the # symbol. This is because Microsoft Jet uses the period to separate database and table names and to separate table and field names. For example, Table1.DB needs to be written as Table1#DB when referring to the table in Jet.

Attaching/Linking a password-protected table:

Dim db As DAO.DatabaseDim td As DAO.TableDefDim rs As DAO.RecordsetSet db = OpenDatabase(App.Path & “\NWIND.MDB”)Set td = db.CreateTableDef(“Table1″)td.Connect = “Paradox 4.x;PWD=ABC;DATABASE=C:\MYDATA;”td.SourceTableName = “Table1#DB”db.TableDefs.Append tdSet rs = db.OpenRecordset(“Table1″)

Opening directly using a Database object:In this case, the database is the directory where the tables reside. If all tables have the same password, you can open them from the same Database object. If the tables have different passwords, you will need to open a different Database object for each unique password.

Set db = DBEngine(0).OpenDatabase(“C:\MYDATA”, 0, 0, “Paradox 4.x;PWD=ABC;”)Set rs = db.OpenRecordset(“Table1#DB”)

Referencing a Paradox table directly in SQL statementsMicrosoft Jet allows use of an IN clause or the [Database].[Table] syntax to reference a table outside the current database. The [Database].[Table] syntax is preferred.

Set db = OpenDatabase(App.Path & “\NWIND.MDB”)Set rs = db.OpenRecordset(“SELECT * FROM [Paradox 3.x;DATABASE=C:\MYDATA;PWD=ABC].[Table1#DB]“)” Or’Set rs = db.OpenRecordset(“SELECT * FROM [Table1#DB] IN ” [Paradox 3.x;DATABASE=C:\MYDATA;PWD=ABC]“)” Or’Set rs = db.OpenRecordset(“SELECT * FROM [Table1#DB] IN ‘C:\MYDATA’ [Paradox 3.x;PWD=ABC]“)
You can also use action queries with the same direct SQL technique:

‘ Make a new table Access “LocalTable” from data in Paradox table.Set db = OpenDatabase(App.Path & “\NWIND.MDB”)db.Execute “SELECT * INTO LocalTable FROM [Paradox 3.x;DATABASE=C:\MYDATA;PWD=ABC].[Table1#DB]“‘ Append data into an Access table “LocalTable” from Paradox table.db.Execute “INSERT INTO LocalTable SELECT * FROM [Paradox 3.x;DATABASE=C:\MYDATA;PWD=ABC].[Table1#DB]“‘ Append data into a Paradox table from an Access table “LocalTable”. db.Execute “INSERT INTO [Paradox 3.x;DATABASE=C:\MYDATA;PWD=ABC].[Table1#DB] SELECT * FROM LocalTable”‘ Delete records from a Paradox table.db.Execute “DELETE FROM [Paradox 3.x;DATABASE=C:\MYDATA;PWD=ABC].[Table1#DB] WHERE Field1 = 55″
(c) Microsoft Corporation 1999, All Rights Reserved. Contributions by Malcolm Stewart, Microsoft Corporation.

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.