2011-02-04 40 views
7

thể Duplicates:
Get all field names in Microsoft Access Table using SQL
MS Access 2010 Get column namescách lấy tên cột của bảng trong truy cập ms?

hi all,

Làm thế nào để có được fieldnames hoặc columnname của một bảng trong MSAccess sử dụng truy vấn?

Ai đó có thể giúp tôi khắc phục sự cố này không?

Giống như nếu tôi có bảng gọi là "nhân viên", tôi cần tìm nạp tên trường (id, name, workstatus..etc) của nhân viên một mình chứ không phải hồ sơ của nó ... Có phương pháp nào để lấy nó không?

Cảm ơn

+1

Duplicate của: http://stackoverflow.com/questions/2221250/get-all-field-names-in-microsoft-access-table-using-sql –

+0

@Shawn: thực sự là câu hỏi được yêu cầu cụ thể cho SQL, điều này là không thể. Tuy nhiên, câu hỏi của Yugal ở đây * là * một bản sao của: http://stackoverflow.com/questions/3343922/ms-access-2010-get-column-names –

+0

Chúng tôi đồng ý rằng nó là một bản sao. – Fionnuala

Trả lời

13

Dưới đây là một số mã tôi đã sử dụng trong quá khứ để theo dõi việc cho kiểm toán vv Tôi không thể mất tín dụng cho nó, tôi thấy nó trên lứa tuổi web trước

Sub GetField2Description() 
'********************************************************** 
'Purpose: 1) Deletes and recreates a table (tblFields) 
'   2) Queries table MSysObjects to return names of 
'    all tables in the database 
'   3) Populates tblFields 
'Coded by: raskew 
'Inputs: From debug window: 
'   Call GetField2Description 
'Output: See tblFields 
'********************************************************** 

Dim db As Database, td As TableDef 
Dim rs As Recordset, rs2 As Recordset 
Dim Test As String, NameHold As String 
Dim typehold As String, SizeHold As String 
Dim fielddescription As String, tName As String 
Dim n As Long, i As Long 
Dim fld As Field, strSQL As String 
n = 0 
Set db = CurrentDb 
' Trap for any errors. 
    On Error Resume Next 
tName = "tblFields" 

'Does table "tblFields" exist? If true, delete it; 
docmd.SetWarnings False 
    docmd.DeleteObject acTable, "tblFields" 
docmd.SetWarnings True 
'End If 
'Create new tblTable 
db.Execute "CREATE TABLE tblFields(Object TEXT (55), FieldName TEXT (55), FieldType TEXT (20), FieldSize Long, FieldAttributes Long, FldDescription TEXT (20));" 

strSQL = "SELECT MSysObjects.Name, MSysObjects.Type From MsysObjects WHERE" 
strSQL = strSQL + "((MSysObjects.Type)=1)" 
strSQL = strSQL + "ORDER BY MSysObjects.Name;" 

Set rs = db.OpenRecordset(strSQL) 
If Not rs.BOF Then 
    ' Get number of records in recordset 
    rs.MoveLast 
    n = rs.RecordCount 
    rs.MoveFirst 
End If 

Set rs2 = db.OpenRecordset("tblFields") 

For i = 0 To n - 1 
    fielddescription = " " 
    Set td = db.TableDefs(i) 
    'Skip over any MSys objects 
    If Left(rs!Name, 4) <> "MSys" And Left(rs!Name, 1) <> "~" Then 
     NameHold = rs!Name 
     On Error Resume Next 
     For Each fld In td.Fields 
      fielddescription = fld.Name 
      typehold = FieldType(fld.Type) 
      SizeHold = fld.Size 
      rs2.AddNew 
      rs2!Object = NameHold 
      rs2!FieldName = fielddescription 
      rs2!FieldType = typehold 
      rs2!FieldSize = SizeHold 
      rs2!FieldAttributes = fld.Attributes 
      rs2!FldDescription = fld.Properties("description") 
      rs2.Update 
     Next fld 

     Resume Next 
    End If 
    rs.MoveNext 
Next i 
rs.Close 
rs2.Close 
db.Close 
End Sub 

Function FieldType(intType As Integer) As String 

Select Case intType 
    Case dbBoolean 
     FieldType = "dbBoolean" '1 
    Case dbByte 
     FieldType = "dbByte"  '2 
    Case dbInteger 
     FieldType = "dbInteger" '3 
    Case dbLong 
     FieldType = "dbLong"  '4 
    Case dbCurrency 
     FieldType = "dbCurrency" '5 
    Case dbSingle 
     FieldType = "dbSingle"  '6 
    Case dbDouble 
     FieldType = "dbDouble"  '7 
    Case dbDate 
     FieldType = "dbDate"  '8 
    Case dbBinary 
     FieldType = "dbBinary"  '9 
    Case dbText 
     FieldType = "dbText"  '10 
    Case dbLongBinary 
     FieldType = "dbLongBinary" '11 
    Case dbMemo 
     FieldType = "dbMemo"  '12 
    Case dbGUID 
     FieldType = "dbGUID"  '15 
End Select 

End Function 
+2

Bạn đang tìm kiếm điều này ở khắp mọi nơi. Cảm ơn, một năm rưỡi sau! –

Các vấn đề liên quan