工程-> 引用 Microsoft Activex Data Objects 2.5 Library
module1.mod Public Function getRec() As ADODB.Recordset On Error GoTo err Dim conn As ADODB.Connection Dim rs As ADODB.Recordset Set conn = New ADODB.Connection With conn .Provider = "sqloledb.1" .ConnectionString = "integrated security = sspi;" & _ "data source = 'yd04'; inital catalog = 'pubs';" & _ "user id = 'sa' ; password = ''" .Open End With Set rs = New ADODB.Recordset With rs .ActiveConnection = conn .Source = "select * from jobs;" .CursorType = adOpenKeyset .LockType = adLockOptimistic .Open End With Set getRec = rs Exit Function err: Set getRec = Nothing MsgBox "´connection error" End Function
form1 Dim dbrs As ADODB.Recordest Private Sub Command1_Click() Set dbrs = getRec() Text1.Text = dbrs.Fields(0).Value test2.Text = dbrs.Fields(1).Value test3.test = dbrs.Fields(3).Value