could you help me please. I need to fill a combo box with all the names
of the tables in a database I am using. Any ideas of a good way to go
about it.
Public catNew As New ADOX.Catalog
Dim i as integer
'list all tables in CmbTables
For i = 0 To catNew.Tables.Count - 1
If Left(catNew.Tables(i).Name, 3) = "Tbl" Then _
CmbTables.AddItem catNew.Tables(i).Name
Next i
CmbTables = CmbTables.List(0) 'select first column
My ComboBox is called CmbTables. All my Tablenames start with "Tbl", but if yours don't I'm sure you can filter your tablenames out from what catNew.tables returns.
Form_Load() would be a good place to put it. I use SS tab and run the code then the user changes tab.
You have to have a reference to Microsoft ADO Ext. 2.5 for dll and security (MSADOX.dll). I assume that you allready have a reference to Microsoft ActiveX Objects 2.5 Library (MSADO25.tlb). Or other version of these libraries.
The catalog (CatNew) does not need to be public unless you want to use it public...
I have put this code into the Form_load().
I have left out the IF stmt as I don't want to filter the tables.
The combo box does not contain any information when I run it.
I have a adodc connecting the database.
Dim catNew As New ADOX.Catalog
Dim i As Integer
'Dim catNew As String
'list all tables in CmbTables
For i = 0 To catNew.Tables.Count - 1
'If Left(catNew.Tables(i).Name, 3) = "Tbl" Then
cmbQ1Table1.AddItem catNew.Tables(i).Name
Next i
' cmbQ1Table1 = cmbQ1Table1.List(0) 'select first column
'End If
'Copy this code into a form with a combobox (combo1)
'Set references to Microsoft ADO Ext. 2.5 for dll and security (MSADOX.dll)
'and Microsoft ActiveX Objects 2.5 Library (MSADO25.tlb).
Option Explicit
Dim con As ADODB.Connection
Dim catNew As New ADOX.Catalog
Dim rst As Recordset
Private Sub Form_Load()
Dim StrCon As String, i As Integer
'Create database
If Dir("c:\test.mdb" <> "" Then Kill "c:\test.mdb"
StrCon = catNew.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\test.mdb"
Set con = catNew.ActiveConnection
'Insert values into tblproject
con.Execute ("INSERT INTO TblProject (name) VALUES ('TESTPROJECT')"
'Insert values into tblPerson incl foreign key
Set rst = con.Execute("SELECT ID FROM TblProject WHERE name='TESTPROJECT'"
con.Execute ("INSERT INTO TblPerson (projectID,surname) VALUES (" + CStr(rst.Fields(0)) + ",'testname')"
'populate combobox with table names
For i = 0 To catNew.Tables.Count - 1
If Left(catNew.Tables(i).Name, 3) = "Tbl" Then _
Combo1.AddItem catNew.Tables(i).Name
Next i
Combo1 = Combo1.List(0) 'select first column
con.Close
End Sub
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.