为了庆祝我的第800个字节,我要发表我的第一篇文章。 我计划在“ Dev Tools”系列中发表一些文章; 这些文章包含供开发人员使用的工具,而不是简单地传递有关如何做某事的知识。 第一个工具是子例程“ GetColumnNames”。
Intellisense很不错,但并不总是可用,并且一次只能限制在一个字段中。 我经常发现自己对特定字段名称的拼写不太确定-是OrderQty还是QtyOrdered,CustPO或CustPONO,这些东西曾经使我发疯。 最后,不久前,我编写了一个例程,在即时窗口中列出了表的列名。 随着时间的推移,我将其扩展为根据需要以各种格式返回信息。
到目前为止,此例程将向以下格式之一显示立即窗口:
1.仅字段或列名
2. TableName.FieldName.FieldType
3. rs1!FieldName = X
4. X = rs1!FieldName
在格式3和4中,X为文字; 我将返回的代码粘贴到正在使用的模块中,并用适当的字段名称或值替换每个X。 当我必须在VBA中构建,复制或更新记录时,可以节省很多键入时间。 同样,在给定实例中,“ rs1”可能会也可能不必更改为我正在使用的任何rs对象名。
最后,“表”和“字段名”参数是可选的,表示要匹配的搜索字符串。
GetColumnNames(1,“ PREmployee”,“ W2”)将返回表“ PREmployee”中所有以“ W2”开头的列名。
GetColumnNames(2,“ PR”)将返回每个以“ PR”开头的表中的每个表名,字段名和字段类型
GetColumnNames(3,“ PREmployee”,“ YTD”)可能返回以下内容:
rs1!YTDWages = X
rs1!YTDFedWH = X
rs1!YTDNet = X ...以此类推,所有以YTD开头的列
我使用了Enmerations,以便用户不必记住1 = gcnSimple,2 = gcnComplex,3 = gcnCopyTo和4 = gcnCopyFrom。
足够的胡言乱语,加上代码。
Option Compare Database
Option Explicit
Public Enum gcnTypes
gcnSimple = 1
gcnComplex = 2
gcnCopyTo = 3
gcncopyfrom = 4
End Enum
'---------------------------------------------------------------------------------------
' Procedure : GetColumnNames
' Author : Jim
' Date : 4/28/2014
' Purpose : Returns Table Field names in optional formats to the immediate window
' Optional paramerters allow selective return of all tables and fields or wildcard matches
' Return formats are
' Field Name only (gcnSimple)
' TableName.FieldName.Type (gcnComplex)
' rs1!FieldName=x (gcnCopyTo)
' x=rs1!FieldName (gcnCopyFrom)
'---------------------------------------------------------------------------------------
'
Public Sub GetColumnNames(ReplyType As gcnTypes, Optional TableName_str As String, Optional FieldPrefix_str As String)
' reply types are 1=simple (field names only)
' 2=complex (table name, field name, field type)
' 3=Move to
' 4=Move from
'
' returns data in the immediate window
Dim tbl As DAO.TableDef
Dim fld As DAO.Field
Dim fldTypes(23) As String
Dim fldTyp As Integer
Dim fldDesc As String
On Error GoTo GetColumnNames_Error
fldTypes(1) = "Boolean"
fldTypes(2) = "Byte"
fldTypes(3) = "Integer"
fldTypes(4) = "Long"
fldTypes(5) = "Currency"
fldTypes(6) = "Single"
fldTypes(7) = "Double"
fldTypes(8) = "Date"
fldTypes(9) = "Binary"
fldTypes(10) = "Text"
fldTypes(11) = "Long Binary"
fldTypes(12) = "Memo"
fldTypes(13) = "Attachment" '101
fldTypes(14) = "Complex Byte" '102
fldTypes(15) = "Complex Integer"
fldTypes(16) = "Complex Long"
fldTypes(17) = "Complex Single"
fldTypes(18) = "Complex Double"
fldTypes(19) = "Complex GUID"
fldTypes(20) = "Complex Decimal"
fldTypes(21) = "Complex Text" ' 109
fldTypes(22) = "Other"
' Print the header.
On Error GoTo GetColumnNames_Error
' Loop through all the table definitions.
For Each tbl In CurrentDb.TableDefs
If Len(Nz(TableName_str)) = 0 Or (Left(tbl.Name, Len(TableName_str)) = TableName_str) Then
For Each fld In tbl.Fields
fldTyp = fld.Type
If fldTyp > 100 And fldTyp <= 109 Then
fldTyp = fldTyp - 88 ' 101 becomes 13
End If
If fldTyp > 0 And fldTyp <= 22 Then
fldDesc = fldTypes(fldTyp)
Else
fldDesc = fld.Type & " Other"
End If
'only include fields matching the name requested
If Len(Nz(FieldPrefix_str)) = 0 Or (Left(fld.Name, Len(FieldPrefix_str)) = FieldPrefix_str) Then
Select Case ReplyType
Case gcnSimple
Debug.Print fld.Name
Case gcnComplex
Debug.Print tbl.Name & "." & fld.Name & "." & fldDesc
Case gcnCopyTo
Debug.Print "rs1!" & fld.Name & "= x"
Case gcncopyfrom
Debug.Print "x=rs1!" & fld.Name
End Select
End If
Next fld
End If
Next tbl
On Error GoTo 0
Exit Sub
GetColumnNames_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure GetColumnNames of Module PublicCode_vb"
Resume Next
On Error GoTo 0
Exit Sub
End Sub
将代码粘贴到公共模块中,并在需要时从立即窗口中调用它。
随时将名称更改为GCN或其他短名称。
吉姆
From: https://bytes.com/topic/access/insights/956360-dev-tools-fill-immediate-window-table-columns