Results 1 to 23 of 23

Thread: help needed in SQL Statement

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2002
    Posts
    31

    help needed in SQL Statement

    Hello.
    I need to develop one application.
    The code is as follows
    Private Sub cboCity_Click(Area As Integer)
    Dim strCatalog As String: strCatalog = "e:\data\stat\dao"
    Dim strTable As String: strTable = "db_field.dbf"
    Dim strTable_1 As String: strTable_1 = "value_f.dbf"
    Dim sSQL1 As String
    Dim strConnect As String
    strConnect = "Provider=MSDASQL.1;" & "Persist Security Info=False;" & "Data Source=dBASE Files;" & "Initial Catalog=" & strCatalog
    Set m_cn_1 = New ADODB.Connection
    With m_cn_1
    .ConnectionString = strConnect
    .Open
    End With
    Dim strSQL1 As String
    strSQL1 = "SELECT value_f.V_NAME "
    strSQL1 = strSQL1 & "FROM value_f, db_field WHERE db_field.ID=value_f.VF_CODE and db_field.FIELD_N ='" & cboCity.BoundText & "'"
    Set m_rs_1 = New ADODB.Recordset
    With m_rs_1
    .ActiveConnection = m_cn_1
    .CursorLocation = adUseClient
    .CursorType = adOpenForwardOnly
    .LockType = adLockReadOnly
    .Open strSQL1
    End With
    Set cboCity1.DataSource = m_rs_1
    Set cboCity1.RowSource = m_rs_1
    Set m_rs_1 = Nothing
    End Sub
    This application should get the boundtext from db_field.dbf and throw corresponding field of db_field.ID i.e. value_f.V_NAME into cboCity1
    I guess that something is wrong with my sql statement.
    Any help will be appreciated
    Hayk

  2. #2
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951
    See if this helps:

    strSQL1 = "SELECT value_f.V_NAME "
    strSQL1 = strSQL1 & "FROM value_f, db_field WHERE db_field.ID= '" & value_f.VF_CODE & '"
    strSQL1 = strSQL1 & " and db_field.FIELD_N ='" & cboCity.BoundText & "'"

  3. #3
    Hyperactive Member
    Join Date
    Oct 2000
    Posts
    303

    make sure you trim

    your text from the combo box. Also if you want to fool prove,
    you might even have to trim the one from the database so
    that you are comparing "string" = "string" and not
    "string" = "string ". Also you might want to
    conside converting to specific cases, ie. upper just for
    comparing. Of course, this only applies to char fields.

  4. #4
    Fanatic Member SkiNLaB's Avatar
    Join Date
    Jan 2002
    Location
    Sydney, Australia
    Posts
    747
    put [vb*code]some code[/vb*code]

    tags around ur code! (without the stars of course)

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Nov 2002
    Posts
    31
    Thanks for all of your replies, but I can't get it right every time an error occurs and one parameter is missing. I tried it with one table and it worked. I think that something is wrong with the join statement.
    And I forgot to say that all fields are of string type.

  6. #6
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313
    Your original SQL statement looks OK to me.

    However, are you using a DataCombo control?
    If so, you need to set the ListField property (and I think the BoundField property too).

  7. #7
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478

    Re: help needed in SQL Statement

    if the relation between them tables is id an vf_code then change the string to
    VB Code:
    1. strSQL1 = "SELECT value_f.V_NAME "
    2. strSQL1 = strSQL1 & "FROM value_f Inner join db_field ON db_field.ID=value_f.VF_CODE Where db_field.FIELD_N ='" & cboCity.BoundText & "'"

    You have to specify the relation between them tables with a join statement.

    Join can be used differently (Right,Left, Inner,..)
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  8. #8
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313
    swatty, your JOIN syntax is equivalent to the join in the original post.
    Same thing different syntax.

  9. #9
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    I like to keep the join statement seperated from the where clause.

    This is more readable and maintainable. (my opinion)
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  10. #10
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313
    Fair point - and obviously personal preferences count for a lot.
    I too prefer JOIN syntax, but not all RDBMS support the same level of JOIN syntax.

  11. #11
    Hyperactive Member
    Join Date
    Oct 2000
    Posts
    303

    Can't be!

    strSQL1 = "SELECT V.V_NAME"
    strSQL1 = strSQL1 & " FROM value_f as V, db_field as F "
    strSQL1 = strSQL1 & " WHERE F.ID=V.VF_CODE and "
    strSQL1 = strSQL1 & " F.FIELD_N ='" & cboCity.BoundText & "'"

    Real example without any junk ticks:

    SELECT V.V_NAME
    FROM Value_f as V,
    db_field as F,
    WHERE F.ID = V.VF_CODE
    AND F.FIELD_N = 'your value goes here';

    Generaal Syntax:
    SELECT A.fielddname, B.fieldname
    FROM table1 as A, table2 as B
    WHERE A.columnX = B.columnY
    AND B.columnZ = NumberValue; should work!

    MSGBOX out and make sure that your SQL is good--meaning
    having the proper space between each word/syntax.

    Also, check out SQL syntax on the WEB/Search engine for
    more info

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Nov 2002
    Posts
    31
    Well let me tell you what I got after experimenting a little bit. I have different field called F_VCODE1 in db_field. After I changed FIELD_N in db_field to F_VCODE1, the SQL worked. The code is much similar to the one posted by swatty. I got it generated by MSSQL.
    strSQL1 = "SELECT value_f.V_NAME "
    strSQL1 = strSQL1 & "FROM db_field LEFT OUTER JOIN value_f ON db_field.ID = value_f.VF_CODE "
    strSQL1 = strSQL1 & "WHERE db_field.FIELD_N = '" & cboCity.BoundText & "'"
    But why is it so?
    The fields are of the same type, but SQL works with F_VCODE1 and not with FIELD_N. I tried it with 3 more fields in db_field, but the code worked only with F_VCODE1.

  13. #13
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    Can you show the structure of the table and its contents ?

    It has to do with the join statement the left outer join will give all records from the left table and only the ones from the right where there is a matching record on the left table.

    If you specify a where clause on the right table but no relationed record is found on the left then there are no records returned.
    Last edited by swatty; Dec 4th, 2002 at 03:41 AM.
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Nov 2002
    Posts
    31
    Find the tables attached
    Thanks again
    Attached Files Attached Files

  15. #15
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    Like I said the join does the trick

    VB Code:
    1. "SELECT value_f.V_NAME
    2. FROM db_field INNER JOIN value_f ON db_field.ID = value_f.VF_CODE
    3. WHERE ((db_field.FIELD_N)='" & cboCity.BoundText & "')"

    This works for me
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Nov 2002
    Posts
    31
    Doesn't work for me.
    Seems that there are no errors but the result is empty.
    I put a datagrid and set its datasource to final recordset and it stays empty too. It shows the field name V_NAME and that's all.

  17. #17
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    You have to select a value where there is a related record in value_f

    This is not so for all records of db_field
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Nov 2002
    Posts
    31
    That's what I do.
    I select the second record from db_field.FIELD_N, which has ID = 2,
    value_f.VF_CODE has 2 values in it, which means that it shoud populate the datagrid or combo with values from value_f.V_NAME that have VF_CODE=2 Right?
    But nothing happens.

    Can you look at the code if I send you it?

  19. #19
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    Yes i can but can you first check what is in the boundtext of the combo.

    I think this is the id and not the value of it.

    If it is the id then you can simplify the select statement to this
    VB Code:
    1. "SELECT V_NAME FROM value_f  
    2. WHERE ((VF_CODE)='" & cboCity.BoundText & "')"

    Can you try this out first.

    If not working can you
    tell me what is in cboCity.BoundText when you debug.print it before the string is populated with it.
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  20. #20

    Thread Starter
    Junior Member
    Join Date
    Nov 2002
    Posts
    31
    The query you have suggested works OK. I can get all the fields if I work with one table. But with the second one it stucks.

  21. #21
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    The second will work if you change the where statement to

    WHERE ((db_field.Id ='" & cboCity.BoundText & "'))"

    Because the boundtext is the id of the record you must check against the id and not against the value of Field_N
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  22. #22

    Thread Starter
    Junior Member
    Join Date
    Nov 2002
    Posts
    31
    No I have FIELD_N in the boundtext, because I have it loaded in the Form_Load.
    I would be very grateful if you could look at the Project and send it to [email protected]
    Thanks again
    Attached Files Attached Files

  23. #23
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    The fonts are not the same and therefore you will not get any good result it would be better to set the boundtext to the id field and sort on that
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

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