在Access VBA中,查找字段名称,将列插入表中

时间:2016-02-26 16:14:28

标签: sql vba ms-access access-vba

我正在尝试创建一个新表,其中源文件中的字段名称是变量。例如,源数据中的ID字段可以是" ID"或者" HB_REF_NO",出生日期可能是DoBDate of BirthDate_of_birth

我已经创建了一个代码,用于搜索各种字段名称并返回它们所在的列但是很难将这些列中的数据传输到表中

这是代码,请原谅其粗俗的方法论......

Private Sub cmdCompare_Click()

Set db = CurrentDb()
Set RecordSet1 = db.OpenRecordset("OriginalData")
Dim Fld As DAO.Field
Dim FldArray() As String
Dim i As Integer
Dim j As Integer
Dim SQLCreate As String
Dim SQLInsert As String
Dim s As Integer
Dim d As Integer
Dim b As Integer
Dim p As Integer

j = RecordSet1.Fields.Count - 1
ReDim FldArray(j)

'Assigns field names to the array
For Each Fld In RecordSet1.Fields
    FldArray(i) = Fld.Name
    i = i + 1
Next

For i = 0 To j
    If FldArray(i) = "Surname" Then
        s = i
    Else
    End If
Next

For i = 0 To j
    If FldArray(i) = "HB_REF_NO" Then
        d = i
    Else
    End If
Next

For i = 0 To j
    If FldArray(i) = "NC_DATE_OF_BIRTH" Then
        b = i
    Else
    End If
Next

For i = 0 To j
    If FldArray(i) = "POSTCODE" Then
        p = i
    End If
Next


SQLCreate = "CREATE TABLE OriginalComp" & _
        "(ID varchar(255), Surname varchar(255), DoB varchar(255), Postcode varchar(255))"

DoCmd.RunSQL SQLCreate

SQLInsert = "INSERT INTO OriginalComp (ID, Surname, DoB, Postcode) " & _
      "VALUES ('" & FldArray(d) & "','" & FldArray(s) & "','" & FldArray(b) & "','" & FldArray(p) & "');"

DoCmd.RunSQL SQLInsert

End Sub

2 个答案:

答案 0 :(得分:2)

检查源表的TableDef.Fields集合并查找每个变量字段名称的当前名称会更简单。您不需要以Recordset打开表格,也不需要使用数组。

此示例将查找并使用名称与*Dob**Date*Birth*匹配的出生日期字段。 (如果表中的多个字段可以匹配这些模式,则您需要替换更具选择性的模式。)

Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim strDateOfBirth As String
Dim strInsert As String

Set db = CurrentDb
Set tdf = db.TableDefs("OriginalData")
For Each fld In tdf.Fields
    With fld
        If .Name Like "*DoB*" Or .Name Like "*Date*birth*" Then
            strDateOfBirth = .Name
        End If
    End With
Next

If Len(strDateOfBirth) > 0 Then
    strInsert = "INSERT INTO OriginalComp (ID, Surname, DoB, Postcode) " & _
        "SELECT HB_REF_NO, Surname, [" & strDateOfBirth & "], POSTCODE " & _
        "FROM OriginalData;"
    Debug.Print strInsert '<- inspect this in Immediate window; Ctrl+g will take you there
    db.Execute strInsert, dbFailOnError
Else
    MsgBox "Birth date field not found!"
End If

扩展该示例以处理HB_REF_NOID字段。

答案 1 :(得分:1)

问题在于:

SQLInsert = "INSERT INTO OriginalComp (ID, Surname, DoB, Postcode) " & _
      "VALUES ('" & FldArray(d) & "','" & FldArray(s) & "','" & FldArray(b) & "','" & FldArray(p) & "');"

FldArray变量是所有列名称的列表,因此dth条目将是dth列名称,这就是您插入列名而不是值的原因!

如果您想将这些值插入到OriginalComp中,那么为什么不这样做:

Private Sub cmdCompare_Click()

Set db = CurrentDb()

SQLCreate = "CREATE TABLE OriginalComp" & _
            "(ID varchar(255), Surname varchar(255), DoB varchar(255), Postcode varchar(255))"

DoCmd.RunSQL SQLCreate

SQLInsert = "INSERT INTO OriginalComp (ID, Surname, DoB, Postcode) " & _
            "SELECT HB_REF_NO, Surname, NC_DATE_OF_BIRTH, POSTCODE " & _
            "FROM OriginalData"

DoCmd.RunSQL SQLInsert

End Sub