不带字段名称的 INSERT INTO
INSERT INTO without field names
我正在尝试使用 INSERT INTO 语句而不定义我的列字段名称。我知道这是可能的,但我做不到。我目前拥有的 SQL 字符串是,
strSQL = " INSERT INTO MLE_Table (pnr, [Overall Assesment], risk, reason, justification)" & _
" SELECT tbl_Import.pnr, tbl_Import.[Overall Assesment], tbl_Import.risk, tbl_Import.reason, tbl_Import.justification " & _
" FROM tbl_Import;"
现在,在此代码中我使用了我的字段名称。但我不想那样做。
我希望 SQL 仅在两个 table 的字段名称匹配时将字段插入新的 table。
我认为可以通过 For Each 循环来完成,但我不确定。
有没有人以前做过这个..我使用的是 MS Access 2010.
非常感谢!!
您可以使用 DAO:
Public Sub CopyRecords()
Dim rstSource As DAO.Recordset
Dim rstInsert As DAO.Recordset
Dim fld As DAO.Field
Dim strSQL As String
Dim lngLoop As Long
Dim lngCount As Long
strSQL = "SELECT Top 1 * FROM tblInsert"
Set rstInsert = CurrentDb.OpenRecordset(strSQL)
strSQL = "SELECT * FROM tblSource"
Set rstSource = CurrentDb.OpenRecordset(strSQL)
With rstSource
lngCount = .RecordCount
For lngLoop = 1 To lngCount
With rstInsert
.AddNew
For Each fld In rstSource.Fields
With fld
If .Attributes And dbAutoIncrField Then
' Skip Autonumber or GUID field.
ElseIf .Name = "Something" Then
' Insert default value.
rstInsert.Fields(.Name).Value = 0
ElseIf .Name = "SomethingElse Then
' Ignore this field.
Else
' Copy field content.
rstInsert.Fields(.Name).Value = .Value
End If
End With
Next
.Update
End With
.MoveNext
Next
rstInsert.Close
.Close
End With
Set rstInsert = Nothing
Set rstSource = Nothing
End Sub
我正在尝试使用 INSERT INTO 语句而不定义我的列字段名称。我知道这是可能的,但我做不到。我目前拥有的 SQL 字符串是,
strSQL = " INSERT INTO MLE_Table (pnr, [Overall Assesment], risk, reason, justification)" & _
" SELECT tbl_Import.pnr, tbl_Import.[Overall Assesment], tbl_Import.risk, tbl_Import.reason, tbl_Import.justification " & _
" FROM tbl_Import;"
现在,在此代码中我使用了我的字段名称。但我不想那样做。 我希望 SQL 仅在两个 table 的字段名称匹配时将字段插入新的 table。
我认为可以通过 For Each 循环来完成,但我不确定。
有没有人以前做过这个..我使用的是 MS Access 2010.
非常感谢!!
您可以使用 DAO:
Public Sub CopyRecords()
Dim rstSource As DAO.Recordset
Dim rstInsert As DAO.Recordset
Dim fld As DAO.Field
Dim strSQL As String
Dim lngLoop As Long
Dim lngCount As Long
strSQL = "SELECT Top 1 * FROM tblInsert"
Set rstInsert = CurrentDb.OpenRecordset(strSQL)
strSQL = "SELECT * FROM tblSource"
Set rstSource = CurrentDb.OpenRecordset(strSQL)
With rstSource
lngCount = .RecordCount
For lngLoop = 1 To lngCount
With rstInsert
.AddNew
For Each fld In rstSource.Fields
With fld
If .Attributes And dbAutoIncrField Then
' Skip Autonumber or GUID field.
ElseIf .Name = "Something" Then
' Insert default value.
rstInsert.Fields(.Name).Value = 0
ElseIf .Name = "SomethingElse Then
' Ignore this field.
Else
' Copy field content.
rstInsert.Fields(.Name).Value = .Value
End If
End With
Next
.Update
End With
.MoveNext
Next
rstInsert.Close
.Close
End With
Set rstInsert = Nothing
Set rstSource = Nothing
End Sub