Query using SQL doesn't work 
Author Message
 Query using SQL doesn't work

This example is a simplified version of the actual case I'm working on.  
I have three tables. None of my queries work on these tables, which means
that I am missing something.

Table1 - has unique number and other personal information
         like name and address, etc.
CaseFile
  CaseNumber(unique)
       .
       .
       .
Table2 - Doctor information
DoctorTable
  DoctorID
     .
     .
     .
Table3 - this table allows me to store an unlimited
         number doctors for a given patient.
ContactTable
  CaseNumberID (From Table 1 - CaseNumber )
  ContactID  (From Table 2 - DoctorID )

The following is excerpts of the code I use:

    ' Set the data control's DatabaseName and RecordSource properties
    datDoctorInfo.DatabaseName = "CaseFile.MDB"
    datDoctorInfo.RecordSource = "DoctorTable"

    ' Set the data control's DatabaseName and RecordSource properties
    datContactTableInfo.DatabaseName = "Contact.MDB"
    datContactTableInfo.RecordSource = "ContactTable"

At this point I already know which case number I am working on.

This is the SQL statement I use to list all of the Doctors associated
with this particular patient. ( The SQL statement show here is broken
up for easier viewing.  It is one complete line as required by Visual
Basic)

Case_Number = #

datDoctorInfo.RecordSource = "Select * from [Doctor] WHERE

   " & datContactTableInfo.Recordset!CaseNumberID & " =
   " & Case_Number & "   AND  

   " & datDoctorInfo.Recordset!DoctorID & " =
   " & datContactTableInfo.Recordset!ContactID & "

     ORDER BY [LastName]"

Suppose we have CaseNumber = 1 and he has 3 Doctors (1, 2, 3)
                CaseNumber = 2 and he has 3 Doctors (4, 5, 6)

My results are:  When Case_Number = 1 I get all the entries in Doctor
Table.  When Case_Number = 2 I don't get any.  I can't figure out why.  
Any ideas would be greatly appreciated.  Thanks in advance!



Sun, 17 May 1998 03:00:00 GMT  
 Query using SQL doesn't work

Quote:

>This is the SQL statement I use to list all of the Doctors associated
>with this particular patient. ( The SQL statement show here is broken
>up for easier viewing.  It is one complete line as required by Visual
>Basic)

>Case_Number = #

>datDoctorInfo.RecordSource = "Select * from [Doctor] WHERE

>   " & datContactTableInfo.Recordset!CaseNumberID & " =
>   " & Case_Number & "   AND  

>   " & datDoctorInfo.Recordset!DoctorID & " =
>   " & datContactTableInfo.Recordset!ContactID & "

>     ORDER BY [LastName]"

>Suppose we have CaseNumber = 1 and he has 3 Doctors (1, 2, 3)
>                CaseNumber = 2 and he has 3 Doctors (4, 5, 6)

>My results are:  When Case_Number = 1 I get all the entries in Doctor
>Table.  When Case_Number = 2 I don't get any.  I can't figure out why.  
>Any ideas would be greatly appreciated.  Thanks in advance!

Mark

    I think you are close, but not quite.  If I read your statement
correct, it will evaluate the *value* of
datDoctorInfo.Recordset!DoctorID, (something like 52 or 100 or some other
ID number) and not look at the *field name*.  What you want to do is more
like this:

datDoctorInfo.RecordSource = "Select * from [Doctor] WHERE "

    & " CaseNumberID = "                <=== change this line
    & Case_Number &
"   AND  DoctorID =" &                  <=== change this line
   & datContactTableInfo.Recordset!ContactID &
"   ORDER BY [LastName]"

So that your final SQL reads something like

"Select * from [Doctor] Where CaseNumberID = 53 and DoctorID = 1"

   This says to look at the values of the fields named CaseNumberID and
DoctorID, and compare them to the values you want.  Your example (I
believe) said compare the *value* of datDoctorInfo...DoctorID, and
compare it to the *value* that you want.  The resulting SQL of your
original (if I am interpreting it correct) would have read:

"Select * from [Doctor] where 53 = 53 and 1 = 1"

  Which makes no sense.  Hope this helps.  When in doubt, do the
following:

- go into debug mode, and stop at the statement where you assign the SQL
value.
- copy the SQL - go to the debug window and do a "?
datDoctorInfo.Recordsource"
- Open up MS Access against your database, and paste the SQL that you
copied into a new query.  This will show what your SQL statement looks
like and what it will return,

--
Jim Gilbert                    Sub Disclaimer()

Monsanto Co.                      myOpinionsReflectMyEmployers = False
St. Louis, MO                  End Sub



Mon, 18 May 1998 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. SQL query works, Crystal query doesn't

2. Why doesn't this query work?

3. select query doesn't always work

4. Query criteria >50 doesn't work if passed from form..;(

5. STMAdmin.dll to query NT Log doesn't seem to work properly

6. Cancel doesn't work on async queries

7. adCommand.Cancel doesn't seem to work with SELECT INTO query

8. Query doesn't work when it comes from textbox

9. Cancel doesn't work on async queries

10. fRefreshLinks Doesn't work if path doesn't exist

11. ADO doesn't receive result of SQL query from a stored procedure

12. SQL in VB doesn't work - why?

 

 
Powered by phpBB® Forum Software