0
我正在使用VB 2010 Express訪問.mdb格式的數據庫。 我想查找包含特定字符串和數字的行,所以我寫了while循環。我使用If語句來確保循環不超過最大行(MaxRows)並拋出錯誤。它一直給出的結果是:「找不到記錄」,即使數據庫中存在字符串。我究竟做錯了什麼?visual basic:找到匹配的字符串
備註:inc用於增加行數。消息框是隻以查看變量值
代碼:在消息框中的變量值的
Dim lpn As String
Dim lpn2 As String
inc = 0
lpn = TextBox5.Text
lpn2 = ds.Tables("RegisteredCarsDataSet").Rows(inc).Item(7)
MsgBox(lpn & " " & lpn2 & " ")
While lpn <> lpn2
If inc <> MaxRows - 1 Then
inc = inc + 1
lpn2 = ds.Tables("RegisteredCarsDataSet").Rows(inc).Item(7)
MsgBox(lpn & " " & lpn2)
Else
MsgBox("No record found.")
Exit While
End If
End While
TextBox1.Text = ds.Tables("RegisteredCarsDataSet").Rows(inc).Item(1)
TextBox2.Text = ds.Tables("RegisteredCarsDataSet").Rows(inc).Item(2)
TextBox3.Text = ds.Tables("RegisteredCarsDataSet").Rows(inc).Item(3)
TextBox4.Text = ds.Tables("RegisteredCarsDataSet").Rows(inc).Item(4)
ComboBox1.Text = ds.Tables("RegisteredCarsDataSet").Rows(inc).Item(5)
ComboBox2.Text = ds.Tables("RegisteredCarsDataSet").Rows(inc).Item(6)
TextBox5.Text = ds.Tables("RegisteredCarsDataSet").Rows(inc).Item(7)
Offtopic,但你不認爲發出直接的sql命令就像「select * from mytable where myfield like'mystring''?或者至少發佈類似條件的記錄集上的查找命令? – Arvo
我不熟悉sql,這是我第一次使用數據庫。我用我唯一的方式知道。但如果你知道更好的方法,請告訴我。 –
如果我使用「select * from mytable where myfield like'mystring'」,我應該將結果保存到新數據集中,是否正確? –