Results 1 to 9 of 9

Thread: [RESOLVED] Problem with my query...

  1. #1

    Thread Starter
    Hyperactive Member DubweiserTM's Avatar
    Join Date
    Dec 2005
    Location
    St-Ferdinand, Québec
    Posts
    427

    Resolved [RESOLVED] Problem with my query...

    Hi everybody...

    I have a query that does'nt work, maybe I have an error into it or it's not possible what I'm trying to do ??? Refer to the underlined and bold line in the code below...

    I want that the strSQL query will be equal to the PrimaryKey of the value in the Combobox

    Here is the code (Sorry some words are in french):
    VB Code:
    1. 'Instanciation des variables
    2. Set cn = New ADODB.Connection
    3. Set rs = New ADODB.Recordset
    4. Set cmdSQL = New ADODB.Command
    5. Set prmSQL = New ADODB.Parameter
    6.  
    7. 'Connection Database
    8. cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & adoSource
    9. cn.Open
    10.  
    11. 'Création de la requête
    12. strSQL = "SELECT Desc_F FROM RevType WHERE TypePK in (SELECT TypePK FROM Revetement WHERE CategPK = ?)"
    13.  
    14. 'Configurer la commande
    15. cmdSQL.CommandText = strSQL
    16. cmdSQL.CommandType = adCmdText
    17.  
    18. 'Création du paramètre
    19. Set prmSQL = cmdSQL.CreateParameter("Whatever", adVarChar, adParamInput, 255, [B][U]"SELECT CategPK FROM RevCategorie WHERE Desc_F = " & cboFexRev(0).Text[/U][/B])
    20. cmdSQL.Parameters.Append prmSQL
    21.  
    22. 'Exécuter la requête paramétrée
    23. Set cmdSQL.ActiveConnection = cn
    24. Set rs = cmdSQL.Execute
    25.  
    26. 'Vider le ComboBox
    27. adoCombo.Clear
    28.  
    29. 'Positionner sur le premier recordset
    30. rs.MoveFirst
    31.         'Ajouter Items ComboBox
    32.         Do Until rs.EOF
    33.                 adoCombo.AddItem rs.Fields("Desc_F").Value
    34.                 'Déplace curseur sur prochain recordset
    35.                 rs.MoveNext
    36.         Loop
    37.  
    38. 'Fermeture de la connection ADO
    39. rs.Close
    40. Set rs = Nothing
    41. cn.Close
    42. Set cn = Nothing
    Thanks in advance !
    DubweiserTM

    If your question has been answered, you can mark a thread as resolved...

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Problem with my query...

    Is the CategPK field numeric? If yes, set the ComboBox.ItemData = CategPK when you load cboFexRev(0).
    Assuming the recordset to load the combobox has these the 2 fields CategPK and Desc_F

    VB Code:
    1. Do Until rs.EOF
    2.     cboFexRev(0).AddItem rs.Fields("Desc_F").Value
    3.     cboFexRev(0).ItemData(cboFexRev(0).NewIndex) = rs.Fields("CategPK").Value
    4.     rs.MoveNext
    5. Loop

    Then use this Parameter statement

    VB Code:
    1. Set prmSQL = cmdSQL.CreateParameter("Whatever", adInteger, adParamInput,, cboFexRev(0).ItemData(cboFexRev(0).ListIndex))

    If CategPK is not numeric, try this SQL Query

    VB Code:
    1. strSQL = "SELECT RT.Desc_F FROM RevCategorie RC " _
    2. & "Inner Join Revetement R On R.CategPK = RC.CategPK " _
    3. & "Inner Join RevType RT On RT.TypePK = R.TypePK " _
    4. & "Where RC.Desc_F = ?"
    5.  
    6. Set prmSQL = cmdSQL.CreateParameter("Whatever", adVarChar, adParamInput, 255, cboFexRev(0).Text)

    Note: the syntax of the query above for use in Access is probably incorrect.
    Last edited by brucevde; Nov 23rd, 2006 at 03:29 PM.

  3. #3

    Thread Starter
    Hyperactive Member DubweiserTM's Avatar
    Join Date
    Dec 2005
    Location
    St-Ferdinand, Québec
    Posts
    427

    Re: Problem with my query...

    Quote Originally Posted by brucevde
    Is the CategPK field numeric? If yes, set the ComboBox.ItemData = CategPK when you load cboFexRev(0).
    Yes CategPK is numeric...

    Quote Originally Posted by brucevde
    Assuming the recordset to load the combobox has these the 2 fields CategPK and Desc_F
    I use the CategPK of cboFexRev(0).text to fill another combobox cboFexRev(1)... I will add this part of the code(.itemdata) at the good place...

    Thanks !
    Last edited by DubweiserTM; Nov 23rd, 2006 at 03:45 PM.
    DubweiserTM

    If your question has been answered, you can mark a thread as resolved...

  4. #4

    Thread Starter
    Hyperactive Member DubweiserTM's Avatar
    Join Date
    Dec 2005
    Location
    St-Ferdinand, Québec
    Posts
    427

    Re: Problem with my query...

    Quote Originally Posted by DubweiserTM
    I will add this part of the code(.itemdata) at the good place...
    Finally it's not easy, because I fill all the comboboxes with a Function and in some table there is no CategPF...

    Is there another way ???

    Thanks in advance !
    DubweiserTM

    If your question has been answered, you can mark a thread as resolved...

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Problem with my query...

    You could use something similar to the sub I wrote for the Database FAQ's (here).. pass parameters to it to specify field names, and preferably pass the recordset rather than build it in the sub as I did (as the method above is better, but a little complex for the FAQ).

  6. #6

    Thread Starter
    Hyperactive Member DubweiserTM's Avatar
    Join Date
    Dec 2005
    Location
    St-Ferdinand, Québec
    Posts
    427

    Re: Problem with my query...

    Your code will be helpful ! Thanks !
    DubweiserTM

    If your question has been answered, you can mark a thread as resolved...

  7. #7

    Thread Starter
    Hyperactive Member DubweiserTM's Avatar
    Join Date
    Dec 2005
    Location
    St-Ferdinand, Québec
    Posts
    427

    Re: Problem with my query...

    Hi !

    Is someone could explain to me in details the signification of this query.

    Usually I use very simple query but this one is complicated for me and I want to understand...

    strSQL = "SELECT Desc_F FROM RevType WHERE TypePK in (SELECT TypePK FROM Revetement WHERE CategPK = ?)"

    Thanks in advance !
    DubweiserTM

    If your question has been answered, you can mark a thread as resolved...

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Problem with my query...

    I presume the highlighted parts are what concern you, so:

    Instead of doing a "SELECT *" (which returns all fields from the row) it is more efficient and accurate to specify just the fields you want, and in the order you want them, eg: "SELECT Desc_F, AnotherField, AndAnother"

    The "In" is a bit like an = , except it checks against multiple values. A simpler version would be "WHERE TypePK in (1, 2, 3)", which would return all rows where TypePK is 1, 2, or 3. In this case, instead of specifying the values, they come from the results of another query (all TypePK's in Revetement that have a specified CategPK).

  9. #9

    Thread Starter
    Hyperactive Member DubweiserTM's Avatar
    Join Date
    Dec 2005
    Location
    St-Ferdinand, Québec
    Posts
    427

    Re: Problem with my query...

    Quote Originally Posted by si_the_geek
    I presume the highlighted parts are what concern you, so:

    Instead of doing a "SELECT *" (which returns all fields from the row) it is more efficient and accurate to specify just the fields you want, and in the order you want them, eg: "SELECT Desc_F, AnotherField, AndAnother"

    The "In" is a bit like an = , except it checks against multiple values. A simpler version would be "WHERE TypePK in (1, 2, 3)", which would return all rows where TypePK is 1, 2, or 3. In this case, instead of specifying the values, they come from the results of another query (all TypePK's in Revetement that have a specified CategPK).
    Merci ! (Thanks !)
    DubweiserTM

    If your question has been answered, you can mark a thread as resolved...

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