Solved

Opening a database file and running a query

Posted on 2001-07-27
10
207 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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
 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Modifying Conditional Format from VBA code 3 86
pop out of webbrowser1 control vba6 5 52
Set WorkSheet  not Working 9 62
VB6 Enviroment 3 38
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

710 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