You are on page 1of 17

Print list of Field Names and Data Types Method 1

http://www.expertsexchange.com/Microsoft/Development/MS_Access/Q_22568193.html

Sub ListAllTableFields() On Error Resume Next

Dim db As DAO.Database Dim tdf As DAO.TableDef Dim fld As DAO.Field Dim rst As DAO.Recordset

Set db = CurrentDb

With CurrentProject.Connection .Execute ("DROP TABLE tblTempTableFieldList") .Execute ("CREATE TABLE tblTempTableFieldList " & _ "(TableName varChar(40), FieldCount Integer, FieldName varChar(40), FieldType varChar(40), FieldSize varChar(40), FieldDesc varChar(40))") End With

If Err Then MsgBox "Error creating temp table", vbInformation Exit Sub

End If

Set rst = db.OpenRecordset("tblTempTableFieldList", dbOpenDynaset)

With rst For Each tdf In db.TableDefs If Left(tdf.Name, 4) <> "MSys" Then For Each fld In tdf.Fields .AddNew !TableName = tdf.Name !FieldCount = tdf.Fields.Count !FieldName = fld.Name !FieldType = fFieldType(fld.Type) !FieldSize = fld.Size !FieldDesc = fld.Properties("Description") .Update Next End If Next .Close End With

Set fld = Nothing Set tdf = Nothing Set rst = Nothing Set db = Nothing

End Sub

Function fFieldType(intType As Integer)

Select Case intType Case 1 fFieldType = "Boolean" Case 3 fFieldType = "Integer" Case 4 fFieldType = "Long" Case 5 fFieldType = "Currency" Case 6 fFieldType = "Single" Case 7 fFieldType = "Double" Case 8 fFieldType = "Date" Case 10 fFieldType = "Text" Case 12 fFieldType = "Memo" End Select

End Function

Sub ListAllQueryInfo(Optional blnIncludeHidden As Boolean = False) On Error Resume Next

Dim db As Object 'Database Dim qdf As Object 'QueryDef Dim rst As Object 'dao.Recordset

Const cTempTbl = "tblTempQueryList"

Set db = CurrentDb

With CurrentProject.Connection .Execute ("DROP TABLE " & cTempTbl) DoEvents .Execute ("CREATE TABLE " & cTempTbl & " " & _ "(QueryName varChar(40), FieldCount Integer, Description varChar(50), LastMod Date, Created Date, QueryType varChar(10), SQLText Text)") End With

If Err Then MsgBox "Error creating temp table", vbInformation Exit Sub End If

Set rst = db.OpenRecordset(cTempTbl, dbOpenDynaset)

With rst For Each qdf In db.QueryDefs If Left(qdf.Name, 4) <> "MSys" And IIf(blnIncludeHidden, True, Left(qdf.Name, 3) <> "~sq") Then .AddNew !QueryName = qdf.Name !FieldCount = qdf.Fields.Count !Description = qdf.Properties("Description") !Created = qdf.DateCreated !LastMod = qdf.LastUpdated !QueryType = fQueryType(qdf.Type) !SQLText = qdf.SQL .Update End If Next .Close End With

Set qdf = Nothing Set rst = Nothing Set db = Nothing

End Sub

Function fQueryType(intType As Integer) As String

Select Case intType Case 0 fQueryType = "Select" Case 48 fQueryType = "Update" Case 32 fQueryType = "Delete" Case 64 fQueryType = "Append" Case 16 fQueryType = "Crosstab" Case 96 fQueryType = "Data Definition" Case 80 fQueryType = "Make Table" Case 240 fQueryType = "Action" Case 112 fQueryType = "Passthrough" End Select

End Function

Sub ListAllTableIndexes() On Error Resume Next

Dim db As DAO.Database Dim tdf As DAO.TableDef Dim idx As DAO.Index Dim fld As DAO.Field Dim rst As DAO.Recordset

Set db = CurrentDb

With CurrentProject.Connection .Execute ("DROP TABLE tblTempTableIndexList") Err.Clear .Execute ("CREATE TABLE tblTempTableIndexList " & _ "(TableName varChar(40), IndexCount Integer, IndexName varChar(40), IndexType varChar(10), IndexReqd varChar(3), IndexUnique varChar(3), FieldName varChar(40), SortOrder varChar(5))") End With

If Err Then MsgBox "Error creating temp table", vbInformation Exit Sub End If

Set rst = db.OpenRecordset("tblTempTableIndexList", dbOpenDynaset)

