Solved

Opening a database file and running a query

Posted on 2001-07-27
10
186 Views
Last Modified: 2010-05-02
I have a database file with several tables.  I need to open the database from my vb 6 program and run a simple query using two tables from the database and store the result in vb.

DataBase = "H:\Access\NT\NT_Cat_dbs.mdb"
Table 1 = "Test Information"
Fields needed to get:  TestNo & Phase

Table 2 = "Test Animals"
Fields needed to get:  ProductID

I need to query the database using the fields:
AnimalId, NT

If datatypes need declared please include code.

Ask for More info if needed or not clear.....

thanks
0
Comment
Question by:ataripirate
10 Comments
 

Expert Comment

by:sorinuc
ID: 6327284
What's the relation between Table 1 and Table 2?
0
 

Author Comment

by:ataripirate
ID: 6327286
The two tables are linked with common Test Numbers.

TestNo




thanks
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6327289
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Set cnn = New ADODB.Connection
Set rst = New ADODB.Recordset
cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=H:\Access\NT\NT_Cat_dbs.mdb;"
cnn.Open
rst.Open "Select TestNo,Phase From [Test Information]",cnn,adOpenStatic,adLockReadOnly
With rst
  .MoveFirst
  Do
    'Do Something with each record e.g.,
    MsgBox .Fields("TestNo").Value & " : " & .Fields("Phase").Value
    .MoveNext
  Loop Until .Eof
End With
rst.Close
rst.Open "Select ProductID From [Test Animals]",cnn,adOpenStatic,adLockReadOnly
With rst
  .MoveFirst
  Do
    'Do Something with each record e.g.,
    MsgBox .Fields("ProductID").Value
    .MoveNext
  Loop Until .Eof
End With
rst.Close
Set rst = Nothing
cnn.Close
Set cnn = Nothing

This code requires a reference to Microsoft ActiveX Data Objects 2.x Library added to the project.

You can extend the sql query by using a Where clause which specifies the values of AnimalID and NT to restrict the number of records returned.

Hope this helps.
0
 

Author Comment

by:ataripirate
ID: 6327294
I will check it out, and respond asap.

thanks
0
 
LVL 5

Expert Comment

by:rkot2000
ID: 6327306
You need to use a join to get all related data.

lvsql =Select  TestNo ,  Phase, ProductID From Table1, Table2 Where Table 1. Test Numbers = Table2. Test Numbers?

so you can use tim's example and this sql
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 33

Accepted Solution

by:
hongjun earned 100 total points
ID: 6327309
Try this. I am assuming you have included "Microsoft ActiveX Data Objects 2.1 or 2.5 library" in Project->References.

Dim objconn as adodb.connection
dim objrs as adodb.recordset
dim strsql as string

set objconn = new adodb.connection
objconn.open "Driver={Microsoft Access Driver (*.mdb)};dbq=h:\Access\NT\NT_Cat_dbs.mdb;uid=sa;pwd=;"

strsql = "select t1.TestNo, t1.Phase, t2.ProductID " & _
         "from [Test Information] t1, [Test Animals] t2 " & _
         "where t1.AnimalId = t2.AnimalId"

set objrs = objconn.execute(strsql)


One thing to ask about your where clause and the relationship of the 2 tables. I am using AnimalId to do the joining and am assuming this field is common to both tables. I could be wrong so tell us your where condition and the field in common.

Records retrieved will be stored in objrs object.

hongjun
0
 

Author Comment

by:ataripirate
ID: 6327330
Common Record is TestNo

thanks
0
 

Author Comment

by:ataripirate
ID: 6327333
Common Record is TestNo

thanks
0
 

Author Comment

by:ataripirate
ID: 6327399
Hongjun,

I ran your code and go the following error:

Runtime error:
[Microsoft] [ODBC Microsoft Access 97 Driver] too Few Parameters, Expected 4

?

Thanks
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6329221
Dim objconn as adodb.connection
dim objrs as adodb.recordset
dim strsql as string

set objconn = new adodb.connection
objconn.open "Driver={Microsoft Access Driver (*.mdb)};dbq=h:\Access\NT\NT_Cat_dbs.mdb;uid=sa;pwd=;"

strsql = "select t1.TestNo, t1.Phase, t2.ProductID " & _
        "from [Test Information] t1, [Test Animals] t2 " & _
        "where t1.TestNo = t2.TestNo"

set objrs = objconn.execute(strsql)



hongjun
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
z = x + y – 1 6 67
Use closed file on desktop in vba 6 65
Windows 10 start screen issues 9 51
SQL VB connection works in one PC and doesn't in another 15 55
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now