1、先建立excel与access数据库的odbc连接
2、每次表格 *** 作完成后触发一个动作更新access数据库
3、考虑到每次 *** 作都更新access数据会严重影响机器性能,建议一次性更新完Excel表格后,运行一个vba批量更新access数据库
上薇琪表格助手,免费帮你编写vba
rsopen"select from news,article where newsid="&取值&" and articleid="&取值&"“,conn,3,3
if rseof then
responsewrite"没有找到NEWSID和ARTICLEid相同的数据"
else
rs("newscontent")=CONTENT的取值
rs("articlecontent")=CONTENT的取值
rsupdate
不记得这样行不行,你可以试试。
connexecute("update news,article set newscontent="&content的取值&" and articlecontent="&content的取值&" newsid="&取值&" and articleid="&取值&"")
这样写肯定不行了!
复杂点的方法就是用代码打开所需要的数据库,然后读取打开数据库的表
简单点的方法就是创建表链接。以下以2010版为例:
1、外部数据→access→通过创建链接表来链接到数据源,然后选择路径、表……
这样,系统会创建一个与外部数据库中该表同名的链接表,若本地数据有这个名字,系统自动将链接表名+1
2、将当链接表是本地表一样建立更新查询,查询结果可以更新外部数据。当然查询时也可以引用外部数据。一般数据库的前台后台用的就是链接表功能。
是,用链接表可以实现。
下面是代码的动态实现表链接。
'首先 , 作如下测试工作:
'新建空白 Access 数据库 (mdb) 文件,
'并添加若干类型链接表:
'dBase 5 (dbf)、
'Microsoft Excel (xls)、
'Microsoft Access (mdb;mda;mde) 等,
'然后再编写如下 VB6 程序:
'Delphi、VC 等程序同理也可引用 ADOX
'引用 Microsoft ActiveX Data Objects 2x Library
'引用 Microsoft ADO Ext 2x for DDL and Security
'控件: Form1、Command1、Command2、Command3
Private Sub Command1_Click() '测试链接表信息
Dim adoConnection As New ADODBConnection
adoConnectionOpen "Provider=MicrosoftJetOLEDB40;Data Source=E:\LnkTblsmdb;Persist Security Info=False;Jet OLEDB:Database Password=123"
Dim adoCatalog As New ADOXCatalog
Set adoCatalogActiveConnection = adoConnection
Dim adoTable As New ADOXTable
Set adoTableParentCatalog = adoCatalog
Dim i As Integer
For Each adoTable In adoCatalogTables
If adoTableType = "LINK" Then
DebugPrint adoTableName
For i = 0 To adoTablePropertiesCount - 1
DebugPrint " " & adoTablePropertiesItem(i)Name & ": " & adoTablePropertiesItem(i)Value
Next i
DebugPrint VBAvbCrLf
End If
Next adoTable
End Sub
'编程添加链接表
Private Sub Command2_Click()
Dim adoConnection As New ADODBConnection
adoConnectionOpen "Provider=MicrosoftJetOLEDB40;Data Source=E:\LnkTblsmdb;Persist Security Info=False;Jet OLEDB:Database Password=123"
Dim adoCatalog As New ADOXCatalog
Dim adoTable As New ADOXTable
'Access
Set adoCatalogActiveConnection = adoConnection
Set adoTableParentCatalog = adoCatalog
adoTablePropertiesItem("Jet OLEDB:Link Datasource")Value = "e:\nwind2kpwdmdb"
adoTablePropertiesItem("Jet OLEDB:Remote Table Name")Value = "产品"
adoTablePropertiesItem("Jet OLEDB:Create Link")Value = True
adoTablePropertiesItem("Jet OLEDB:Link Provider String")Value = "MS Access;Pwd=456"
adoTableName = "Access"
adoCatalogTablesAppend adoTable
adoConnectionClose
'dBase
adoConnectionOpen
Set adoCatalogActiveConnection = adoConnection
Set adoTableParentCatalog = adoCatalog
adoTablePropertiesItem("Jet OLEDB:Link Datasource")Value = "E:\Borland\Shared\Data"
adoTablePropertiesItem("Jet OLEDB:Remote Table Name")Value = "animals#dbf"
adoTablePropertiesItem("Jet OLEDB:Create Link")Value = True
adoTablePropertiesItem("Jet OLEDB:Link Provider String")Value = "dBase 50"
adoTableName = "dBase5"
adoCatalogTablesAppend adoTable
adoConnectionClose
'Excel
adoConnectionOpen
Set adoCatalogActiveConnection = adoConnection
Set adoTableParentCatalog = adoCatalog
adoTablePropertiesItem("Jet OLEDB:Link Datasource")Value = "E:\Book97xls"
adoTablePropertiesItem("Jet OLEDB:Remote Table Name")Value = "Sheet1$"
adoTablePropertiesItem("Jet OLEDB:Create Link")Value = True
adoTablePropertiesItem("Jet OLEDB:Link Provider String")Value = "Excel 50;HDR=NO;IMEX=2"
adoTableName = "Excel"
adoCatalogTablesAppend adoTable
adoConnectionClose
'
End Sub
'编程删除链接表
Private Sub Command3_Click()
Dim adoConnection As New ADODBConnection
adoConnectionOpen "Provider=MicrosoftJetOLEDB40;Data Source=E:\LnkTblsmdb;Persist Security Info=False;Jet OLEDB:Database Password=123"
Dim adoCatalog As New ADOXCatalog
Set adoCatalogActiveConnection = adoConnection
Dim j As Integer
Dim i As Integer
For i = adoCatalogTablesCount To 1 Step -1
If adoCatalogTablesItem(i - 1)Type = "LINK" Then
DebugPrint adoCatalogTablesItem(i - 1)Name
For j = 0 To adoCatalogTablesItem(i - 1)PropertiesCount - 1
DebugPrint " " & adoCatalogTablesItem(i - 1)PropertiesItem(j)Name & ": " & adoCatalogTablesItem(i - 1)PropertiesItem(j)Value
Next j
DebugPrint VBAvbCrLf
If VBAMsgBox("Delete link table [" & adoCatalogTablesItem(i - 1)Name & "]", vbYesNo) Then
adoCatalogTablesDelete adoCatalogTablesItem(i - 1)Name
End If
End If
Next i
End Sub
Private Sub Command4_Click()
Dim adoConnection As New ADODBConnection
adoConnectionOpen "Provider=MicrosoftJetOLEDB40;Data Source=E:\LnkTblsmdb;Persist Security Info=False;Jet OLEDB:Database Password=123"
Dim adoCatalog As New ADOXCatalog
Set adoCatalogActiveConnection = adoConnection
adoCatalogTablesItem("Excel")PropertiesItem("Jet OLEDB:Link Provider String")Value = "Excel 50;HDR=yes;IMEX=2"
End Sub
Private Sub Form_Load()
Command1Caption = "链接表信息"
Command2Caption = "添加链接表"
Command3Caption = "删除链接表"
Command4Caption = "刷新链接表"
End Sub
你好,就是对数据作一些限制, 只能输入正确规范的数据 比如有两个表 一个存了学生的信息,第二个存了学生考试的成绩 如果做了关联,则可以让第二个表的学生姓名不可以随便插入,一定要在第一个表里名字有的才行。
贴出的更新语句有点小毛病,请更改如下:
strSQ = "update A ,B set A关机记录=B关机记录,A关机=B关机 where A序号 = B序号;"
更新多个字段,请遵循下面写法:
update 目标表,源表 where 目标表字段1=源表源字段1,目标表字段2=源表源字段2,
目标表字段n=源表源字段n where 连接和筛选条件;
以上就是关于我想对excel表 *** 作,同时连接access,且数据随excel *** 作进行添加更新,有什么简单的办法吗全部的内容,包括:我想对excel表 *** 作,同时连接access,且数据随excel *** 作进行添加更新,有什么简单的办法吗、对ACCESS数据库里的两张表进行同步更新、Access 中的 Update 怎么跨数据库更新等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)