With rst For Each tdf In db.TableDefs

If Left(tdf.Name, 4) <> "MSys" Then For Each idx In tdf.Indexes For Each fld In idx.Fields .AddNew !TableName = tdf.Name !IndexCount = tdf.Indexes.Count !IndexName = idx.Name !IndexType = IIf(idx.Primary, "Primary", "Index") !IndexReqd = IIf(idx.Required, "Yes", "No") !IndexUnique = IIf(idx.Unique, "Yes", "No") !SortOrder = IIf(fld.Properties("Attributes") = 0, "ASC", "DESC") !FieldName = fld.Name .Update Next Next End If Next .Close End With

Set fld = Nothing Set idx = Nothing Set tdf = Nothing Set rst = Nothing Set db = Nothing

End Sub

Sub PrintAllTableIndexFieldProperties(strTableName As String) On Error Resume Next

Dim db As DAO.Database Dim tdf As DAO.TableDef Dim idx As DAO.Index Dim prp As DAO.Property Dim fld As DAO.Field

Const cTab = "

"

Set db = CurrentDb Set tdf = db(strTableName)

For Each idx In tdf.Indexes Debug.Print idx.Name & vbCrLf & "--------" For Each prp In idx.Properties Debug.Print prp.Name & " : " & prp.Value Next Debug.Print "--------" For Each fld In idx.Fields Debug.Print cTab & fld.Name For Each prp In fld.Properties Debug.Print cTab & prp.Name & " : " & prp.Value

Next Debug.Print cTab & "--------" Next Debug.Print " " Next

Set prp = Nothing Set idx = Nothing Set tdf = Nothing Set db = Nothing End Sub

Method 2
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? docmd.SetWarnings False If true, delete it;

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" Case dbByte FieldType = "dbByte" Case dbInteger FieldType = "dbInteger" Case dbLong FieldType = "dbLong" Case dbCurrency FieldType = "dbCurrency" Case dbSingle FieldType = "dbSingle" Case dbDouble FieldType = "dbDouble" Case dbDate FieldType = "dbDate" Case dbBinary FieldType = "dbBinary" Case dbText FieldType = "dbText" Case dbLongBinary FieldType = "dbLongBinary" Case dbMemo FieldType = "dbMemo" Case dbGUID FieldType = "dbGUID" End Select End Function

'1 '2 '3 '4 '5 '6 '7 '8 '9 '10 '11 '12 '15

Ref: http://www.access-programmers.co.uk/forums/showthread.php?t=99194

