2

我正在尝试在 excel 中使用 VBA 连接到数据库,但不知道出了什么问题。有什么建议么?

这是代码。我想获取查询结果,然后同时放入 Excel 工作表

Private Sub CommandButton1_Click()

Dim dd As String
Dim nn As String

OMCS_Connection.Show

If OMCS_Connection.CommandButton1 = False Then

dd = OMCS_Connection.TextBoxa.Value
nn = OMCS_Connection.TextBoxb.Value
End If

Dim order_number As String
order_number = TextBox1.Text
Dim sql_query As String

Set ad = CreateObject("ADODB.Connection")      'create and open ODBC connection
ad.ConnectionString = "ODBC;DRIVER=IBM DB2 ODBC DRIVER - DB2COPY1; DSN=ODRPTDB;UID = " & dd & " ; PWD = " & nn & ";"
'UID=me"
ad.Open

sql_query = "SELECT" & _
                "CMOT.MFGNO AS MANUFACTURING_NUMBER," & _
                "'BOX' AS ORDER_TYPE," & _
                "CMOT.IDCUS AS CUSTOMER_ID," & _
                "CMOT.WTCTN AS WT_CUSTOMER_ID," & _
                "CMOT.PLORN AS ORDER_NUMBER," & _
                "CMOT.IDMAC AS MACHINE_TYPE," & _
                "CMOT.IDSMN AS MODEL_NUMBER," & _
                "'    ' AS NEW_MACHINE_TYPE," & _
                "'    ' AS NEW_MODEL_NUMBER," & _
                "CMOT.BOXSH AS PSSD," & _
                "CMOT.ALDPF AS RSSD," & _
                "COGUS.ITPDC AS REGION" & _
                "FROM DB2COATG.COS1S0T0 CMOT" & _
                "INNER JOIN COGUS.WTCTN_CONVERSION COGUS" & _
                "ON(COGUS.WTCTN = CMOT.WTCTN AND COGUS.REGIO = CMOT.REGIO)" & _
                "WHERE CMOT.PLORN IN  (" & order_number & ");"


Set RS = ad.Execute(sql_query)        'execute query
4

1 回答 1

0

在 ad.open 之前尝试插入以下行 ad.CommandTimeout=0 这应该可以防止查询在长时间运行时超时。如果这不起作用,这将有助于了解哪行代码引发了错误。你知道如何一步一步思考代码吗?

于 2012-12-29T02:11:09.377 回答