我试图在ms访问的所有表中添加4列(我将导入),然后根据我的选择查询结果更新它们。但是我的更新查询无效。任何人都可以看看,让我知道解决方案
谢谢
Dim db As DAO.Database
Dim objtbl As DAO.TableDef
Dim sqlstr As String
Dim rs As DAO.Recordset
Dim sq2 As DAO.Recordset
Dim sq3 As DAO.Recordset
Dim sq4 As DAO.Recordset
Dim sq5 As DAO.Recordset
Set db = CurrentDb
On Error Resume Next
For Each objtbl In db.TableDefs
db.Execute "ALTER TABLE " & "[" & objtbl.Name & "]" & " ADD COLUMN [Account] Text;"
db.Execute "ALTER TABLE " & "[" & objtbl.Name & "]" & " ADD COLUMN [Account_Serial] Text;"
db.Execute "ALTER TABLE " & "[" & objtbl.Name & "]" & " ADD COLUMN [Invoice_Date] Text;"
db.Execute "ALTER TABLE " & "[" & objtbl.Name & "]" & "ADD Column [LOB] Text;"
db.Execute "ALTER TABLE " & "[" & objtbl.Name & "]" & "ADD Column [LOB] Text;"
q = "Select [Field3] from " & "[" & objtbl.Name & "]" & " Where " & "[" & objtbl.Name & "].[Field1] = ""CUSTOMERRECORD"";"
Set rs = CurrentDb.OpenRecordset("Select [Field3] from " & "[" & objtbl.Name & "]" & " Where " & "[" & objtbl.Name & "].[Field1] = ""CUSTOMERRECORD"";")
Set sq2 = CurrentDb.OpenRecordset("Select Field4 from " & "[" & objtbl.Name & "]" & "Where" & "[" & objtbl.Name & "].[Field1] = ""CUSTOMERRECORD"";")
Set sq3 = CurrentDb.OpenRecordset("Select Field5 from" & "[" & objtbl.Name & "]" & "Where" & "[" & objtbl.Name & "].[Field1] = ""Customerrecord"";")
Set sq4 = CurrentDb.OpenRecordset("Select Field6 from" & "[" & objtbl.Name & "]" & "Where" & "[" & objtbl.Name & "].[Field1] = ""Customerrecord"";")
db.Execute
db.Execute "Update " & "[" & objtbl.Name & "] SET " & "[" & objtbl.Name & "].[Account] = " & rs!Field3 & ";"
Next
答案 0 :(得分:0)
除非您刷新数据库窗口,否则访问可能不会立即反映您对表所做的更改。
尝试在更改表格之后和打开记录集之前添加命令 Application.RefreshDatabaseWindow 。
答案 1 :(得分:0)
请从您的代码中删除On Error Resume Next
。然后修复您将获得的所有错误。例如。
& "ADD Column [LOB] Text;"
("Select Field5 from" & "[" &
db.Execute
没有参数定义变量sTable = "[" & objtbl.Name & "]"
并在任何地方使用它将使您的代码更易于阅读。
如果Field3是文本字段,则可能需要
" .[Account] = '" & rs!Field3 & "';"