I have two questions on ADO. The first one is the most necessary as the second one is only for optimising working code.
1) Using ADO how do I create a new field in an Access table?
2) I got this code and reworked it to put all the field names in a table into a list box. How ever, to do this it cycles through EVERY field in EVERY table in the database (I think). Clearly, there must be a more efficient way although my code work fine.
Private Sub Tables_Click()
'ONCE A TABLE HAS BEEN SELECTED, A LIST OF FIELDS FOR THAT TABLES IS DISPLAYED
Fields.Clear 'clear list from previous display
Dim adoConnection As ADODB.Connection
Dim adoRsFields As ADODB.Recordset
Dim sConnection As String
Set adoConnection = New ADODB.Connection
sConnection = "Provider=Microsoft.Jet.OLEDB.3.51;Data Source=" + DatabaseDir.Path + "\" + DatabaseFile.Text 'connects to selected database
adoConnection.Open sConnection
Set adoRsFields = adoConnection.OpenSchema(adSchemaColumns)
sCurrentTable = ""
sNewTable = ""
Do Until adoRsFields.EOF 'run through every field in EVERY table (not good, should only select one table)
If adoRsFields!TABLE_NAME = Tables.List(Tables.ListIndex) Then 'wait until table matches the selected one
Fields.AddItem adoRsFields!COLUMN_NAME 'add the column name of that field (ie field name) to the list box
End If
adoRsFields.MoveNext 'onto next field
Loop
adoRsFields.Close
Set adoRsFields = Nothing
adoConnection.Close 'close connection
Set adoConnection = Nothing
End Sub
1) Using ADO how do I create a new field in an Access table?
2) I got this code and reworked it to put all the field names in a table into a list box. How ever, to do this it cycles through EVERY field in EVERY table in the database (I think). Clearly, there must be a more efficient way although my code work fine.
Private Sub Tables_Click()
'ONCE A TABLE HAS BEEN SELECTED, A LIST OF FIELDS FOR THAT TABLES IS DISPLAYED
Fields.Clear 'clear list from previous display
Dim adoConnection As ADODB.Connection
Dim adoRsFields As ADODB.Recordset
Dim sConnection As String
Set adoConnection = New ADODB.Connection
sConnection = "Provider=Microsoft.Jet.OLEDB.3.51;Data Source=" + DatabaseDir.Path + "\" + DatabaseFile.Text 'connects to selected database
adoConnection.Open sConnection
Set adoRsFields = adoConnection.OpenSchema(adSchemaColumns)
sCurrentTable = ""
sNewTable = ""
Do Until adoRsFields.EOF 'run through every field in EVERY table (not good, should only select one table)
If adoRsFields!TABLE_NAME = Tables.List(Tables.ListIndex) Then 'wait until table matches the selected one
Fields.AddItem adoRsFields!COLUMN_NAME 'add the column name of that field (ie field name) to the list box
End If
adoRsFields.MoveNext 'onto next field
Loop
adoRsFields.Close
Set adoRsFields = Nothing
adoConnection.Close 'close connection
Set adoConnection = Nothing
End Sub