Dynamic queries on Access databases
I have been programming computers since 1962 in just about every computer language that exist, but I have spent the past 12 hours trying to accomplish in VB 2005 what I accomplished in VB 6 with 3 statements:
Set DB1 = DBEngine.OpenDatabase("C:\DataFolder\DataDB.mdb")
Sql = "SELECT * FROM TABLE1 WHERE INDEX = 1432;"
Set rs1 = DB1.OpenRecordset(Sql)
I am no closer now that I was at 6 AM this morning. I really need some help!
I can access the database and read/write data to it but cannot find the way to create and excecute the Sql statement so I access
the correct record.
Thanks
Dick Roose
Hi,
Your 3 line statements there is equivalent to:
Imports System.Data.OleDbDim constr as String = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=<mdb path>"
Dim conn as new OleDbConnection(constr)
Dim dt as new DataTable()
Dim cmd as new OleDbCommand("<your sql statement>", conn)
conn.Open()
Dim dr as OleDbDataReader = cmd.ExecuteReader();
dt.Load(dr);
your datatable now contains your queried data...
BTW, don't forget to dispose and close the objects after using. To avoid resource leaks...
cheers,
Paul June A. Domag
I tried the code suggested by Paul and it compiled very well, I thought I had my answer. Unfortunately when I run the code I get the error message:
Could not find installable ISAM.
I then spent a couple of hours trying to resolve this issue and I am about to give up. It seems that Microsoft.Jet.Oledb.4.0 requires the Msrd4x40.dll or an equivalent file which I cannot find anywhere. The Msrd3x40.dll file works for jet 3 but doesn't work for jet 4.Any suggestions on what I do now?
Thanks
Dick Roose
Hi,
Ok, here is a trick I learned on how to obtain a connection string for your database connection:
1. create a .txt file in windows explorer.
2. rename it to .
udl.
3. Open it, it should display the
Data Link Properties Dialog
4. Select the
Microsoft Jet x.x OLE DB Provider in the list
5. Click next and specify the .
mdb location.
6. Close and save the changes.
7. Open the .udl file with notepad.
8. Copy the generated connection string "except the line that starts with
;
9. past this as a connection string in your code...
cheers,
Paul June A. Domag