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 ‘data access object’

PRB: RepairDatabase Method Is No Longer Available in DAO 3.6

Symptoms
If you issue a DbEngine.RepairDatabase method after you change your project references from Microsoft DAO 3.51 Object Library to Microsoft DAO 3.6 Object Library, you may receive the following error message:

Error # 3251 was generated by DAO.DbEngine.
Operation is not supported for this type of object.Or, you may notice that the method is not available through IntelliSense when you issue a DbEngine.RepairDatabase method.
Resolution
In Data Access Object (DAO) 3.6, the RepairDatabase method is no longer available or supported. This is by design to match Microsoft Jet 4.0.

PRB: CompactDatabase Method Requires Locale to Convert 2.0 MDB

Symptoms
When attempting to use the Data Access Object (DAO) 3.5 CompactDatabasemethod to convert a Jet 2.0 .mdb file to a Jet 3.0 file format, theresulting .mdb file is still in a Jet 2.0 file format.
Resolution
DAO 3.5 requires you to provide explicitly the locale argument of theCompactDatabase when converting .mdb file formats.

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 Bypass Login Prompt When Opening Linked Table

Symptoms
In Access, when you first link (attach) an external table using an ODBCdriver, you have the option to store the User ID and password for the tablelocally. If you do not store the ID and password locally, you will beprompted later for such information when you open the table.
This article demonstrates how to bypass the Login prompt when you open anAccess linked table by pre-connecting to the database and providing User IDand password programmatically in Basic.
Resolution
The Microsoft Jet database engine caches authentication information foreach DSN. This prevents users from being prompted to login to remotedatabases each time a table is opened. You can take advantage of thisbehavior by pre-connecting to the database directly and programmaticallyproviding user ID and password to prevent the login prompt from appearingwhen opening linked tables that don’t have the user ID and password cached.In Access, create a new database, db1.mdb, and a linked table,dbo_authors, from SQL Server Pubs database.In Visual Basic, start a new project and choose “Standard EXE.” Form1is created by default.In Access, create a new database and create a new form (Form1).In Visual Basic 4.0 and later, add a Reference to:

Microsoft Data Access Object 2.x(VB4 16-bit)Microsoft Data Access Object 3.x(VB4 32-bit; VB5) Paste the following code in the General Declarations section of Form1:
Note You must change Username=<username> and PWD=<strong password> to the correct values before you run this code. Make sure that Username has the appropriate permissions to perform this operation on the database.

Sub Command1_Click()Dim db1 As DatabaseDim db2 As DatabaseDim rs As RecordsetDim strConnect As String’*** You have to modify the path to where db1.mdb is locatedSet db1 = OpenDatabase(“C:\MyTest\db1.mdb”)strConnect = UCase(db1.TableDefs(“dbo_authors”).Connect) & _”;Username=<username>;PWD=<strong password>”Set db2 = OpenDatabase(“”, False, False, strConnect)db2.CloseSet db2 = NothingSet rs = db1.OpenRecordset(“dbo_authors”)Debug.Print rs(0)Debug.Print “Recordset Opened Successfully”rs.Closedb1.CloseSet rs = NothingSet db1 = NothingEnd Sub NOTES:
You must provide correct login information, User ID and Password, instrConnect to establish the connection.If you know which DSN the table is linked to, you can hard-code thevalue of strConnect.The Microsoft Jet database engine will first try to log you in with thesame user ID and password that you log into the Jet database with(default is Admin/no password). If you make the local login match theserver login, you will not get any login prompts.Microsoft SQL Server can integrate its security mechanism with MicrosoftNT domain accounts. If the user has a valid account in the domain, youwill not get any login prompts.

FIX: Unrecognized Database Format Error with Data Control or Data Form Wizard

Symptoms
When you attempt to connect to a Microsoft Access 2000 database by using either the standard Data control or the Data Form Wizard, you see the following error message where ‘…\databasename.mdb’ is the absolute filename of the Access 2000 database:

Unrecognized database format ‘…\databasename.mdb’”
Resolution
The standard Data control uses Data Access Object (DAO) 3.51 to connect to Access databases by using the Jet 3.51 engine.
The Data Form Wizard uses the Jet 3.51 OLE DB Provider to connect to Access databases by using the Jet 3.51 engine.
The Jet 3.51 engine can connect to Access database versions prior to Access 2000, but cannot connect to Access 2000 databases.