vba讀取資料庫數據
⑴ 用VBA如何獲取access資料庫中欄位的數據類型
1、打開相應的access資料庫。
2、在左邊的對象欄里單擊模塊,觀察右邊的窗口,如果裡面有模塊,可以雙擊打開。如果沒有新建一個模塊,然後雙擊打開。
3、粘貼以下代碼並運行、觀察立即窗口。
PrivateSubaaaa()
DimTableNameAsString,FieldNameAsString,iAsInteger
TableName="tb1"'表名tb1
FieldName="編號"'欄位名編號
SelectCaseCurrentDb.TableDefs(TableName)(FieldName).Type
CasedbBoolean
Debug.Print"是/否"
CasedbByte
Debug.Print"數字(位元組)"
CasedbInteger
Debug.Print"數字(整型)"
CasedbLong
If(CurrentDb.TableDefs(TableName)(FieldName).AttributesAnddbAutoIncrField)=dbAutoIncrFieldThen
Debug.Print"自動編號(長整型)"
Else
Debug.Print"數字(長整型)"
EndIf
CasedbSingle
Debug.Print"數字(單精度)"
CasedbDouble
Debug.Print"數字(雙精度)"
CasedbDecimal
Debug.Print"數字(小數)"
CasedbCurrency
Debug.Print"貨幣"
CasedbDate
Debug.Print"日期/時間"
CasedbText
Debug.Print"文本"
CasedbMemo
If(CurrentDb.TableDefs(TableName)(FieldName).AttributesAnddbHyperlinkField)=dbHyperlinkFieldThen
Debug.Print"超鏈接"
Else
Debug.Print"備注"
EndIf
CasedbGUID
Debug.Print"自動編號(自動復制ID)"
EndSelect
EndSub
⑵ 用VBA語言調用sql資料庫數據到excel中
VBE 工具 引用,選中 Microsoft ActiveX Data Object 2.8 Library
我不清楚是不是UFDATA_013_2008表的VENDOR列的數據,如果不是,你自己改一下SQL語句吧,sql = "select VENDOR from UFDATA_013_2008"
Sub SQL數據導入()
Dim cn As New ADODB.Connection
Dim rs As ADODB.Recordset
Dim str As String
Dim sql As String
Dim i As Integer
str = "Provider=SQLOLEDB;Data Source=SQLSERVER伺服器名或IP;DATABASE=資料庫;UID=用戶名;PWD=密碼"
cn.Open str
Set rs = New ADODB.Recordset
sql = "select VENDOR from UFDATA_013_2008"
rs.Open sql, cn, adOpenStatic, adLockOptimistic
If Not rs.EOF Then
For i = 1 To rs.RecordCount
Sheet2.Cells(i, 1).Value = rs.Fields(0).Value
Next i
rs.MoveNext
End If
rs.Close
cn.Close
End Sub
⑶ excel vba讀取access資料庫記錄空值出錯
使用Variant數據異常。
如果需要使用空值,請使用Variant數據類型,使用Nz()函數指定要用於Null的值。
在VBA中,唯一可以包含Null的數據類型是Variant。當欄位的值分配給非變數時,都必須考慮該欄位可能為空的可能性。