1
我有一個綁定到Excel電子表格中的列的下拉列表。我在下拉列表中選擇一個客戶,然後使用Excel電子表格中的相關數據填充一些地址字段。如何處理Word VBA SQL查詢中的空值?
代碼:
Private Sub cboCompany_Change()
Dim customerName As String
customerName = cboCompany.Value
customerName = Replace(customerName, "'", "''")
Dim i As Integer
Dim cn As ADODB.Connection
Dim rsT As New ADODB.Recordset
Dim customer As String
Dim postcode As String
Dim address1 As String
Dim suburb As String
Dim addressType As String
Dim state As String
Dim country As String
Set cn = New ADODB.Connection
With cn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data Source=C:\Customer.xls;Extended Properties=Excel 8.0;"
.CursorLocation = adUseClient
.Open
End With
rsT.Open "SELECT Customer, Postcode, [Address 1] AS Address1, [Postal Suburb] AS Suburb, [Address Type] AS AddressType, State, Country FROM Customers WHERE [Address Type] = 'Postal Address' AND Customer = '" & customerName & "'", cn, adOpenStatic
i = 0
With rsT
Do Until .EOF
customer = rsT.Fields("Customer")
postcode = rsT.Fields("Postcode")
address1 = rsT.Fields("Address1")
suburb = rsT.Fields("Suburb")
addressType = rsT.Fields("AddressType")
state = rsT.Fields("State")
country = rsT.Fields("Country")
CompanyAddress1.Value = address1
CompanyAddress2.Value = suburb + " " + state + " " + postcode + " " + country
CompanyName.Value = customer
.MoveNext
i = i + 1
Loop
End With
End Sub
然而,如果其中一個字段(郊區爲例)是空的形式崩潰,所以我怎麼能處理呢?
你的標題說的是Word,但你的問題是在談論Excel。 – 2010-10-20 22:29:07
我在Word文檔中有宏,但它從Excel電子表格中提取數據。 – Morgan 2010-10-20 22:30:59
無論如何,與郊區= rsT.Fields(「郊區」)和郊區爲該客戶爲null,我該如何處理? – Morgan 2010-10-20 22:32:03