我对VB很愚蠢。我有我的班级(dalv1
),我的所有功能都存储在我的主表格中。
代码如下:
Public Sub getCustomerDetails()
Try
Dim cust_dataTable As New DataTable()
Dim Connection As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source = C:\Users\Public\Documents\VP - A1\Database\Database.mdb"
Dim SQL As String = ("SELECT * FROM tblCustomers")
Dim dataAdapter As New OleDbDataAdapter(SQL, Connection)
dataAdapter.Fill(cust_dataTable)
dataAdapter.Dispose()
Catch ex As Exception
MsgBox("ERROR : " & ex.Message.ToString)
End Try
End Sub
我想将DataTable cust_dataTable
传递给主窗体,以便在表单加载时填充文本框。
答案 0 :(得分:2)
您需要将其转换为函数,以便表格返回给调用者:
Public Function getCustomerDetails() As DataTable
Dim cust_dataTable As New DataTable()
Try
Dim Connection As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source = C:\Users\Public\Documents\VP - A1\Database\Database.mdb"
Dim SQL As String = ("SELECT * FROM tblCustomers")
Dim dataAdapter As New OleDbDataAdapter(SQL, Connection)
dataAdapter.Fill(cust_dataTable)
dataAdapter.Dispose()
Catch ex As Exception
MsgBox("ERROR : " & ex.Message.ToString)
End Try
Return cust_dataTable
End Function
您没有分享您的表单代码,但这里有一个如何调用它的示例:
Private da As New dalv1
Private _MyDataTable As DataTable
Protected Overrides Sub OnLoad(ByVal e As EventArgs)
MyBase.OnLoad(e)
_MyDataTable = da.getCustomerDetails
End Sub