返回列表 上一主題 發帖

[發問] 我有一個EXCEL 連結資料庫的問題要發問

[發問] 我有一個EXCEL 連結資料庫的問題要發問

本帖最後由 cp914682 於 2023-9-1 20:49 編輯

我再用EXCEL查詢ACCESS裡面的資料  我要找相似的資料 我有用過 *也用過% 下去尋找
但是不管怎麼查都是空白,然後照原本的select * from 資料 where條件 like '" & Trim(UF1.TB2.Text) & "'")  這樣查得出來
程式碼如下
-------------------------------------------------------------------------------------------- 分割線
Application.ScreenUpdating = False

Sheets("資料").Range("A2:CE" & r).ClearContents
UF1.LBox1.Clear
'宣告物件
Dim mycon As New ADODB.Connection
Dim myrs As New ADODB.Recordset
If UF1.TB2.Text = "" Then

mycon.Open "provider=microsoft.jet.oledb.4.0;" & "data source=" & ThisWorkbook.Path & "資料庫.mdb;"

Set myrs = mycon.Execute("select * from 資料 where 條件 '%" & Trim(UF1.TB1.Text) & "%'")

Sheets("資料").Range("a2").CopyFromRecordset myrs

myrs.Close
mycon.Close

Set myrs = Nothing
Set mycon = Nothing

r=sheets("資料").range("b1").end (xldown).row

For I = 2 To r
If UF1.TB1.Text = Sheets("資料").Cells(I, 2) Then

    UF1.LBox1.AddItem Trim(Sheets("資料").Cells(I, 2)) & " " & Trim(Sheets("資料").Cells(I, 3)) & " " & Format(Trim(Sheets("資料").Cells(I, 4)), "e/m/dD") & " " & Trim(Sheets("資料").Cells(I, 10)) & " " & Trim(Sheets("資料").Cells(I, 11))
  End If
Next I
End If
If UF1.TB1.Text = "" Then

mycon.Open "provider=microsoft.jet.oledb.4.0;" & "data source=" & ThisWorkbook.Path & "\±ø¥óªí.mdb;"

Set myrs = mycon.Execute("select * from 資料 where條件 like '%" & Trim(UF1.TB2.Text) & "%'")

Sheets("¸資料").Range("a2").CopyFromRecordset myrs

myrs.Close
mycon.Close

Set myrs = Nothing
Set mycon = Nothing
r = Sheets("資料").Range("B1").End(xlDown).Row


For I = 2 To r
If UF1.TB2.Text = Sheets("資料").Cells(I, 10) Then

    UF1.LBox1.AddItem Trim(Sheets("資料").Cells(I, 2)) & " " & Trim(Sheets("資料").Cells(I, 3)) & " " & Format(Trim(Sheets("資料").Cells(I, 4)), "e/m/dD") & " " & Trim(Sheets("資料").Cells(I, 10)) & " " & Trim(Sheets("資料").Cells(I, 11))
  End If
Next I
End If


Application.ScreenUpdating = True

回復 1# cp914682


    以解決

TOP

        靜思自在 : 一個人的快樂.不是因為他擁有得多,而是因為他計較得少。
返回列表 上一主題