Results 1 to 13 of 13

Thread: Combo Box and SQL

  1. #1

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Question Combo Box and SQL

    I have been trying to populate a combo box with the data contained in a table in a MS SQL Database. I have searched the BB for this information but I was unable to combine the posts in a manner that made sense to me.

    I have a table located in SQL called Current_Users that I have linked to my vb project via the dataenvironment1. This table contains two (2) fields; UserName and P_Word. I want to use contains of the UserName field to populate ComboBox1. How can I accomplish this?


    Thank You,

    Mark

  2. #2
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    VB Code:
    1. Dim conn As ADODB.Connection
    2. Dim rs As ADODB.Recordset
    3. Dim strSql As String
    4.  
    5. Set conn = "Driver={SQL Server};Server=ServerName;Database=dbName;Uid=Username;Pwd=password;"
    6. Set strSql = "SELECT * FROM Current_Users"
    7.  
    8. rs.Open(strSql, conn, 3, 3)
    9.  
    10. If(rs.eof) then
    11.    MsgBox("There are no records in the database")
    12. Else
    13.    do until rs.eof
    14.       combobox1.Add rs("Username") 'add the field to the combobox
    15.       rs.movenext
    16.    loop
    17. End
    18.  
    19. rs.Close
    20. conn.Close
    21.  
    22. Set rs = nothing
    23. Set conn = nothing
    24. Set strSql = nothing
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  3. #3

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Wink References

    I am now getting an error regarding the line:


    Dim conn As ADODB.Connection

    Error:

    Compile Error
    User Defined Type Not Defined

    I have the Microsoft ADO Ext. 2.7 for DLL and Security reference loaded.

    Any suggestions?

    Mark
    Last edited by Mark Gambo; Sep 23rd, 2003 at 06:35 PM.

  4. #4
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    add a reference to Microsoft ADO 2.x library.
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  5. #5

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Combo Box

    Thanks, it worked like a champ!

    I am using this combo box to so that the user can select a his/her name from the drop down list. I would like them to be able to type the first letter of the last name form in order for them to select there name.

    i.e., List
    AAronn, John
    Abhrams, Roberta
    Able, Edward
    Adams, Sara

    I want Edward Able to press the "A" key three (3) times in order to select his name.

    How can I do this?

    Regards,

    Mark

  6. #6
    Addicted Member
    Join Date
    Sep 2003
    Posts
    160
    try this:

    Dim Backspaced As Boolean

    Private Sub cboNom_Change()

    If Backspaced = True Or cboNom.Text = "" Then
    Backspaced = False
    Exit Sub
    End If

    Dim i As Long
    Dim nSel As Long

    For i = 0 To cboNom.ListCount - 1
    If InStr(1, cboNom.List(i), cboNom.Text, _
    vbTextCompare) = 1 Then
    nSel = cboNom.SelStart
    cboNom.Text = cboNom.List(i)
    cboNom.SelStart = nSel
    cboNom.SelLength = Len(cboNom.Text) - nSel
    Exit For
    End If
    Next

    End Sub

    Private Sub cboNom_KeyDown(KeyCode As Integer, Shift As Integer)

    If KeyCode = vbKeyBack Or KeyCode = vbKeyDelete Then
    If cboNom.Text <> "" Then
    Backspaced = True
    End If
    End If

    End Sub

  7. #7
    Addicted Member
    Join Date
    Sep 2003
    Posts
    160
    ops I didn't read your post very well at first beginning the code I post is for autocompleting but let me know if you really need only one character of first name so I'll modify if you think the one I sent is better then cheers

    Hope it helps.

  8. #8

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965
    Thanks for the code!

    Yeah, I was looking for the user to continue to press the letter of the last name until there get the name they are looking for.

    Thank you again!!!
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  9. #9
    Addicted Member
    Join Date
    Sep 2003
    Posts
    160

    LoL

    hi man
    this is so funny, I never notice, default ComboBox does already have that thing, I just started to do that but it does have it.
    have you ever give it a try?


    Hope it helps.

  10. #10

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965
    I tried it before and it would not work. I'll create a new one and see if it works. Thanks again.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  11. #11

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965
    vb,

    I tried it and I could not get it to work. I tried your code and the autocomplete will be handy for another portion of my program. Do you know what I could be doing wrong regarding my original post?
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  12. #12
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016
    Set the Style property on your Combo box to 2 - Dropdown List.
    Chris

    Master Of My Domain
    Got A Question? Look Here First

  13. #13

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965
    That did the trick, Thanks
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread 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