You can use adodc control or data control if you link the database (sql server, access). To the sql server, first you should create an ODBC driver, then link it with the adodc control.
2) You can use programming method to link the database.
Option Explicit
Dim cn As ADODB.Connection
Dim cmd As ADODB.Command
Dim rs As ADODB.RecordsetPrivate Sub Command1_Click()
Set rs = GetRecordSet()
While Not (rs.EOF Or rs.BOF)
Debug.Print rs!emp_id
rs.MoveNext
Wend
Set rs = Nothing
End SubFunction GetRecordSet() As ADODB.Recordset
Dim AdoCn As ADODB.Connection
Dim AdoCmd As ADODB.Command
Dim sSQL As String
Dim sCn As String
On Error GoTo RecordSetErr
'sCn = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=pubs;Data Source=FUJITSU-DEMO"
sCn = "Provider=sqloledb.1;Data Source=FUJITSU-DEMO;Initial Catalog=pubs;User Id=sa; Password=;"
Set AdoCn = New ADODB.Connection
AdoCn.Open sCn
sSQL = "SELECT * INTO #TempTable FROM employee "
Set AdoCmd = New ADODB.Command
Set AdoCmd.ActiveConnection = AdoCn 'ADODB.Connection
AdoCmd.CommandText = sSQL
AdoCmd.CommandType = adCmdText '+ adExecuteNoRecord
AdoCmd.CommandTimeout = 15
AdoCmd.Execute
AdoCmd.CommandText = "#TempTable"
AdoCmd.CommandType = adCmdTable
AdoCmd.CommandTimeout = 15
Set GetRecordSet = AdoCmd.Execute()
Set AdoCn = Nothing
Set AdoCmd = Nothing
Exit Function
RecordSetErr:
MsgBox "Error Message!"End Function
2) You can use programming method to link the database.
Option Explicit
Dim cn As ADODB.Connection
Dim cmd As ADODB.Command
Dim rs As ADODB.RecordsetPrivate Sub Command1_Click()
Set rs = GetRecordSet()
While Not (rs.EOF Or rs.BOF)
Debug.Print rs!emp_id
rs.MoveNext
Wend
Set rs = Nothing
End SubFunction GetRecordSet() As ADODB.Recordset
Dim AdoCn As ADODB.Connection
Dim AdoCmd As ADODB.Command
Dim sSQL As String
Dim sCn As String
On Error GoTo RecordSetErr
'sCn = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=pubs;Data Source=FUJITSU-DEMO"
sCn = "Provider=sqloledb.1;Data Source=FUJITSU-DEMO;Initial Catalog=pubs;User Id=sa; Password=;"
Set AdoCn = New ADODB.Connection
AdoCn.Open sCn
sSQL = "SELECT * INTO #TempTable FROM employee "
Set AdoCmd = New ADODB.Command
Set AdoCmd.ActiveConnection = AdoCn 'ADODB.Connection
AdoCmd.CommandText = sSQL
AdoCmd.CommandType = adCmdText '+ adExecuteNoRecord
AdoCmd.CommandTimeout = 15
AdoCmd.Execute
AdoCmd.CommandText = "#TempTable"
AdoCmd.CommandType = adCmdTable
AdoCmd.CommandTimeout = 15
Set GetRecordSet = AdoCmd.Execute()
Set AdoCn = Nothing
Set AdoCmd = Nothing
Exit Function
RecordSetErr:
MsgBox "Error Message!"End Function
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货