我已经开始构建应用程序,我可以在Access中导航表,并带入三个文本框ProjectID,ProjectName和ProjectManager。我还添加了4个按钮(MoveNext,MoveBack,MoveFirstRecord和MoveLastRecord)。
目前,当我加载应用程序时,我能够浏览记录并在文本框中显示项目名称,项目ID和项目管理器。我也可以移动下一步,向后移动,移动到第一条记录并移动最后一条记录。
我还设法添加了一个显示所有项目ID的组合,它们始终是唯一的。
我需要做的是,从该组合框中选择一个项目ID,然后单击一个名为“搜索项目”的新按钮,在文本框中填入组合中选择的项目ID的相应信息。
我很遗憾,我不知道如何以有效的方式解决这个问题。
这是我目前的代码:
Public Class Form2
Dim connection As New OleDb.OleDbConnection
Dim DataSet As New DataSet
Dim DataAdapter As OleDb.OleDbDataAdapter
Dim sql1 As String
Dim sql2 As String
Dim inc As Integer
Dim MaxRows As Integer
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
connection.ConnectionString = "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source=Z:\Corporate Reporting\2014_15\Corporate\QuarterlyReporting.accdb"
connection.Open()
'--For the text boxes--
sql1 = "SELECT * FROM Projects_Application"
DataAdapter = New OleDb.OleDbDataAdapter(sql1, connection)
DataAdapter.Fill(DataSet, "Projects")
'--For the combo--
sql2 = "SELECT ProjectID FROM Projects_Application ORDER BY ProjectID"
Dim cm As New OleDb.OleDbCommand(sql2, connection)
Dim dr As OleDb.OleDbDataReader = cm.ExecuteReader
While dr.Read
cmbSearch.Items.Add(dr(0).ToString)
End While
dr.Close()
connection.Close()
MaxRows = DataSet.Tables("Projects").Rows.Count
inc = -1
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub NavigateRecords()
txtProjectName.Text = DataSet.Tables("Projects").Rows(inc).Item(0)
txtProjectID.Text = DataSet.Tables("Projects").Rows(inc).Item(1)
txtProjectManager.Text = DataSet.Tables("Projects").Rows(inc).Item(2)
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
If inc <> (MaxRows - 1) Then
inc = inc + 1
NavigateRecords()
End If
End Sub
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
If inc > 0 Then
inc = inc - 1
NavigateRecords()
End If
End Sub
Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
If inc > 0 Then
inc = 0
NavigateRecords()
End If
End Sub
Private Sub bntLastR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bntLastR.Click
If inc <> (MaxRows - 1) Then
inc = MaxRows - 1
NavigateRecords()
End If
End Sub
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
??? ???
End Sub
End Class
答案 0 :(得分:0)
由于您的数据已经是本地的(在内存中),您不需要为它查询数据库,您可以对该表运行查询。此链接很好地解释了它:MSDN
以下是该网站的示例:
Dim table As DataTable = DataSet1.Tables("Orders")
' Presuming the DataTable has a column named Date.
Dim expression As String
expression = "Date > #1/1/00#"
Dim foundRows() As DataRow
' Use the Select method to find all rows matching the filter.
foundRows = table.Select(expression)
答案 1 :(得分:0)
在您的搜索点击事件中;
sql2 = "SELECT col1,col2,col3 FROM Projects_Application where ProjectID = '" & cmbSearch.value & "'"
Dim cm As New OleDb.OleDbCommand(sql2, connection)
Dim dr As OleDb.OleDbDataReader = cm.ExecuteReader
While dr.Read
textbox1.text = dr(0)
textbox2.text = dr(1)
textbox3.text = dr(2)
End While
dr.Close()
connection.Close()
注意:col1,2,3 ect将是数据库中的列,textbox1,2,3将是您希望显示数据的文本框。只读。