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