Solved

Opening a database file and running a query

Posted on 2001-07-27
10
175 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

760 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

21 Experts available now in Live!

Get 1:1 Help Now