I have been using the datagrid with ADO, but I want to use the datagrid to add to an empty table, my problem is how do I set up the columns so that the column headings are the field names? I keep getting a rowset error.
Printable View
I have been using the datagrid with ADO, but I want to use the datagrid to add to an empty table, my problem is how do I set up the columns so that the column headings are the field names? I keep getting a rowset error.
If you are using SQL Server you can use the syscolumns table to be able to generate a list of the columns for the table in question and use that information to populate the column headers.
This gives you a list of the fields in the table. If you want to get REALLY smart you can include the datatype into the statement, connect to the systypes table and also determine the width each field needs to be to display ALL the data... but that would triple the complexity of the queryCode:SELECT sc.Name as ColumnName
FROM sysobjects so LEFT JOIN syscolumns sc ON so.Id = sc.Id
WHERE so.Type = 'U'
AND so.Name = '<Insert Table Name Here>'
Your alternative is to open a recordset.
See how that goesCode:Dim iField as Integer
Dim sMsg as String
Set rstTemp = db.OpenRecordset("TableName",dbOpenSnapshot, dbReadOnly, dbReadOnly)
For iField = 1 to rstTemp.Fields.Count
sMsg = sMsg & "Field called " & rstTemp.Fields(iField).Name & vbCR
< You can assign to the header of this column here>
Next iField
MsgBox sMsg