Problem with Open File Dialog

May 17 2005 8:03 PM
My program supposed to use the Open File Dialog to allow the user to locate de Database file just in case that file is not available at the default location. But I don't know how to setup the FileOk event handler in order to Open the file without error. This is what I did so far: Option Explicit On Option Strict On Imports System.Convert Imports System.Data 'Declare a structure, it allows us to group related data items together. Public Structure scontactRec Public sFirst As String Public sLast As String Public sEmail As String End Structure Public Class frmContact Inherits System.Windows.Forms.Form 'Declare some public variables Public sCounter, totalCount As Integer 'Declare an array of structure. It is declared as a type. Public contactRec(99) As scontactRec #Region " Windows Form Designer generated code " ' Declate two constants for the EditState procedure. These constants will ' indicate whether the current record is being edited or is not being edited. Private Const cblnEditing As Boolean = True Private Const cblnNotEditing As Boolean = False 'Load the database. Private Sub frmContact_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load Try OleDbDataAdapter1.Fill(DsContacts1) Call EditState(cblnNotEditing) Catch ex As SystemException MessageBox.Show(ex.Message, "Error") Call FileOpen() End Try End Sub 'Unload the form from the Menu bar. Private Sub mmuFileExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mmuFileExit.Click Me.Close() End Sub 'Enable a record for editing. Private Sub mmuEditEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mmuEditEdit.Click EditState(cblnEditing) End Sub 'Set the EditState. This procedure enables or disables the text boxes on the form. 'It also prevents the user from navigating to a different record while a record is being edited. Private Sub EditState(ByVal plnState As Boolean) Select Case plnState Case cblnEditing mmuEditAdd.Enabled = False mmuEditEdit.Enabled = False mmuEditUpdate.Enabled = True mmuEditCancel.Enabled = True mmuEditDelete.Enabled = False txtFName.Enabled = True txtLName.Enabled = True txtEmail.Enabled = True Case cblnNotEditing mmuEditAdd.Enabled = True mmuEditEdit.Enabled = True mmuEditUpdate.Enabled = False mmuEditCancel.Enabled = False mmuEditDelete.Enabled = True txtFName.Enabled = False txtLName.Enabled = False txtEmail.Enabled = False End Select End Sub 'Add a new row to the dataset. Private Sub mmuEditAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mmuEditAdd.Click 'Set the menu items to indicate that a record is being edited. EditState(cblnEditing) 'Add the new record. Me.BindingContext(DsContacts1, "ContactList").AddNew() End Sub 'Enable editing on the current record. Private Sub mmuEditUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mmuEditUpdate.Click Dim pdsInsRows, pdsModRows As DataSet 'Set the menu items to indicate that a record is being edited. EditState(cblnNotEditing) 'End editing on the current record. Me.BindingContext(DsContacts1, "ContactList").EndCurrentEdit() pdsInsRows = DsContacts1.GetChanges(DataRowState.Added) pdsModRows = DsContacts1.GetChanges(DataRowState.Modified) 'Check to see if there is an inserted row. If there is, update the Dataset. If Not pdsInsRows Is Nothing Then OleDbDataAdapter1.Update(pdsInsRows) End If 'Check to see if there is a modified row. If there is, update the Dataset. If Not pdsModRows Is Nothing Then OleDbDataAdapter1.Update(pdsModRows) End If 'Update the dataset for all the changes. DsContacts1.AcceptChanges() End Sub 'Delete the current record. Private Sub mmuEditDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mmuEditDelete.Click Dim pdsDelRows As DataSet 'Remove the current record. Me.BindingContext(DsContacts1, "ContactList").RemoveAt _ (Me.BindingContext(DsContacts1, "ContactList").Position) 'Generate the temporary Dataset to store the deleted row. pdsDelRows = DsContacts1.GetChanges(DataRowState.Deleted) 'Delete the row from the database. OleDbDataAdapter1.Update(pdsDelRows) 'Update the dataset for all the changes. DsContacts1.AcceptChanges() End Sub 'Cancel editing on the current record. Private Sub mmuEditCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mmuEditCancel.Click EditState(cblnNotEditing) Me.BindingContext(DsContacts1, "ContactList").CancelCurrentEdit() End Sub 'Allows to display the previous record. Private Sub btnMBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMBack.Click Me.BindingContext(DsContacts1, "ContactList").Position -= 1 End Sub 'Display the next record. Private Sub btnMForw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMForw.Click Me.BindingContext(DsContacts1, "ContactList").Position += 1 End Sub 'Reset the fields for new input. Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click txtFName.Text = "" txtLName.Text = "" txtEmail.Text = "" End Sub Private Sub OpenFileDialog2_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog2.FileOk NEED SOME CODE HERE!!! End Sub Public Sub FileOpen() OpenFileDialog2 = New System.Windows.Forms.OpenFileDialog With OpenFileDialog2 .Title = "Select a file to open" .Filter = "Microsoft Office Access|*.mdb|All Files|*.*" .FilterIndex = 1 .ShowDialog() End With End Sub End Class Thank you for your help

Answers (3)