Results 1 to 5 of 5

Thread: Populate a combo box with results from SQL query

  1. #1

    Thread Starter
    Addicted Member run_GMoney's Avatar
    Join Date
    May 2002
    Location
    Detroit
    Posts
    186

    Populate a combo box with results from SQL query

    I'm not sure exactly how to go about doing this. I was hoping the SQL would be something like:
    VB Code:
    1. "SELECT gameid FROM game WHERE hometeamid = " & Text1.Text & " OR awayteamid = " & Text1.Text)
    That doesn't work, doesn't like breaking the "" like that. So basically, what I'm doing is creating a recordset from one table (team). The values are displayed one at a time in a textbox (Text1). A command button is used to browse through the values. I would hope that I could populate a combo box with the value(s) in another table (game) that matched the Text1.Text. Is it clear what I'm asking for? I'm not even sure if I know what I'm talking about. Anyway, if somebody has an idea of what I'm looking for I'd appreciate it. Thanks
    Place Your VBForums Ad Here

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Populate a combo box with results from SQL query

    well what data types are hometeamid and awayteamid ?? if they are string you need additional ' marks around them.. like
    Code:
    "SELECT gameid FROM game WHERE hometeamid = '" & Text1.Text & "' OR awayteamid = '" & Text1.Text & "'")
    otherwise from here the SQL statement looks ok... i could be missing something though

  3. #3
    Let me in .. techyspecy's Avatar
    Join Date
    Aug 2002
    Location
    Back to VBF.
    Posts
    2,456

    Re: Populate a combo box with results from SQL query

    Originally posted by run_GMoney
    I'm not sure exactly how to go about doing this. I was hoping the SQL would be something like:
    VB Code:
    1. "SELECT gameid FROM game WHERE hometeamid = " & Text1.Text & " OR awayteamid = " & Text1.Text)
    That doesn't work, doesn't like breaking the "" like that. So basically, what I'm doing is creating a recordset from one table (team). The values are displayed one at a time in a textbox (Text1). A command button is used to browse through the values. I would hope that I could populate a combo box with the value(s) in another table (game) that matched the Text1.Text. Is it clear what I'm asking for? I'm not even sure if I know what I'm talking about. Anyway, if somebody has an idea of what I'm looking for I'd appreciate it. Thanks
    Try this ..

    strSql = "SELECT gameid FROM game WHERE (hometeamid = " & Text1.Text & " OR awayteamid = " & Text1.Text & ")"

    Then use the variable ....

  4. #4

    Thread Starter
    Addicted Member run_GMoney's Avatar
    Join Date
    May 2002
    Location
    Detroit
    Posts
    186
    Matt, yes the values are strings. I fixed that but got some other errors. It's probably gonna be easier to just start over I think. I gotta be doing a million things wrong. Is there a tutorial somewhere (been searching for a couple hours this morning with little success) that might allude to a way of doing this. Or perhaps someone here could help me out. What I'm not sure of is how to assign the results of an SQL query to a combo box. In general, I know that this code loops through the values in the teamid field
    from the team table
    VB Code:
    1. Do Until rs.EOF = True
    2.     Combo1.AddItem rs.Fields("TEAMID")
    3.     rs.MoveNext
    4. Loop
    I want something similar, that checks the game.hometeamid and game.awayteamid and if either one matches the value from the team.teamid (that's currently displayed in the Text1 textbox), it adds the item to the combo box.....inhale....exhale....ok.
    VB Code:
    1. Do Until rs.EOF = True
    2.     Combo1.AddItem results from SQL query???
    3.     rs.MoveNext
    4. Loop
    I guess that's my main problem. Also do I need to set up a seperate Recordset if I'm working with 2 different tables? Clueless at this point. Thanks for the help so far, though.
    Place Your VBForums Ad Here

  5. #5

    Thread Starter
    Addicted Member run_GMoney's Avatar
    Join Date
    May 2002
    Location
    Detroit
    Posts
    186
    Hey new error here. I'm not sure if I'm doing this right so feel free to correct me here. I've declared a recordset and am trying to assign the result of an SQL query to it:
    VB Code:
    1. Set rs = New ADODB.Recordset
    2.  
    3. rs.Open "SELECT * FROM GAME WHERE HOMETEAMID = '" & Text1.Text & "' OR AWAYTEAMID = '" & Text1.Text & "'"
    It was my hope that I could then loop through these results and use them to populate my combo box. Not so much. I get an error:
    Code:
    Run-time error '3709';
    
    The connection cannot be used to perform this operation. It is either
    closed or invalid in this context.
    I must be using it in the wrong context. So what the heck is the right context??
    Place Your VBForums Ad Here

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