【问题标题】:Appending the data into the access table based on number of record in the table根据表中的记录数将数据附加到访问表中
【发布时间】:2014-09-04 16:54:03
【问题描述】:

我是新的 Access VBA, 我的场景是我想从另一个表中选择表名

(In same access db (table name as parameter)). 

根据从参数中选择的表名,我需要从同一个表(参数)中选择 le 列。然后我想从参数中检查不同的 le。 基于参数表中的不同 le (4 in my case) 我想将记录附加到另一个表 (ULAE). 追加语句将是 insert into 与 where 条件 as LE from parameter table

Public Sub LinkTables()

Dim rs1 As DAO.Recordset
Dim rs2 As DAO.Recordset
Dim stringsql As String
Set rs1 = CurrentDb.OpenRecordset("Table Valued Parameter")


While Not rs1.EOF = True

MsgBox rs1![Table_name]
tblname = rs1!Table_name
stringsql = ("SELECT COUNT(LE) from [Table Valued Parameter]")

MsgBox tblname
DoCmd.SetWarnings False
DoCmd.RunSQL ("SELECT COUNT(LE) FROM [Table Valued Parameter]")
'Next i
DoCmd.SetWarnings True


NEW_TBLNAME = "t_00_unearned_unincepted_alloc_basis_table"
'MsgBox NEW_TBLNAME
'CurrentDb.TableDefs.Append New_tblname

'MsgBox rs1![le]
'Set TD = Nothing

'Dim stringsql As String
DoCmd.SetWarnings False

'stringsql = ("SELECT COUNT(LE) from " & tblname)
'DoCmd.RunSQL (stringsql,False)lename&

cnt = CurrentDb.TableDefs.Count - 1
MsgBox cnt
        For i = 0 To cnt
        lename = rs1![le]
MsgBox lename
DoCmd.RunSQL ("INSERT INTO " & NEW_TBLNAME & " SELECT * FROM " & tblname & " where le = '" & lename& "')
Next i
DoCmd.SetWarnings True


rs1.MoveNext

Wend

代码给出错误 2342 。 CMDSQL需要sql语句。

有人可以建议如何实现它

感谢您的帮助

【问题讨论】:

  • 您的问题不清楚。你能改善你的问题吗?您是否尝试根据另一个表中的参数将记录导出到新表?

标签: sql ms-access vba ms-access-2010


【解决方案1】:

这是通过创建一个函数来完成的。

Function LinkTables()

Dim rs1 As DAO.Recordset
Dim rs2 As DAO.Recordset
Dim stringsql As String
Set rs1 = CurrentDb.OpenRecordset("Table Valued Parameter")

While Not rs1.EOF = True

'MsgBox rs1![Table_name]
'MsgBox rs1![le]

tblname = rs1!Table_name

legal_entity = rs1.Fields(1)

NEW_TBLNAME = rs1![destination_table]

DoCmd.SetWarnings False

DoCmd.RunSQL ("INSERT INTO " & NEW_TBLNAME & " SELECT * FROM " & tblname & " where le = '" & legal_entity & "'")

DoCmd.SetWarnings True

rs1.MoveNext

Wend

MsgBox "Tables Refreshed"


End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-15
    • 2016-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多