我正在尝试在 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