list combobox with array from db

pape93

New Member
Joined
Feb 1, 2011
Messages
14
Hi all, I'm getting the error:
"Run-time error '3021': Either BoF or EOF is True, or the current record has been deleted. Requested operation requires a current record"

when i run this code below. I'm trying to populate a combobox with supplier names from a table.

PHP:
Sub getSupNamFromDB()
   Dim conn As ADODB.Connection
   Dim supName As ADODB.Recordset
   Dim strConn As String
   Dim supArr As Variant
   strConn = "Provider = Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & ThisWorkbook.Path & "\eps.mdb"
   Set conn = New ADODB.Connection
   conn.Open strConn
   Set supName = New ADODB.Recordset
   supName.Open "SELECT * FROM supplier_table", conn, adOpenDynamic, adLockOptimistic, adCmdText
   If Not (supName.BOF And supName.EOF) Then
 
    For Each Field In supName.Fields
      supArr = supName("sup_name").value
    'txtDescInvItem.RowSource = supArr
    'MsgBox "found"
    supName.MoveNext
    Next
    txtDescInvItem.RowSource = supArr
 
   End If
   supName.Close
   Set supName = Nothing
   conn.Close
   Set conn = Nothing
End Sub

Any help would be appreciated
thanks much.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
not tested but try this:
Code:
Sub getSupNamFromDB()
   Dim conn As ADODB.Connection
   Dim supName As ADODB.Recordset
   Dim strConn As String
   Dim supArr As Variant
   strConn = "Provider = Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & ThisWorkbook.Path & "\eps.mdb"
   Set conn = New ADODB.Connection
   conn.Open strConn
   Set supName = New ADODB.Recordset
   supName.Open "SELECT * FROM supplier_table", conn, adOpenDynamic, adLockOptimistic, adCmdText
If supname.RecordCount <> 0 Then '******************
   If Not (supName.BOF And supName.EOF) Then
 
    For Each Field In supName.Fields
      supArr = supName("sup_name").value
    'txtDescInvItem.RowSource = supArr
    'MsgBox "found"
    supName.MoveNext
    Next
    txtDescInvItem.RowSource = supArr
 
   End If
 End If   '*******************************
   supName.Close
   Set supName = Nothing
   conn.Close
   Set conn = Nothing
End Sub  

[/code
 
Upvote 0
...doh I missed the last ] in the end code tag :-(
 
Upvote 0
Thanks for the reply pcc
I tried you suggestion but no luck.. I got the same error message.
I also tried the following and got a different error message:
error: Compile error: For Each may only iterate over a collection object or an array

PHP:
Sub getSupNamFromDB()
   Dim conn As ADODB.Connection
   Dim supName As ADODB.Recordset
   Dim strConn As String
   Dim supArr As Variant
   strConn = "Provider = Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & ThisWorkbook.Path & "\eps.mdb"
   Set conn = New ADODB.Connection
   conn.Open strConn
   Set supName = New ADODB.Recordset
   supName.Open "SELECT * FROM supplier_table", conn, adOpenDynamic, adLockOptimistic, adCmdText
   If Not (supName.BOF And supName.EOF) Then
 
    For Each Record In supName.RecordCount
      supArr = supName("sup_name").value
    'txtDescInvItem.RowSource = supArr
    'MsgBox "found"
    supName.MoveNext
    Next
    txtDescInvItem.RowSource = supArr
 
   End If
   supName.Close
   Set supName = Nothing
   conn.Close
   Set conn = Nothing
End Sub

I need help. I'm sure it might be very simple but i'm a newbie at this.
thanks for you help
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,904
Members
452,948
Latest member
Dupuhini

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top