我有两个名为frmChooseDBase
和frmMain
的表单。 frmChooseDBase
用于选择文件(数据库文件)。完成选择数据库后,frmMain
将加载从frmChooseDBase
中选择的数据库。
我怎么做dat?任何帮助。
这是我的示例代码:
Public Class frmChooseDBase
Public sr As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
sr = OpenFileDialog1.FileName
Me.Hide()
FrmMain.Show()
End If
End Sub
End Class
Private Sub FrmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Desktop\'" & frmChooseDBase.sr & "';Extended Properties=Excel 8.0"
con.Open()
FillDGView("SELECT [CCCD Loading Database] AS [Transaction Date], [F2] AS [Unit Number], [F3] AS [Category], [F4] AS [Temp Required (C)], [F5] AS [Type Length], [F6] AS [T-State], [F7] AS [Position], [F8] AS [I/B Actual Visit], [F9] AS [Fright Kind] FROM [Loading$]")
End Sub
答案 0 :(得分:6)
如果您只想加载文件,则无需创建新表单。只需要一个按钮或菜单项,说明加载DB。单击它将弹出OpenFileDialog。将openFileDialog控件拖到窗体上,并为其指定一个有意义的名称(openFileDialog1 ...)
openFileDialog1.Title = "Please select a DB file"
openFileDialog1.InitialDirectory = "C:\"
openFileDialog1.Filter = "DB Files|*.extensionHERE"
If openFileDialog1.ShowDialog() = DialogResult.OK then
'Do things here, the path is stored in openFileDialog1.Filename
'If no files were selected, openFileDialog1.Filename is ""
End If
如果您遇到困难或需要快速帮助,有很多使用openFileDialog控件的示例。
答案 1 :(得分:0)
您甚至不必使用控件:
Dim ofd As OpenFileDialog = New OpenFileDialog
ofd.DefaultExt = "txt"
ofd.FileName = "defaultname"
ofd.InitialDirectory = "c:\"
ofd.Filter ="All files|*.*|Text files|*.txt"
ofd.Title = "Select file"
If ofd.ShowDialog() <> DialogResult.Cancel Then
MsgBox(ofd.FileName)
End If