get all of the field names from a table in a SQL database and write them out to a VB form?
Printable View
get all of the field names from a table in a SQL database and write them out to a VB form?
AngelinaM,
The following example lists the fields for a recordSet created from an Access database, but you should be able to apply it to a SQL database too. I just threw this code together, so minor changes may be needed to get all the fields. The most important point is that you use the ADODB.fields object to retrieve the field names.
Code:Option Explicit
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim adofields As ADODB.Fields
Private Sub Command1_Click()
Dim x As Integer
Dim adofields As ADODB.Fields
Set adofields = rs.Fields
For x = 1 To (adofields.Count - 1)
Debug.Print adofields.Item(x).Name
Next x
End Sub
Private Sub Form_Load()
Set cnn = New ADODB.Connection
Set rs = New ADODB.Recordset
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Program Files\Microsoft Visual Studio\VB98\Nwind.mdb;" & _
"Persist Security Info=False"
rs.Open "Select * from Customers", cnn, adOpenKeyset, adLockOptimistic
End Sub
THANK YOU!