Userform Combo Box Error

Inactiveb0347

New Member
Joined
Jan 13, 2014
Messages
20
Hey

I'm trying to get my userform combo box to link to a list I've got in access. The table name is called FoodCat and the field name is CategoryOne. I've been trying to figure out for hours why I keep getting a Run Time Error - Type Mismatch.

Is anyone able to help me with this?


Code:
Code:
Dim RecSet As ADODB.Recordset
Dim SQL As String

Private Sub UserForm_Initialize()


SQL = "SELECT * FROM FoodCat"
Set RecSet = dbConnect(SQL)


RecSet.MoveFirst
    With Me.cboFoodCat
        .Clear
        Do
            .AddItem RecSet!CategoryOne [COLOR=#ff0000][B]<---- Run Time Error Happens Here[/B][/COLOR]


            RecSet.MoveNext
        Loop Until RecSet.EOF
    End With


End Sub


Access Connection Function:

Code:
Option Explicit
Private Const AccessConnection = "[B]Have removed this so no one can see[/B]"


Public Function dbConnect(SQL As String) As ADODB.Recordset


    'On Error GoTo error_Catch


    Dim RecSet As New ADODB.Recordset
    Dim DBConn As New ADODB.Connection


    Set RecSet = Nothing


    With DBConn
        .Provider = "Microsoft.ACE.OLEDB.12.0"
        .Open (AccessConnection)
    End With


        RecSet.ActiveConnection = DBConn
        RecSet.CursorLocation = adUseClient
        RecSet.Open (SQL)
        
    Set RecSet.ActiveConnection = Nothing
    Set dbConnect = RecSet


    Set RecSet = Nothing


    DBConn.Close


error_Catch:


    If Err <> 0 Then
        MsgBox ("Error Message")
    If DBConn.State = 1 Then
        DBConn.Close
    End If
    
    Exit Function
    End If


End Function
 

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.
Try...

Code:
Private Sub UserForm_Initialize()
 Dim RecSet As ADODB.Recordset
 Dim SQL As String
 
 SQL = "SELECT [CategoryOne] FROM FoodCat"
 Set RecSet = dbConnect(SQL)

 With Me.cboFoodCat
   If RecSet.RecordCount Then
      .List = Application.Transpose(RecSet.GetRows)
   Else
      MsgBox "No records found"
   End If

 End With
 Set RecSet = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,825
Members
449,096
Latest member
Erald

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