|
各位大大,
困扰呀。有人能解一下吗???
strSQL = "SELECT * FROM 表1" --- 正常显示
strSQL = "SELECT * FROM 表1 where 字段1 = " & Sheet1.TextBox2.value & "" ------ error 。看图3
我的code , 不好意思。不是自己写的。就是网路copy 和 AI 生成的。
Private Sub UserForm_Initialize()
With UserForm1.ListView1
.view = lvwReport
.Gridlines = True
.FullRowSelect = True
.HideSelection = False
.ColumnHeaders.Add , , "ID", 20, lvwColumnLeft
.ColumnHeaders.Add , , "字段1", 100, lvwColumnCenter
.ColumnHeaders.Add , , "字段2", 100, lvwColumnCenter
.ColumnHeaders.Add , , "字段3", 100, lvwColumnCenter
.ColumnHeaders.Add , , "字段4", 100, lvwColumnCenter
End With
Application.ScreenUpdating = False
UserForm1.ListView1.ListItems.Clear
Dim cnn As New ADODB.Connection
Dim RST As New ADODB.Recordset
Dim stpath, strSQL As String
Dim lvItem As listItem
File_Path = Sheet1.TextBox1.value
' Con.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & File_Path & ";"
cnn.connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & File_Path & ";"
cnn.Open
' strSQL = "SELECT * FROM 表1"
strSQL = "SELECT * FROM 表1 where 字段1 = " & Sheet1.TextBox2.value & ""
RST.Open strSQL, cnn, adOpenKeyset, adLockOptimistic, adCmdText
If RST.BOF = False Then
RST.MoveLast
icount = RST.RecordCount
Debug.Print icount
RST.MoveFirst
For i = 1 To icount
Set lvItem = UserForm1.ListView1.ListItems.Add(, , RST.Fields("ID").value)
UserForm1.ListView1.ListItems(i).SubItems(1) = IIf(IsNull(RST.Fields("字段1")), "", RST.Fields("字段1"))
UserForm1.ListView1.ListItems(i).SubItems(2) = IIf(IsNull(RST.Fields("字段2")), "", RST.Fields("字段2"))
UserForm1.ListView1.ListItems(i).SubItems(3) = IIf(IsNull(RST.Fields("字段3")), "", RST.Fields("字段3"))
UserForm1.ListView1.ListItems(i).SubItems(4) = IIf(IsNull(RST.Fields("字段4")), "", RST.Fields("字段4"))
RST.MoveNext
Next i
End If
RST.Close
cnn.Close
Application.ScreenUpdating = True
End Sub
|
|