Userform Combobox mit Access Daten befüllen (ADO)
21.01.2004 11:07:33
Gorginio
Wie im Betreff bereits geschrieben, habe ich das Problem, dass die Comboboxen )(cboEdit, cboDelete) in meiner Userform, keine Daten aus der Datenbank bekommen und ich aber nicht weiss, wo der Fehler liegen könnte.
Die ADO Libary (MS ActiveX 2.7) ist aktiviert.
Der Pfad zu der Accessdb stimmt (Abrufe in ein Excelsheet funktionieren).
WAID... ist der Feldname des Autokey in Access
Vielleicht findet sich ja ein Meister, der mir bezüglich des Codes einige Tips geben kann.
Vielen Dank im Vorraus
Gorginio
Hier nun der Code:
Option Explicit
Private objConn As ADODB.Connection
Private rst As ADODB.Recordset
Private strSQL As String
Private Sub userform_Load()
Dim strPath As String
fraEdit.BorderStyle = vbBSNone
fraEdit.Visible = False
cmdEdit.Enabled = False
cmdDelete.Enabled = False
strPath = Application.path
If Right$(strPath, 1) <> "\" Then strPath = strPath & "\"
Set objConn = New ADODB.Connection
With objConn
.Provider = "Microsoft Jet 4.0 OLE DB Provider"
.ConnectionString = "Data Source=" & strPath & "AMS.mdb"
.Open
End With
Combo_Fill
End Sub
Private Sub Combo_Fill()
Set rst = New ADODB.Recordset
With rst
.ActiveConnection = objConn
.CursorLocation = adUseClient
.Source = "SELECT Activity.* FROM Activity"
.Open
End With
If rst.EOF Then Exit Sub
cboEdit.Clear
cboDelete.Clear
Do While Not rst.EOF
With cboEdit
.AddItem rst.Fields("Name")
.ItemData(.NewIndex) = rst.Fields("WAID")
End With
With cboDelete
.AddItem rst.Fields("Name")
.ItemData(.NewIndex) = rst.Fields("WAID")
End With
rst.MoveNext
Loop
rst.Close
Set rst = Nothing
End Sub