Results 1 to 13 of 13

Thread: SQL Lookup Syntax Question

  1. #1

    Thread Starter
    Addicted Member DKasler's Avatar
    Join Date
    Jan 2005
    Location
    Brooklyn, NYC
    Posts
    177

    SQL Lookup Syntax Question

    Im trying to get data from 2 tables to be part of this lookup.

    When you choose an insurance from a dbcombo box I want it to run an SQL statement that looks at table1 and finds ALL of the matches in that table then takes the DrID field in the same table and matches it with the DrID field in a 2nd table.

    But Im not sure how to do that...

    Insurance Combo -> Matching insurances in table1 -> DrID field in table1 == DrID field in table 2 -> Display DrName, DrAddress (from table2) in MSHFlexgrid

    I can display the data fine... im just not sure how create that type of SQL lookup.

    USUALLY When I do something to fill a mshflexgrid I do it like this... So something that would work with the below code would be great! Thanks.

    VB Code:
    1. Dim sConnect As String
    2.     Dim sSQL As String
    3.     Dim dfwConn As ADODB.Connection
    4.     Dim datPrimaryRs As New ADODB.Recordset
    5.        
    6.     'set strings
    7.     sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & frmlocation.Text1.Text & "';Persist Security Info=False"
    8.     sSQL = "select InsRecID, SpecID, Insurance, InsuranceID from DrsInsuranceList where SpecID like '" & Text7.Text & "'"
    9.    
    10.     ' open connection
    11.     Set dfwConn = New ADODB.Connection
    12.     dfwConn.Open sConnect
    13.    
    14.     'create a recordset using the provided collection
    15.     datPrimaryRs.CursorLocation = adUseClient
    16.     datPrimaryRs.Open sSQL, dfwConn, adOpenForwardOnly, adLockReadOnly
    17.    
    18.     Set MSHFlexGrid1.DataSource = datPrimaryRs
    -----MY SITES-----
    BayRidgeNights.Com - NYC Nightlife Forums

    Fight Communism - Rate Posts!

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: SQL Lookup Syntax Question

    Alias your table names. I don't know what your table or field names are, so I just made stuff up, but try something along the lines of
    VB Code:
    1. sSQL = "select drsins.InsRecID, drsins.SpecID, drsins.Insurance, drsins.InsuranceID, "
    2. sSQL = sSQL & "doctable.drid, doctable.drname, doctable.address from DrsInsuranceList drsins, "
    3. sSQL = sSQL & "DoctorsTable doctable where drsins.SpecID like '" & Text7.Text & "'"
    4. sSQL = sSQL & "and drsins.drid = doctable.drid"
    Bear in mind this is off the top of my head, but you should get the general idea.

  3. #3
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,495

    Re: SQL Lookup Syntax Question

    If you are going to use a LIKE for the seach you might as well include the wild card value in the creteria along with text7.text
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  4. #4

    Thread Starter
    Addicted Member DKasler's Avatar
    Join Date
    Jan 2005
    Location
    Brooklyn, NYC
    Posts
    177

    Re: SQL Lookup Syntax Question

    Thanks, but I think im even more confused now than I was before...

    Lets try this with the actual table & field names.

    DrsInsuranceList - TABLE
    -SpecID
    -Insurance

    When someone choose an insurance from dbcombo2 I want it to look for all matches in the "Insurance" field. Then Match the field SpecID with the SpecID field from the "Specialists" table

    Specialists - Table
    -SpecID
    -SpecialistType
    -SpecialistName

    Then once we have matched the "SpecID" fields from the 2 tables I want to display the "SpecialistType" and "SpecialistName" fields in MSHFlexgrid1

    PS. Thanks SO much!
    -----MY SITES-----
    BayRidgeNights.Com - NYC Nightlife Forums

    Fight Communism - Rate Posts!

  5. #5
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,495

    Re: SQL Lookup Syntax Question

    strsql = "Select SpecialistType,SpecialistName From Specialists Where "
    strsql = strsql & "SpecID = (Select SpecID From DrsInsuranceList Where "
    strsql = strsql & "Insurance = '" & dbcombo2.text & "'"
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  6. #6

    Thread Starter
    Addicted Member DKasler's Avatar
    Join Date
    Jan 2005
    Location
    Brooklyn, NYC
    Posts
    177

    Re: SQL Lookup Syntax Question

    Quote Originally Posted by GaryMazzone
    strsql = "Select SpecialistType,SpecialistName From Specialists Where "
    strsql = strsql & "SpecID = (Select SpecID From DrsInsuranceList Where "
    strsql = strsql & "Insurance = '" & dbcombo2.text & "'"
    Huge step in the right direction... It works where only ONE dr has the insurance chosen from the list... but when more than 1 has it I get this error

    Run-time error '-2147467259(80004005)
    At most one record can be returned by this subquery.
    -----MY SITES-----
    BayRidgeNights.Com - NYC Nightlife Forums

    Fight Communism - Rate Posts!

  7. #7
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,495

    Re: SQL Lookup Syntax Question

    strsql = "Select SpecialistType,SpecialistName From Specialists Where "
    strsql = strsql & "SpecID In (Select SpecID From DrsInsuranceList Where "
    strsql = strsql & "Insurance = '" & dbcombo2.text & "'"
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  8. #8

    Thread Starter
    Addicted Member DKasler's Avatar
    Join Date
    Jan 2005
    Location
    Brooklyn, NYC
    Posts
    177

    Re: SQL Lookup Syntax Question

    Quote Originally Posted by GaryMazzone
    strsql = "Select SpecialistType,SpecialistName From Specialists Where "
    strsql = strsql & "SpecID In (Select SpecID From DrsInsuranceList Where "
    strsql = strsql & "Insurance = '" & dbcombo2.text & "'"
    I changed the "=" to "In" and now I get an error saying "type mismatch in expression"
    -----MY SITES-----
    BayRidgeNights.Com - NYC Nightlife Forums

    Fight Communism - Rate Posts!

  9. #9
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,495

    Re: SQL Lookup Syntax Question

    Opps add ad close peran at the end of the sql.

    strsql = "Select SpecialistType,SpecialistName From Specialists Where "
    strsql = strsql & "SpecID In (Select SpecID From DrsInsuranceList Where "
    strsql = strsql & "Insurance = '" & dbcombo2.text & "')"

    That might help
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  10. #10

    Thread Starter
    Addicted Member DKasler's Avatar
    Join Date
    Jan 2005
    Location
    Brooklyn, NYC
    Posts
    177

    Re: SQL Lookup Syntax Question

    Quote Originally Posted by GaryMazzone
    Opps add ad close peran at the end of the sql.

    strsql = "Select SpecialistType,SpecialistName From Specialists Where "
    strsql = strsql & "SpecID In (Select SpecID From DrsInsuranceList Where "
    strsql = strsql & "Insurance = '" & dbcombo2.text & "')"

    That might help
    Yea, I added that already. It worked ok with they way you first posted it, but only for a single match. It seems to be when more than one match is found it throws a fit.

    I think the "Type Mismatch in expression" error is being cause by the "In" on the 2nd line.
    -----MY SITES-----
    BayRidgeNights.Com - NYC Nightlife Forums

    Fight Communism - Rate Posts!

  11. #11
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,495

    Re: SQL Lookup Syntax Question

    Can you post the SQL that is actually being built and sent to the database? Use a debug pring statement and it should go the development immediate window then you could post it here.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  12. #12
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,495

    Re: SQL Lookup Syntax Question

    What I think is happening is that the text is all just strung together in one type name. It will need to be seperated out and posible another in statement for the second (sub) query.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  13. #13

    Thread Starter
    Addicted Member DKasler's Avatar
    Join Date
    Jan 2005
    Location
    Brooklyn, NYC
    Posts
    177

    Re: SQL Lookup Syntax Question

    Here is my full code:

    VB Code:
    1. Dim sConnect As String
    2.     Dim sSQL As String
    3.     Dim dfwConn As ADODB.Connection
    4.     Dim datPrimaryRs As New ADODB.Recordset
    5.        
    6.     'set strings
    7.     sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & frmlocation.Text1.Text & "';Persist Security Info=False"
    8.     sSQL = "Select SpecialistType,SpecialistName From Specialists Where "
    9.     sSQL = sSQL & "SpecID In (Select SpecID From DrsInsuranceList Where "
    10.     sSQL = sSQL & "Insurance = '" & DBCombo2.Text & "')"
    11.    
    12.     ' open connection
    13.     Set dfwConn = New ADODB.Connection
    14.     dfwConn.Open sConnect
    15.    
    16.     'create a recordset using the provided collection
    17.     datPrimaryRs.CursorLocation = adUseClient
    18.     datPrimaryRs.Open sSQL, dfwConn, adOpenForwardOnly, adLockReadOnly
    19.    
    20.     Set MSHFlexGrid1.DataSource = datPrimaryRs
    -----MY SITES-----
    BayRidgeNights.Com - NYC Nightlife Forums

    Fight Communism - Rate Posts!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width