如何使用Access 2007
中的vba刷新表格列表。
我使用Sql
:
vba
代码中创建了表格
DoCmd.RunSQL "SELECT * INTO table2 FROM table1", True
RefreshDatabaseWindow
DoCmd.TransferDatabase acExport, "Microsoft Access", "C:\import.mdb", , "table2", tab2, 0
错误消息:Microsoft Office Access找不到对象table2
如果暂停RefreshDatabaseWindow
并手动刷新,请按F5
并重新运行以正常工作。怎么自动完成?如果表列表处于活动状态,则Access 2003可正常工作。
我尝试暂停但不工作:
Private Declare Sub sApiSleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
...
RefreshDatabaseWindow
Call sApiSleep(4000)
...
答案 0 :(得分:1)
这是我用来附加SQL /服务器表的方法。我删除它们然后用这段代码重新链接它们。显然,你必须为每个表重复最后一节。我不知道这是否适用于ADP。我的环境不使用ADP。
Dim vConnect As String
Dim td As New TableDef
Dim vtablename As String
vConnect = "ODBC;Driver={SQL Server Native Client 10.0};SERVER=<server>;DATABASE=" <database>; Uid=<user>; Pwd=<pwd>;TrustServerCertificate=yes;encrypt = yes;"
'Delete existing tables
For Each td In CurrentDb.TableDefs
vattributes = td.Attributes And dbAttachedODBC
If Left(td.Name, 4) <> "Msys" And vattributes > 0 Then CurrentDb.TableDefs.Delete td.Name
Next
Debug.Print "Old Tables Deleted"
Set td = CurrentDb.CreateTableDef("<your table name>")
td.Connect = vConnect
td.SourceTableName = "<your table name>"
vtablename = td.Name
CurrentDb.TableDefs.Append td
Debug.Print "Attached table: " & vtablename
答案 1 :(得分:0)
适用于Access 2007, 2010
(项目.ADP
):
RefreshDatabaseWindow
DoCmd.SelectObject acTable, "table2", True
DoCmd.TransferDatabase ...
答案 2 :(得分:0)
这在Access 2010中运行良好
在表创建和TransferDataBase之间调用此函数
功能refrescar conexion BBDD()
Dim cadenaConexion As String
cadenaConexion = Application.CurrentProject.BaseConnectionString
Application.CurrentProject.CloseConnection
Application.CurrentProject.OpenConnection cadenaConexion
结束功能