Results 1 to 8 of 8

Thread: HELP! Combobox Requery/Selection Problem in Access VB

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    23

    HELP! Combobox Requery/Selection Problem in Access VB

    Ok I have been programming in VB access for about 2 weeks and need help. I just found out how to query the database and load information into the combobox which is great but it still has bugs. When the user clicks on the equipment or the manufacture combobox I need it to requery(get rid of existing values which it is doing) and populate the model combobox(will not and giving me the error that it has no current recordset). My second issue is when I try to select the value it does not select. I created a counter ctr so it wont keep populating after multiple clicks. Here is my code thus far:


    Dim ctr As Integer
    Public Sub Event_Counter()

    ctr = ctr + 1
    End Sub


    Private Sub btn_add_inv_entry_Click()


    On Error GoTo Err_btn_add_inv_entry_Click

    Dim stDocName As String


    Exit_btn_add_inv_entry_Click:
    Exit Sub

    Err_btn_add_inv_entry_Click:
    MsgBox Err.Description
    Resume Exit_btn_add_inv_entry_Click

    End Sub


    Private Sub cbo_equip_type_Click()

    cbo_Model_No.RowSource = vbNullString
    cbo_Model_No.Requery
    ctr = 0
    End Sub

    Private Sub cbo_MFG_Click()
    ctr = 0
    cbo_Model_No.RowSource = vbNullString
    cbo_Model_No.Requery
    ctr = 0


    End Sub

    Private Sub cbo_Model_No_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

    Dim ADOCn As ADODB.Connection
    Dim ConnString As String
    Dim adoRS As ADODB.Recordset
    Dim sSQL As String
    Dim MFG As Variant
    Dim EquipType As Variant

    MFG = cbo_MFG.Value
    EquipType = cbo_equip_type.Value
    cbo_Model_No.RowSourceType = "Value List"

    ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=\\Fileserver\Data\Inventorydb\Techway.mdb;" & _
    "Persist Security Info=False"

    Set ADOCn = New ADODB.Connection
    ADOCn.ConnectionString = ConnString
    ADOCn.Open ConnString
    Set adoRS = New ADODB.Recordset
    sSQL = "Select ID, Model_No From dbo_MFG_Model_No Where MFG_Name Like '" & MFG & "' And Equip_Type Like '" & EquipType & "'"
    adoRS.Open sSQL, ADOCn



    If ctr = 0 Then
    Do Until adoRS.EOF
    'It is Crapping out saying no recordset and requery I suspect is the culprit
    cbo_Model_No.AddItem adoRS.Fields.Item("Model_No").Value
    adoRS.MoveNext
    Loop
    End If


    adoRS.Close
    ADOCn.Close
    Set ADOCn = Nothing
    Set adoRS = Nothing
    Event_Counter
    End Sub

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

    Re: HELP! Combobox Requery/Selection Problem in Access VB

    Why are you using the MouseDown event as opposed to the Click event?

    You LIKE statements are missing the necessary wildcard character. With Access, the wildcard character is the *

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    23

    Re: HELP! Combobox Requery/Selection Problem in Access VB

    I am not using a access database but a sql database on the backend. I will try mouse click and see what that does.

    Thank you

    _Stephen

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    23

    Re: HELP! Combobox Requery/Selection Problem in Access VB

    Ok. Tried that. Now it doesnt work at all. It liked the mouse down event better for some reason. Any ideas?

    _Stephen

  5. #5
    Member
    Join Date
    Nov 2004
    Posts
    48

    Re: HELP! Combobox Requery/Selection Problem in Access VB

    VB Code:
    1. Dim ctr As Integer
    2. Public Sub Event_Counter()
    3.  
    4. ctr = ctr + 1
    5. End Sub
    6.  
    7.  
    8. Private Sub btn_add_inv_entry_Click()
    9.  
    10.  
    11. On Error GoTo Err_btn_add_inv_entry_Click
    12.  
    13. Dim stDocName As String
    14.  
    15.  
    16. Exit_btn_add_inv_entry_Click:
    17. Exit Sub
    18.  
    19. Err_btn_add_inv_entry_Click:
    20. MsgBox Err.Description
    21. Resume Exit_btn_add_inv_entry_Click
    22.  
    23. End Sub
    24.  
    25.  
    26. Private Sub cbo_equip_type_Click()
    27.  
    28. cbo_Model_No.RowSource = vbNullString
    29. cbo_Model_No.Requery
    30. ctr = 0
    31. End Sub
    32.  
    33. Private Sub cbo_MFG_Click()
    34. ctr = 0
    35. cbo_Model_No.RowSource = vbNullString
    36. cbo_Model_No.Requery
    37. ctr = 0
    38.  
    39.  
    40. End Sub
    41.  
    42. Private Sub cbo_Model_No_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    43.  
    44. Dim ADOCn As ADODB.Connection
    45. Dim ConnString As String
    46. Dim adoRS As ADODB.Recordset
    47. Dim sSQL As String
    48. Dim MFG As Variant
    49. Dim EquipType As Variant
    50.  
    51. MFG = cbo_MFG.Value
    52. EquipType = cbo_equip_type.Value
    53. cbo_Model_No.RowSourceType = "Value List"
    54.  
    55. ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    56. "Data Source=\\Fileserver\Data\Inventorydb\Techway.mdb;" & _
    57. "Persist Security Info=False"
    58.  
    59. Set ADOCn = New ADODB.Connection
    60. ADOCn.ConnectionString = ConnString
    61. ADOCn.Open ConnString
    62. Set adoRS = New ADODB.Recordset
    63. sSQL = "Select ID, Model_No From dbo_MFG_Model_No Where MFG_Name Like '" & MFG & "' And Equip_Type Like '" & EquipType & "'"
    64. adoRS.Open sSQL, ADOCn
    65.  
    66.  
    67.  
    68. If ctr = 0 Then
    69. Do Until adoRS.EOF
    70. 'It is Crapping out saying no recordset and requery I suspect is the culprit
    71. cbo_Model_No.AddItem adoRS.Fields.Item("Model_No").Value
    72. adoRS.MoveNext
    73. Loop
    74. End If
    75.  
    76.  
    77. adoRS.Close
    78. ADOCn.Close
    79. Set ADOCn = Nothing
    80. Set adoRS = Nothing
    81. Event_Counter
    82. End Sub

    just because its 2am.. and i need to save my poor little eyes

  6. #6
    Member
    Join Date
    Nov 2004
    Posts
    48

    Re: HELP! Combobox Requery/Selection Problem in Access VB

    Why not just use the values in the SQL statement as they are?:

    sSQL = "SELECT ID, Model_No FROM dbo_MFG_Model_No WHERE MFG_Name LIKE [cbo_MFG.Value] AND Equip_Type LIKE [cbo_equip_type.Value]

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    23

    Re: HELP! Combobox Requery/Selection Problem in Access VB

    Ya I could have done that. But that still didnt solve my problem just made the code look prettier. The solution was a AfterUpdate event on the two comboboxs that will requery. I ended up getting rid of the code and just plugging in the information into a query on the row source on the comboboxs design mode and it worked alot better. Microsoft Office has a example on bound comboboxes that helped me alot.

    _Stephen

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    23

    Re: HELP! Combobox Requery/Selection Problem in Access VB

    Great Link to learn how to bind a combobox to another using Sql statements.



    http://office.microsoft.com/en-us/as...730581033.aspx


    _Stephen

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