A. EXCELvba写入ACCESS数据库表
第一步:连接数据库,下面是参考代码
Set conn = CreateObject("ADODB.Connection")
conn.ConnectionString = "Provider=Microsoft.Ace.OleDB.12.0;Data Source=" & mypath
conn.ConnectionString = conn.ConnectionString & ";Jet OLEDB:Database Password=1"
conn.Open
第二步:插入数据,参考代码如下:
sql = "insert into skmxb (内部管理号,收款日期,金额"
Sql = Sql & ") values ('" & nbglH & "', #" & Format(myD, "yyyy/mm/dd") & "#,"
Sql = Sql & jE
Sql = Sql & ")"
conn.Execute Sql
B. 用vb连接access数据库以后,如何一次性执行多条sql语句
使用ACCESS查询对象是无法一次运行多个
SQL语句
的,但是通过ACCESS
VBA代码可以实现一次运行多个SQL语句。下面是具体实现方案;
1)新建一个ACCESS窗体,窗体添加一个命令按钮;
2)编写按钮单击事件过程
Private
Sub
Command1_Click()Dim
strSql
as
stringstrSQL="Update
Policy
SET
LatestDueDate=Year(Date())
&
'-'
&
Format(PolicyDate,'mm-dd')"
DoCmd.RunSql
strSQL
'运行第一个更新语句strSQL="UPDATE
Policy
SET
LatestDueDate=
DateAdd('yyyy',1,LatestDueDate)
WHERE
(((Month(Date())-Month(LatestDueDate))
>
6)
and(PaymentMode='H'))"
DoCmd.RunSql
strSQL
'运行第二个更新语句
End
Sub
跟着点击该命令按钮就可以一次执行两条语句了。
C. 用VBA将EXCEL内容一次性导入SQL
用VBA将EXCEL内容一次性导入SQL
'工具->引用->Microsoft ActiveX Date Object 2.0
Public Sub SaveData()
Dim Cnn As ADODB.Connection
Dim SQL As String
Set Cnn = New ADODB.Connection
'建立于数据库的链接
'这里根据你的实际值修改ConnectionString = "Driver=SQL Server;Server=<a href="https://www..com/s?wd=%E6%9C%8D%E5%8A%A1%E5%99%A8%E5%90%8D%E7%A7%B0&tn=44039180_cpr&fenlei=-bIi4WUvYETgN-" target="_blank" class="-highlight">服务器名称</a>;Database=数据库;Uid=账号;Pwd=密码;"
With Cnn
.Provider = "SQLOLEDB"
.ConnectionString = "Driver=SQL Server;Server=mxb\sqlexpress;Database=test;Uid=sa;Pwd=xiaoma;"
.Open
End With
'保存数据
r = Range("A65534").End(xlUp).Row
For i = 1 To r
'拼sql
SQL = "insert into T values('" & Cells(i, 1) & "','" & Cells(i, 2) & "'," & Cells(i, 3) & ")"
Cnn.Execute SQL
Next
Cnn.Close
Set Cnn = Nothing
MsgBox "保存成功"
End Sub
上面是通过VBA,插入数据到数据库,下面是从SQL查询Excel,然后直接insert into到数据库,也可以用数据库导入向导
--查询excel2007
select * from OpenDataSource('Microsoft.ACE.OLEDB.12.0', 'Data Source=D:\2007.xlsx;Extended Properties="Excel 12.0;HDR=Yes;IMEX=1"')...[Sheet1$]
--查询excel2003
select * from OpenDataSource( 'Microsoft.Jet.OLEDB.4.0','Data Source="D:\2003.xls";Extended properties=Excel 5.0')...[Sheet1$]
D. 请问怎样用vba实现:内嵌SQL并从数据库查询到数据并放入指定的单元格内请高手赐教或给个例子~ 小妹谢过
你大概弄错了,数据库不存在什么最后空行,你指的多半是插入操作,如果是的话,执行sql语句(假定你的第三列字段名为name)
str="insert into wdgj21(name) values('" & Ar & "')"
(怎么执行不用偶说吧)当然如果你用addnew之类来替代上面这个也是可以的。
取出数据跟这个是差不多的,假定你的数据库中的name2列中包括这个ar的内容,你想取出那一行的name的值,
str="select top 1 name from wdgj21 where name2 like '%" & Ar & "%'"
用ADODB.Recordset打开就可以取到结果了。
E. vb怎么连接数据库写入数据
'在使用前需要先安装MySql的驱动,进行正确配置
'注意:必须给出正确的服务器名、数据库名、表名、数据库连接的用户名、密码
Option Explicit
Dim Cnn As ADODB.Connection '定义ADO连接对象
Dim Records As ADODB.Recordset '定义ADO记录集对象
'连接到数据库
Function CnnOpen(ByVal ServerName As String, ByVal DBName As String, ByVal TblName As String, ByVal User As String, ByVal PWD As String) '服务器名或IP、数据库名、登录用户、密码
Dim CnnStr As String '定义连接字符串
Set Cnn = CreateObject("ADODB.Connection") '创建ADO连接对象
Cnn.CommandTimeout = 15 '设置超时时间
CnnStr = "DRIVER={MySql ODBC 5.1 Driver};SERVER=" & ServerName & ";Database=" & DBName & ";Uid=" & User & ";Pwd=" & PWD & ";Stmt=set names GBK" '
Cnn.ConnectionString = CnnStr
Cnn.Open
End Function
'关闭连接
Function CnnClose()
If Cnn.State = 1 Then
Cnn.Close
End If
End Function
'把Excel写入MySql中的数据库
Function InsertToMySql(ByVal SheetName As String, ByVal TblName As String)
Dim SqlStr As String
Dim i, j As Integer
Dim Columns, Rows As Integer
Columns = VBAProject.func_public.GetTotalColumns(SheetName)
Rows = VBAProject.func_public.GetTotalRows(SheetName)
Set Records = CreateObject("ADODB.recordset")
'取得结果集并插入数据到数据库
Set Records = CreateObject("ADODB.Recordset")
'以下语句提供了插入思路,我只是把单条记录的插入方式改为循环,以把所有的记录添加到表中
'rs.Open "insert into newtable values('" & ActiveSheet.Cells(i, 1).Value & "'," & "'" & ActiveSheet.Cells(i, 2).Value & "')", cnn, 0
For i = 2 To Rows
SqlStr = "INSERT INTO " & TblName & " values('" & Sheets(SheetName).Cells(i, 1).Value & "'" '注意:" values('",字母“v”之前是有空格的!!!
For j = 2 To Columns
SqlStr = SqlStr & ",'" & Sheets(SheetName).Cells(i, j).Value & "'"
Next
SqlStr = SqlStr & ")"
Set Records = Cnn.Execute(SqlStr) 'rs.Open SqlStr, cnn, 0 不能用这条语句实现!!!
Next
MsgBox "Insert!", vbOKOnly, "Excel To MySql"
End Function
'清除对象
Function ClearObj()
Set Cnn = Nothing
Set Records = Nothing
End Function
F. 在excel中如何使用vba实现将sql的数据快速写入excel
DimconnAsNewADODB.Connection
connStr="Driver={SQLServer};DataBase=test;Server=(local);UID=sa;PWD=123"
conn.OpenconnStr'连接数据库
DimrsAsNewRecordset
sql="select*froma"'查看表a
rs.Opensql,conn,3,3
ifNotrs.EOFthen
ForiCols=0Tors.fields.Count-1
Sheets(1).Cells(1,iCols+1).Value=rs.fields(iCols).Name
NextiCols
Sheets(1).Cells(2,1).CopyFromRecordsetrs
endif