5

I have two forms named frmChooseDBase and frmMain. frmChooseDBase is for choosing a file(a database file). Once he's done choosing the database, frmMain will load the database chosen from frmChooseDBase. How can I do dat? any help. Here's my sample codes:

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
varsha
  • 1,620
  • 1
  • 16
  • 29
Fvcundo
  • 95
  • 2
  • 3
  • 11

2 Answers2

14

If you just want to load a file, you don't need to make a new form. Just have a button or menu item that says load DB. Clicking that will pop an OpenFileDialog. Drag an openFileDialog control onto the form and give it a meaningful name (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

There are plenty of examples for using the openFileDialog control if you get stuck, or need quick help.

Mathemats
  • 1,185
  • 4
  • 22
  • 35
  • Ok, so instead of getting da name of the file. Maybe its better to get the filepath bcoz I want to replace it in DataSource of the connectionString. Check dis, con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & [THIS IS WHERE I WANT TO PASTE THE PATH] & "';Extended Properties=Excel 8.0" – Fvcundo Jan 09 '15 at 06:58
  • @Fvcundo Just pop a messgebox and see what Filename contains, I think it might actually contain the full path name. – Mathemats Jan 09 '15 at 06:59
  • @Fvcundo No worries mate! – Mathemats Jan 09 '15 at 07:07
5

You don't even have to use a control:

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 
hericson
  • 120
  • 1
  • 8
  • I want to pass what was chose from the OpenFileDialog to connectionString. like this, con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Desktop\'" & [FROM WHAT I CHOSE FROM OPEN FILE] & "';Extended Properties=Excel 8.0" – Fvcundo Jan 09 '15 at 06:44