在vb里進行SQL查詢時總是提示陳述句錯誤,能幫我看下這代碼哪里有問題嗎?
Dim strSQL As String
Dim strCon(7) As String
Dim i As Integer
If Text1.Text <> "" Then
If Check1.Value = 1 Then
strCon(1) = "貨物申請表.申請單號 like '%" & Text1.Text & "%'"
Else
strCon(1) = "貨物申請表.申請單號='" & Text1.Text & "'"
End If
Else
strCon(1) = ""
End If
If Text2.Text <> "" Then
If Check1.Value = 1 Then
strCon(2) = "貨物申請表.運單號 like '%" & Text2.Text & "%'"
Else
strCon(2) = "貨物申請表.運單號='" & Text2.Text & "'"
End If
Else
strCon(2) = ""
End If
If Text3.Text <> "" Then
If Check1.Value = 1 Then
strCon(3) = "客戶資訊表.地址 like '%" & Text3.Text & "%'"
Else
strCon(3) = "客戶資訊表.地址 ='" & Text3.Text & "'"
End If
Else
strCon(3) = ""
End If
If Text4.Text <> "" Then
If Check1.Value = 1 Then
strCon(4) = "貨物申請表.收件人地址 like '%" & Text4.Text & "%'"
Else
strCon(4) = "貨物申請表.收件人地址='" & Text4.Text & "'"
End If
Else
strCon(4) = ""
End If
If Text5.Text <> "" Then
If Check1.Value = 1 Then
strCon(5) = "客戶資訊表.客戶姓名 like '%" & Text5.Text & "%'"
Else
strCon(5) = "客戶資訊表.客戶姓名='" & Text5.Text & "'"
End If
Else
strCon(5) = ""
End If
If Text6.Text <> "" Then
If Check1.Value = 1 Then
strCon(6) = "貨物申請表.收件人姓名 like '%" & Text6.Text & "%'"
Else
strCon(6) = "貨物申請表.收件人姓名='" & Text6.Text & "'"
End If
Else
strCon(6) = ""
End If
If Combo2.Text = "根據申請單號查詢" Then
If Text7.Text <> "" Then
strCon(7) = "貨物申請表.申請單號 ='" & Text7.Text & "'"
Else
strCon(7) = ""
End If
End If
If Combo1.Text = "根據目的地查詢" Then
If Text7.Text <> "" Then
strCon(7) = "貨物申請表.收件人地址='" & Text7.Text & "'"
Else
strCon(7) = ""
End If
End If
If Combo2.Text = "根據發件人查詢" Then
If Text7.Text <> "" Then
strCon(7) = "客戶資訊表.客戶姓名 ='" & Text7.Text & "'"
Else
strCon(7) = ""
End If
End If
If Combo2.Text = "根據收件人查詢" Then
If Text7.Text <> "" Then
strCon(7) = "貨物申請表.收件人姓名 ='" & Text7.Text & "'"
Else
strCon(7) = ""
End If
End If
If Combo2.Text = "根據快件狀態查詢" Then
If Text7.Text <> "" Then
strCon(7) = "在途跟蹤資訊表.運輸狀態 ='" & Text7.Text & "'"
Else
strCon(7) = ""
End If
End If
For i = 1 To 7
If strCon(i) <> "" Then
strSQL = strSQL + " and " + strCon(i)
End If
Next
strSQL = "select 貨物申請表.申請單號,貨物申請表.運單號,客戶資訊表.地址," _
& " 貨物申請表.收件人地址,客戶資訊表.客戶姓名,貨物申請表.收件人姓名,在途跟蹤資訊表.運輸狀態 from 貨物申請表,在途跟蹤資訊表,客戶資訊表" _
& " where 貨物申請表.申請單號 = 在途跟蹤資訊表.申請單號 and 貨物申請表.運單號 = 在途跟蹤資訊表.運單號"
Adodc1.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;User ID=sa;Data Source=物流管理;Initial Catalog=W-L"
Adodc1.CursorLocation = adUseClient
Adodc1.CommandType = adCmdText
Adodc1.RecordSource = strSQL
Adodc1.Refresh
Set DataGrid1.DataSource = Adodc1
uj5u.com熱心網友回復:
Debug.Print strSQL
然后把陳述句貼到 SSMS 的查詢視窗中執行,自然會指出錯誤行。
通過手工分行來確定錯誤位置。
uj5u.com熱心網友回復:
Sql查詢句寫法貌似有錯誤。uj5u.com熱心網友回復:
SQL查詢語言主要結構為:Select 查詢欄位 from 表名 Where 查詢條件陳述句 [排序陳述句或分組陳述句]
uj5u.com熱心網友回復:
將SQL陳述句列印出來,復制粘貼到查詢分析器里面手動運行,必要的話還可以單步除錯,無誤后再繼續。uj5u.com熱心網友回復:
竟然用中文命名。。 嘗試刪掉一些sql陳述句看看吧。 估計是你表和欄位搞錯了,比如某個欄位根本不存在于這個表中,你還去.它uj5u.com熱心網友回復:
中文名可能得用半角方括號[]括住。uj5u.com熱心網友回復:
strSQL = "select 貨物申請表.申請單號,貨物申請表.運單號,客戶資訊表.地址," _& " 貨物申請表.收件人地址,客戶資訊表.客戶姓名,貨物申請表.收件人姓名,在途跟蹤資訊表.運輸狀態 from 貨物申請表,在途跟蹤資訊表,客戶資訊表" _
& " where 貨物申請表.申請單號 = 在途跟蹤資訊表.申請單號 and 貨物申請表.運單號 = 在途跟蹤資訊表.運單號 and 貨物申請表.客戶ID = 客戶資訊表.客戶ID"
沒有給出客戶資訊表的關聯條件。
uj5u.com熱心網友回復:
路過學習一下,剛好有個專案會涉及到SQL資料庫。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/62430.html
標籤:網絡編程
下一篇:VB模糊判斷