Visual Basic for Applications (VBA) functions break in a database with missing references
View products that this article applies to. This article was previously published under Q283806 Moderate: Requires basic macro, coding, and interoperability skills. This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp). For a Microsoft Access 2000 version of this article, see 208218 (http://support.microsoft.com/kb/208218/EN-US/ ) .

For a Microsoft Access 97 version of this article, see 160870 (http://support.microsoft.com/kb/160870/ ) .

On This Page

SYMPTOMS o Error message 1 o Error message 2 CAUSE RESOLUTION MORE INFORMATION o Steps to Reproduce the Behavior REFERENCES

Expand all | Collapse all

SYMPTOMS If you have a procedure that contains a Visual Basic for Applications function...
If you have a procedure that contains a Visual Basic for Applications function and your database contains a reference to a missing object library or type library, you may receive one of the following error messages when you compile your modules or run the procedure: Back to the top Error message 1 Your Microsoft Access database or project contains a missing or broken reference to the file <filename>. * To ensure that your database or project works properly, you must fix this reference. * To learn how to fix this reference, click Help. Back to the top Error message 2 Compile Error: Can't find project or library Back to the top

CAUSE Your database contains a reference to a database, type library, or object libra...
Your database contains a reference to a database, type library, or object library that is marked as MISSING: <referencename> in the References dialog box. Back to the top

RESOLUTION To remove the missing reference, follow these steps: Open your database.Press A...
To remove the missing reference, follow these steps: 1. 2. 3. 4. Open your database. Press ALT+F11 to open the Visual Basic Editor. On the Tools menu, click References. Click to clear the check box for the type library or object library marked as MISSING: <referencename>.

An alternative to removing the reference is to restore the referenced file to the path that is specified in the References dialog box. If the referenced file is in a new location, clear the MISSING: <referencename> reference, and then create a new reference to the file in the new folder. NOTE: In an Access run-time application, you cannot view references from a menu. However, the following article demonstrates how to view references using code: 209849 (http://support.microsoft.com/kb/209849/ ) How to loop through references to view their properties Back to the top

MORE INFORMATION Steps to Reproduce the Behavior Open the sample database Northwind.mdb. Create a...
Steps to Reproduce the Behavior 1. 2. 3. 4. Open the sample database Northwind.mdb. Create a new form that is not based on any table or query. On the Insert menu, click ActiveX Control. In the Select an Activex control list, click Kodak Image Edit Control, and then click OK. 5. Save the form as frmReference, and then close it.

6. Close Northwind.mdb, and then quit Access. 7. Find and rename the ImgEdit.ocx file to ImgEdit.old. 8. Start Access, and then open Northwind.mdb. 9. Open the Startup module in Design view. 10.On the Debug menu, click Compile Northwind. Note that you receive both of the error messages that are mentioned in the "Symptoms" section of this article. 11.Click OK. Note that the References dialog box appears; the following reference is highlighted in the Available References dialog box:
12. MISSING: Kodak Image Edit Control

13.Click Cancel in the Available References dialog box. 14.Find and rename the ImgEdit.old file to ImgEdit.ocx. 15.Repeat step 10 and note that the error message no longer appears. Back to the top

REFERENCES For additional information about another problem that could cause the first err...
For additional information about another problem that could cause the first error message, click the following article number to view the article in the Microsoft Knowledge Base: 275110 (http://support.microsoft.com/kb/275110/ ) "Undefined function in expression" error message when you open a query that references a function Back to the top

List files in a folder with Microsoft Scripting Runtime using VBA in Microsoft Excel
Microsoft Scripting Runtime is included in these products: Windows98, Windows2000, IE5, and Office2000. The macro examples below assumes that your VBA project has added a reference to the Microsoft Scripting Runtime library. You can do this from within the VBE by selecting the menu Tools, References and selecting Microsoft Scripting Runtime.
Sub TestListFilesInFolder() Workbooks.Add ' create a new workbook for the file list ' add headers With Range("A1") .Formula = "Folder contents:" .Font.Bold = True .Font.Size = 12

End With Range("A3").Formula = "File Name:" Range("B3").Formula = "File Size:" Range("C3").Formula = "File Type:" Range("D3").Formula = "Date Created:" Range("E3").Formula = "Date Last Accessed:" Range("F3").Formula = "Date Last Modified:" Range("G3").Formula = "Attributes:" Range("H3").Formula = "Short File Name:" Range("A3:H3").Font.Bold = True ListFilesInFolder "C:\FolderName\", True ' list all files included subfolders End Sub Sub ListFilesInFolder(SourceFolderName As String, IncludeSubfolders As Boolean) ' lists information about the files in SourceFolder ' example: ListFilesInFolder "C:\FolderName\", True Dim FSO As Scripting.FileSystemObject Dim SourceFolder As Scripting.Folder, SubFolder As Scripting.Folder Dim FileItem As Scripting.File Dim r As Long Set FSO = New Scripting.FileSystemObject Set SourceFolder = FSO.GetFolder(SourceFolderName) r = Range("A65536").End(xlUp).Row + 1 For Each FileItem In SourceFolder.Files ' display file properties Cells(r, 1).Formula = FileItem.Path Cells(r, 2).Formula = FileItem.Name Cells(r, 3).Formula = FileItem.Size Cells(r, 4).Formula = FileItem.Type Cells(r, 5).Formula = FileItem.DateCreated Cells(r, 6).Formula = FileItem.DateLastAccessed Cells(r, 7).Formula = FileItem.DateLastModified Cells(r, 8).Formula = FileItem.Attributes Cells(r, 9).Formula = FileItem.ShortPath & FileItem.ShortName ' use file methods (not proper in this example) ' FileItem.Copy "C:\FolderName\Filename.txt", True ' FileItem.Move "C:\FolderName\Filename.txt" ' FileItem.Delete True r = r + 1 ' next row number Next FileItem If IncludeSubfolders Then For Each SubFolder In SourceFolder.SubFolders ListFilesInFolder SubFolder.Path, True Next SubFolder End If Columns("A:H").AutoFit Set FileItem = Nothing Set SourceFolder = Nothing Set FSO = Nothing ActiveWorkbook.Saved = True End Sub

Ref: http://www.exceltip.com/st/List_files_in_a_folder_with_Microsoft_Scripting_Runtime_ using_VBA_in_Microsoft_Excel/446.html

You might also like