将myConn调暗为ADODB.Connection

时间:2015-11-25 20:38:36

标签: excel vba excel-vba ibm-midrange

我正在尝试一些事情,其中​​一个是将excel工作簿与AS400连接,以获取系统中部件号的描述,但尝试保持连接打开或至少在工作簿关闭之前。 有没有人有任何建议?

Public Sub GetPartNumbers()

    Dim myConn As ADODB.Connection
    Dim myRS As ADODB.Recordset
''    Dim selVal As String
''    Dim selRow As Integer
    Set myConn = New ADODB.Connection
    myConn.ConnectionString = "Provider=SEQUEL ViewPoint;"
    myConn.Open
    Set myRS = New ADODB.Recordset

2 个答案:

答案 0 :(得分:1)

我就是这样做的。

Dim myConn As New ADODB.Connection
Dim myRs As New ADODB.Recordset

On Error GoTo ErrorHandler

con.Open "PROVIDER=IBMDAS400;Data Source=999.999.999.999;USER ID= ;PASSWORD= ;"

Set myRs.ActiveConnection = myConn

End

Exit Sub

ErrorHandler:

MsgBox "Can not connect", vbInformation, cHeading
End

End Sub

答案 1 :(得分:0)

我可以同情你,因为在我对VBA了解很多之前我必须弄明白这一点。下面是一个简单的连接和循环结果示例。是的,你不应该使用Activate但是为了这个例子,我通过它。

请注意,您应该在最后清理连接。如果使用错误处理程序,请确保在退出之前关闭所有打开的连接,即使发生错误也是如此。

Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset

Set cnn = New ADODB.Connection
Set rst = New ADODB.Recordset

cnn.Open "PROVIDER=IBMDAS400;Data Source=999.999.999.999;USER ID= ;PASSWORD= ;"
rst.ActiveConnection = cnn
rst.CursorLocation = adUseServer

'Query String (Specific to your database setup)
rst.Source = "SELECT DISTINCT F3002.IXLITM " _
                & "FROM WYATT.PRDDTA.F3002 F3002 " _
                & "WHERE (F3002.IXKITL='30P') AND (F3002.IXTBM='E')"

rst.Open

Worksheets("Sheet1").Range("A1").Activate

Do Until rst.EOF
    ActiveCell.Value = rst.Fields("IXLITM")

    rst.MoveNext
    ActiveCell.Offset(1, 0).Activate
Loop

'Clean up
rst.Close
Set rst = Nothing
Set cnn = Nothing